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
Tiny Networking in Swift
Search
Chris Eidhof | @chriseidhof
December 09, 2014
Technology
2
19k
Tiny Networking in Swift
Chris Eidhof | @chriseidhof
December 09, 2014
Tweet
Share
More Decks by Chris Eidhof | @chriseidhof
See All by Chris Eidhof | @chriseidhof
Dutch FP Day 2015
chriseidhof
2
390
Functional Swift - Brooklyn
chriseidhof
3
1.3k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.3k
Functional Swift
chriseidhof
1
160
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
210
Parsing with Blocks
chriseidhof
2
240
Practical Concurrent Programming
chriseidhof
4
290
Other Decks in Technology
See All in Technology
(技術的には)社内システムもOKなブラウザエージェントを作ってみた!
har1101
0
370
Prox Industries株式会社 会社紹介資料
proxindustries
0
110
StrandsとNeptuneを使ってナレッジグラフを構築する
yakumo
1
140
pool.ntp.orgに ⾃宅サーバーで 参加してみたら...
tanyorg
0
1.7k
Bill One急成長の舞台裏 開発組織が直面した失敗と教訓
sansantech
PRO
2
410
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
広告の効果検証を題材にした因果推論の精度検証について
zozotech
PRO
0
220
SchooでVue.js/Nuxtを技術選定している理由
yamanoku
3
270
Context Engineeringが企業で不可欠になる理由
hirosatogamo
PRO
3
720
ランサムウェア対策としてのpnpm導入のススメ
ishikawa_satoru
0
240
22nd ACRi Webinar - 1Finity Tamura-san's slide
nao_sumikawa
0
110
CDK対応したAWS DevOps Agentを試そう_20260201
masakiokuda
1
480
Featured
See All Featured
Accessibility Awareness
sabderemane
0
61
Unsuck your backbone
ammeep
671
58k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
330
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
230
Statistics for Hackers
jakevdp
799
230k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
290
AI: The stuff that nobody shows you
jnunemaker
PRO
2
280
Test your architecture with Archunit
thirion
1
2.2k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.3k
Transcript
Tiny Networking @chriseidhof
$ pod install AlamoFire
Live coding!
$ rake routes | grep sign_in ... api_user_session POST /api/users/sign_in(.:format)
$ curl "http://localhost:3000/api/users/sign_in" \ -d "{\"email\": \"
[email protected]
\", \"password\": \"testtes\"}" \
--header "Content-Type:application/json" \ {"message":"Error with your login or password"}~
$ curl "http://localhost:3000/api/users/sign_in" -d "{\"email\": \"
[email protected]
\", \"password\": \"testtest\"}" --header "Content-Type:application/json"
{"auth_token":"8yBZFpqhagTJr6DpDSyS","email":"
[email protected]
"}
func apiRequest<A>( baseURL: NSURL, resource: Resource<A>, failure: () -> (),
completion: A -> () )
struct Resource<A> { let path: String let method : Method
let headers : [String:String] let requestBody: NSData? let parse: NSData -> A? }
typealias JSONDictionary = [String:AnyObject] func decodeJSON(data: NSData) -> JSONDictionary? func
encodeJSON(dictionary: JSONDictionary) -> NSData?
Usage
let parse : NSData -> String? = { data in
if let jsonDict = decodeJSON(data) { return jsonDict["auth_token"] as? String } return nil }
let requestParameters = ["email": "
[email protected]
", "password": "testtest"] let jsonBody =
encodeJSON(requestParameters)
let path = "api/users/sign_in" let headers = ["Content-Type": "application/json", "Accept":
"application/json"] let resource = Resource(path: path, method: .POST, requestBody: jsonBody, headers: headers, parse: parse)
let baseURL = NSURL(string:"http://localhost:3000")! apiRequest(baseURL, resource, { println("failure") }, {
authorizationToken in println("Auth token \(authorizationToken)") })
let email = "
[email protected]
" let password = "testtest" let baseURL
= NSURL(string:"http://localhost:3000")! let resource = login(email, password) apiRequest(baseURL, resource, { println("failure") }, { authorizationToken in println("Auth token \(authorizationToken)") })
More live coding!
Resources — https:/ /gist.github.com/chriseidhof/ 26bda788f13b3e8a279c — http:/ /chris.eidhof.nl/posts/tiny-networking-in- swift.html —
http:/ /www.objc.io/books/
Thanks?
None
None
None
None
None