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
380
Functional Swift - Brooklyn
chriseidhof
3
1.2k
Functional Swift - SF
chriseidhof
6
26k
Functional Swift
chriseidhof
6
1.2k
Functional Swift
chriseidhof
1
140
Functional Programming in Swift
chriseidhof
40
19k
Lighter View Controllers
chriseidhof
4
190
Parsing with Blocks
chriseidhof
2
220
Practical Concurrent Programming
chriseidhof
4
270
Other Decks in Technology
See All in Technology
関東Kaggler会LT: 人狼コンペとLLM量子化について
nejumi
3
460
AndroidデバイスにFTPサーバを建立する
e10dokup
0
240
Fintech SREの挑戦 PCI DSS対応をスマートにこなすインフラ戦略/Fintech SRE’s Challenge: Smart Infrastructure Strategies for PCI DSS Compliance
maaaato
0
450
High Performance PHP
cmuench
0
140
目の前の仕事と向き合うことで成長できる - 仕事とスキルを広げる / Every little bit counts
soudai
22
5.8k
APIファーストで実現する運用性の高い IoT プラットフォーム: SORACOMのアプローチ
soracom
PRO
0
240
All you need to know about InnoDB Primary Keys
lefred
0
120
偶然 × 行動で人生の可能性を広げよう / Serendipity × Action: Discover Your Possibilities
ar_tama
1
740
SCSAから学ぶセキュリティ管理
masakamayama
0
140
PL900試験から学ぶ Power Platform 基礎知識講座
kumikeyy
0
110
Kubernetes x k6 で負荷試験基盤を開発して 負荷試験を民主化した話 / Kubernetes x k6
sansan_randd
2
730
第13回 Data-Centric AI勉強会, 画像認識におけるData-centric AI
ksaito_osx
0
360
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
630
Gamification - CAS2011
davidbonilla
80
5.1k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
How to Ace a Technical Interview
jacobian
276
23k
Fireside Chat
paigeccino
34
3.2k
Designing for Performance
lara
604
68k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
The Language of Interfaces
destraynor
156
24k
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