Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
外部APIとうまく付き合う
Search
makinoshi
February 17, 2018
Programming
0
360
外部APIとうまく付き合う
Clojureを使って外部APIを使うコードを設計する、書く方法について
makinoshi
February 17, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
3.3k
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
500
Kiroで始めるAI-DLC
kaonash
2
620
テストカバレッジ100%を10年続けて得られた学びと品質
mottyzzz
2
610
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
870
アセットのコンパイルについて
ojun9
0
130
AI時代のUIはどこへ行く?
yusukebe
18
9k
Android端末で実現するオンデバイスLLM 2025
masayukisuda
1
170
Swift Updates - Learn Languages 2025
koher
2
490
はじめてのMaterial3 Expressive
ym223
2
890
Improving my own Ruby thereafter
sisshiki1969
1
160
The Past, Present, and Future of Enterprise Java with ASF in the Middle
ivargrimstad
0
160
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Typedesign – Prime Four
hannesfritz
42
2.8k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
How STYLIGHT went responsive
nonsquared
100
5.8k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.7k
Building Applications with DynamoDB
mza
96
6.6k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Designing for humans not robots
tammielis
253
25k
How to Ace a Technical Interview
jacobian
279
23k
A Tale of Four Properties
chriscoyier
160
23k
Transcript
֎෦"1*ͱ ͏·͖͘߹͏ 2018-02-11 clj-ebisu #2 @makinoshi
Me • αΠϘζελʔτΞοϓεגࣜձࣾ • Like: C++(11~), PostgreSQL(SQL), Clojure, Emacs(Spacemacs), MS
Sculpt Ergonomic Keyboard • Hobby: ϚϯΨ,͓ञ • ࠷ۙͷΠνΦγ: cljr-create-fn-from-example
ΞδΣϯμ • (֎෦)APIʹΞΫηε͢Δ෦ΛͲ͏࡞Δ͔ • ೖྗ͢ΔσʔλߏΛͲ͏ͭ͘Δ͔ • ΧϓηϧԽ͞Εͨ෦ॲཧΛͭ͘Δ • ݁ՌΛද͢σʔλߏ
ೖྗ͢ΔσʔλߏΛ࡞Δ
ྫ) SendGridͷAPI {:headers {"Authorization" "Bearer ...."} :content-type :json :form-params {:subject
"໊݅" :content [{:type "text/plain" :value "ຊจ"}] :from {:name "MyService" :email "
[email protected]
"} :personalizations [{:to [{:email "
[email protected]
" :name "a͞Μ"} {:email "
[email protected]
" :name "a͞Μ"}] :custom_args {:id "1"}} {:to [{:email "
[email protected]
" :name “b͞Μ"}] :custom_args {:id “2"}}]}} • ͜ͷߏΛҙࣝͨ͠ίʔυॻ͖ͨ͘ͳ͍
Builderύλʔϯ • ֤ϝιουͰҾΛνΣοΫͭͭ͠σʔλཷ ΊࠐΉ • buildϝιουͰ͑ΔɺνΣοΫ͢Δ • ೖग़ྗܗࣜͷมߋΛ෦ͰٵऩͰ͖ΔˠAPIͷ มߋ͏APIࣗମͷมߋ༰қʹͳΔ
࣮ྫ (-> (mail/builder something-component) (mail/with-subject "໊݅") (mail/with-content "ຊจ") (mail/with-recipients [{:email
"
[email protected]
" :to-name "a͞Μ" :id 1 :callback “/app/path/1”} {:email "
[email protected]
" :to-name "b͞Μ" :id 2 :callback “/app/path/2”}]) mail/build) • AWS SDK for Javaͷ࣮͕BuilderύλʔϯΛ ͱ্ͯखʹ͍ͬͯΔͱࢥ͍·͢
෦ॲཧΛΧϓηϧԽ͢Δ
͏ଆʹ࿙Βͨ͘͠ͳ͍ • ͏ϥΠϒϥϦͷڍಈ • ྫ֎͕ൃੜ͢Δঢ়گͷࠩҟɺΓͷࠩҟ(map, promise, etc..) • ࣮ߦ •
rate limit • ಉ࣌ଓ • ϦτϥΠ • ෳͷϦΫΤετ୯ҰϦΫΤετͷγʔέϯεͱͯ͠ѻ͍͍ͨ
diehardͱڞʹ • https://github.com/sunng87/diehard • ઃఆϕʔεͰɺretry/circuit breaker/rate limitterΛ؆୯ʹ࣮Ͱ͖Δ • JavaͷϥΠϒϥϦɺnet.jodah/failsafeͷϥο ύ
࣮ྫ (diehard.core/defratelimiter rl {:rate 100}) ; per sec (clj-http.client/with-connection-pool {:timeout
10 :insecure? false :default-per-route 100} (->> coll (map #(future (diehard.core/with-retry {:retry-if (fn [resp exp] (or (>= (:status resp) 500) (instance? IOException exp))) :max-retries 3 :delay-ms 1000} (diehard.core/with-rate-limiter rl (api/call endpoint %))))) (map deref) doll)) • ΊͪΌͪ͘Ό؆୯ • ͬͱهड़ྔগͳ͘ఆٛ͢Δํ๏υΩϡϝϯτʹ͋Γ·͢
Γͷදݱ
ओͳΤϥʔॲཧ • ѻ͍͍͢Γ • ୯ͳΔσʔλ • Τϥʔ͔Ͳ͏͔ఆ͍͢͠ • Τϥʔʹͳͬͨͱ͖ͷҾΛͦͷ··औΓग़ͤΔ •
ѻ͍ʹ͍͘Γ • ྫ֎͕͛ΒΕͨΓɺ͛ΒΕͳ͔ͬͨΓ • ֎෦ͷϥΠϒϥϦͰఆٛ͞Εͨܕ • promise(ಛʹ֎෦ϥΠϒϥϦͷ) • ඇಉظ͕͍͍͔Ͳ͏͔ݺͼग़͠ଆͷࣄ • => GolangελΠϧ or ಠࣗఆٛͷEitherͷͲͪΒ͔͕Αͦ͞͏
࣮ࡍʹ྆ํͬͯΈͨ݁Ռ • GolangελΠϧͷํ͕ॻ͍ͯͯϛε͕গͳͦ͞͏ʹײ͍ͯ͡· ͢ • ѻ͍͘͢ɺγϯϓϧʹॻ͚Δ • (group-by #(-> %
second some?) results) (try (let [resp (clj-http.client/post endpoint params)] [{:resp resp resp :params params} nil]) (catch clojure.lang.ExceptionInfo e [{:params params} (ex-data e)]) (catch java.io.IOException e [{:params params} e]))
·ͱΊ • ClojureͰσʔλ࡞ͷநԽʹBuilder ཱͭ • ϥΠϒϥϦͷڍಈAPIͰ͋Δ͜ͱࣗମ֎͔ ΒӅ͢ˠޙʑ؆୯ʹมߋͰ͖Δ • Γྫ֎Ͱͳ͘σʔλͱͯ͠ѻ͍͘͢
͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ ͥͻ͝ҙݟΛԼ͍͞ɻ