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を生やすお話.pdf
Search
yagi2
August 03, 2018
0
440
おうちにAPIを生やすお話.pdf
Kyoto.LT 第21回
https://kyotolt.connpass.com/event/93043/
yagi2
August 03, 2018
Tweet
Share
More Decks by yagi2
See All by yagi2
Kotlin DSLを理解してみる / Understanding Kotlin DSL
yagi2
4
2.4k
略解 The Navigation Architecture Component
yagi2
1
200
10分で完全に(雰囲気を)理解するマテリアルデザイン
yagi2
1
400
KtorでおうちAPIをリプレースした話
yagi2
1
990
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Building an army of robots
kneath
302
43k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Building Adaptive Systems
keathley
38
2.3k
How to Ace a Technical Interview
jacobian
276
23k
The World Runs on Bad Software
bkeepers
PRO
65
11k
RailsConf 2023
tenderlove
29
900
Building Applications with DynamoDB
mza
90
6.1k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Docker and Python
trallard
40
3.1k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Transcript
͓͏ͪʹAPIΛੜ͓͢ 2018/08/03 Kyoto.LT #21 γʔαʔגࣜձࣾ Itsuki AOYAGI (@yagi2)
ࣗݾհ • ੨༄ थʢItsuki AOYAGI) • ͗ʹ͍ʢ@yagi2, @yaginierʣ • γʔαʔגࣜձࣾ
• AndroidΞϓϦΤϯδχΞ • ͓͏͕ͪେ͖
ࣗݾհ • ੨༄ थʢItsuki AOYAGI) • ͗ʹ͍ʢ@yagi2, @yaginierʣ • γʔαʔגࣜձࣾ
• AndroidΞϓϦΤϯδχΞ • ͓͏͕ͪେ͖
࠷ۙ؆୯ʹ͓͏ͪͱITΛܨ͛ΒΕΔ ɾGoogle Home ɾAmazon Alexa ɾHomePod ……
࠷ۙ؆୯ʹ͓͏ͪͱITΛܨ͛ΒΕΔ ɾGoogle Home ɾAmazon Alexa ɾHomePod …… ͕ɺҙ֎ͱͰ͖Δ͜ͱݶΒΕ͍ͯΔ……
σϑΥϧτͰແ͍ػೳΛΔʹ • ࣗͰΞΫγϣϯεΩϧΛ͏ • ൺֱత؆୯ʹՃͰ͖Δ͕ॊೈੑऑ͍ • IFTTTΛͬͯWebhook͍ͯ͠Ζ͍ΖΔ • Webhook͢ΔͷͰՄೳੑແݶେ͕ͩ༻ҙ͢Δͷଟ͍
σϑΥϧτͰແ͍ػೳΛΔʹ • ࣗͰΞΫγϣϯεΩϧΛ͏ • ൺֱత؆୯ʹՃͰ͖Δ͕ॊೈੑऑ͍ • IFTTTΛͬͯWebhook͍ͯ͠Ζ͍ΖΔ • Webhook͢ΔͷͰՄೳੑແݶେ͕ͩ༻ҙ͢Δͷଟ͍
͓͏ͪAPI
APIʢAPlication Interfaceʣ ͬͯΔਓ
APIͬͨ͜ͱ͋Δਓ
API࡞ͬͨ͜ͱ͋Δਓ
ࣗʹAPIαʔό͕͋Δਓ
͓͏ͪAPI • ࣗͷ͋Ε͜ΕΛHTTP(S)ܦ༝Ͱ͋Ε͜ΕͰ͖ ΔΑ͏ʹ͢Δͭ • ͓͏ͪͷঢ়ଶʢGETʣ • Ոిͷૢ࡞ʢPOSTʣ
ԿΛ࣮ݱ͍͔ͨ͠ • PCͷిݯΛೖΕ͍ͨʢWake on LANʣ • ΠϯλʔωοτϥδΦͷૢ࡞Λ͍ͨ͠ • ΤΞίϯͷՈిͷૢ࡞Λ͍ͨ͠ •
෦ͷԹͱ͔औΓ͍ͨ
ࠓճेീ൪ͷKotlinͰAPIΛ࡞Δ
KotlinͰͷAPI • બࢶ͍Ζ͍Ζ͋ΔʢͳʹͤJavaͷࢿ࢈͕͑ΔͷͰʣ • SpringBootવΓ…… • KotlinͱSpring BootΛͬͯαΫοͱୟ͚ΔAPIΛ࡞Δ • ͔͠͠KotlinͰΓ͍ͨ
• ͷͰKtorͱ͍͏ϑϨʔϜϫʔΫΛ͏
Ktor • ΊͬͪΌബ͍WebΞϓϦͷϑϨʔϜϫʔΫ ( https://github.com/ktorio/ktor ) • ඇಉظॲཧ͍͍ײ͡ʹͳ͍ͬͯΔ • KotlinͷCoroutinesͱ͍͏ػೳ
fun main(args: Array<String>) { embeddedServer(Netty, 8080) { routing { get("/") { call.respondText("Hello, world!", ContentType.Text.Html) } } }.start(wait = true) }
KtorͰͷAPI • https://github.com/yagi2/Kaya • Private Repository • ͱ͍࣮͑ࠓճͷຊ࣭తͳ෦Ͱͳ͍ͷͰলུ • KtorͰ͓͏ͪAPIΛϦϓϨʔεͨ͠
- Speaker Deck
ͯ͞APIͨ͠
ͳͥAPIΛ࡞ͬͨͷ͔ • Application Interface • ͭ·ΓAPIΛ༻ҙ͢Ε͍ΖΜͳͱ͜Ζ͔Β͑Δ • Web,εϚϗ,HomeܥσόΠε
ࠓճͷߏ • API • Kotlin+KtorΛRaspberryPi্Ͱಈ͔͢ʢࣗʣ • ΫϥΠΞϯτ • WebϒϥβʢૉHTMLʣ •
AndroidΞϓϦʢेീ൪ʣ • Google Home miniʢࣗʣ ※DDNSΛͬͯࣗ֎͔ΒAPIʹΞΫηεՄೳ
ྑ͍ • ͓͏ͪͷ͋Ε͜Εʹର͢ΔೖྗͷΠϯλʔϑΣʔε͕૿͑ͨ • εϚϗࣾձͳݱʹͽͬͨΓ • ೖྗΠϯλʔϑΣʔε͕ଟ͍ʹӽͨ͜͠ͱͳ͍࠷ߴ • ͳΜͳΒcurlͰՈిૢ࡞Ͱ͖Δ •
͓͏ͪͷ͋Ε͜ΕΛҰݩཧͰ͖Δͷྑ͍ • APIΛ࡞Δೳྗؒҧ͍ͳ্͕͘Δ
ྑ͘ͳ͍ • ͳΜ͔RasPi͕ࢮΜͩΓϓϩηε͕ࢮΜͩΓ͢Δ • MackerelͰࢮ׆ࢹ͍ͯ͠Δ • ঢ়ଶΛRasPiʹͨͤ͗͢Δͱࠓਖ਼نͷϦϞίϯ͔Βૢ࡞ Λͨ͘͠ͳ͘ͳΔ • APIΛհ͞ͳ͍ͷͰঢ়ଶཧ͕໘ʹͳΔ
ࠓޙΓ͍ͨ͜ͱ • ࠓRESTfulAPIͳͷͰGraphQLʹͨ͠Γ͍ͨ͠ɻ • WebUI࡞Γ͍ͨ • Թͱ͔֎෦ͷՈిૢ࡞ɺऔಘʹ͍ΖΜͳόΠφϦ͕ґଘͯ͠ ΔͷͰDockerͱ͔ͰυΧυΧ͍ͨ͠
࣮ࡍʹى͖ͨා͍
ͦͷ̍ ͳΜ͔͍Ζ͍Ζઢͯͨ͠ΒLED͕ ম͖͖Εͨ
ిؾͷษڧΛ͔ͬ͠Γ͠·͠ΐ͏
ͦͷ̎ ͳΜ͔উखʹPC͕͍ͨΓϥδΦ ͕͍ͨΓ͢Δոحݱ
https://twitter.com/rombulow/status/990684453734203392
Կ͕ى͖ͨͷ͔ • GETͰτάϧͳϦΫΤετΛ࣮͢Δ • ςϨϏʹGET → ిݯONͳΒOFF OFFͳΒON • Chromeͷ৽نλϒ։͍ͨͱ͖ͷ࠷ۙ։͍ͨϖʔδʹAPI
͏ϖʔδ͕͋ΔͱGET͕ϦΫΤετ͞ΕΔ
HTTPΛཧղͤΑ HTTPͷGETႈʹ͢Δ͖Ͱ ͋ͬͯɺ෭࡞༻ΛͨͤΔͱ Googleͱ͔ͷBOTͱ͔ͷΫϩʔϥ ͕͓Ոʹ৵ೖͯ͘͠Δ
ͱ͍͑ΊͪΌΜ͜ศརͩ͠ ݱGoogle Homeͱ͔͋ΔͷͰ ΈΜͳΖ͏ ͓ΘΓ