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
OCI技術資料 : コンピュート・サービス 概要
ocise
4
54k
マルチロールEMが実践する「組織のレジリエンス」を高めるための組織構造と人材配置戦略
coconala_engineer
3
710
元エンジニアPdM、IDEが恋しすぎてCursorに全業務を集約したら、スライド作成まで爆速になった話
doiko123
1
590
最強のAIエージェントを諦めたら品質が上がった話 / how quality improved after giving up on the strongest AI agent
kt2mikan
0
160
スクリプトの先へ!AIエージェントと組み合わせる モバイルE2Eテスト
error96num
0
150
IBM Bobを使って、PostgreSQLのToDoアプリをDb2へ変換してみよう/202603_Dojo_Bob
mayumihirano
1
310
プロジェクトマネジメントをチームに宿す -ゼロからはじめるチームプロジェクトマネジメントは活動1年未満のチームの教科書です- / 20260304 Shigeki Morizane
shift_evolve
PRO
1
250
「ストレッチゾーンに挑戦し続ける」ことって難しくないですか? メンバーの持続的成長を支えるEMの環境設計
sansantech
PRO
3
640
Kubernetesにおける推論基盤
ry
1
300
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
8
7.2k
新職業『オーケストレーター』誕生 — エージェント10体を同時に回すAgentOps
gunta
4
1.8k
EMからVPoEを経てCTOへ:マネジメントキャリアパスにおける葛藤と成長
kakehashi
PRO
9
1.7k
Featured
See All Featured
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
330
A designer walks into a library…
pauljervisheath
210
24k
A Soul's Torment
seathinner
5
2.4k
How to train your dragon (web standard)
notwaldorf
97
6.6k
Mind Mapping
helmedeiros
PRO
1
120
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
460
The Cost Of JavaScript in 2023
addyosmani
55
9.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
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