Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Tiny Networking in Swift
Search
Chris Eidhof | @chriseidhof
December 09, 2014
Technology
19k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Tiny Networking in Swift
Chris Eidhof | @chriseidhof
December 09, 2014
Transcript
23 slides
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
More Decks by Chris Eidhof | @chriseidhof
See All by Chris Eidhof | @chriseidhof
Dutch FP Day 2015
chriseidhof
2
420
Functional Swift - Brooklyn
chriseidhof
3
1.3k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.3k
Functional Swift
chriseidhof
1
190
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
230
Parsing with Blocks
chriseidhof
2
280
Practical Concurrent Programming
chriseidhof
4
320
Other Decks in Technology
See All in Technology
NutanixへのSSHアクセスの今後について
shadowhat
0
190
Welcome JXL!
dynamis
0
990
本の挿絵に困ったから挿絵メーカーを自作した
yoshimura_datam
1
120
Kubernetes, Agones, WebRTC, GPU で作る クラウドゲーミング基盤を本番運用に乗せるまで
vivion
1
290
社内アプリ基盤「Quartz」による、 非エンジニアとAIに本番デプロイを 任せても壊れないプラットフォームの設計
staka121
PRO
1
250
競馬予想をAIでやってみた話
yoshimura_datam
0
150
技術書典で本を売る流れ
yoshimura_datam
0
200
セキュリティチームは どのコンテナイメージの脆弱性対応を優先すべきか 〜CISA新指針とSCCで作る脆弱性トリアージ〜
swdtkuy
0
360
リリースしたで終わらない。 観測できるフロントエンド開発
yamakenji24
4
1.6k
gradle-best-practices 使ってみた
mikanichinose
0
240
全てを疑え!非自明な挙動と異常系から考える堅牢なカスタムコントローラ開発入門(CNDW2026)
lycorptech_jp
PRO
0
120
AWS Blocksで AIエージェント作れるの知ってます?
kmiya84377
1
170
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
680
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.3k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
390
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
690
HDC tutorial
michielstock
2
1k
Testing 201, or: Great Expectations
jmmastey
46
8.4k
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
3
180
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
1k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
390