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
3
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
370
Functional Swift - Brooklyn
chriseidhof
3
1.1k
Functional Swift - SF
chriseidhof
7
26k
Functional Swift
chriseidhof
6
1.2k
Functional Swift
chriseidhof
1
130
Functional Programming in Swift
chriseidhof
40
14k
Lighter View Controllers
chriseidhof
4
180
Parsing with Blocks
chriseidhof
2
220
Practical Concurrent Programming
chriseidhof
4
250
Other Decks in Technology
See All in Technology
大規模なアジャイル開発の現場と技術負債 / Technical Debt
yoshiitaka
21
4.1k
KubeCon EU 2024 : Knative Maintainers Session
salaboy
0
380
プッシュ型子育てサービスを、先行プロジェクト実施自治体において開始します
govtechtokyo
0
290
Vos logs méritent mieux que la config par défaut
lyrixx
2
410
今さら聞けない!? AWSの生成AIサービス Amazon Bedrock入門!
minorun365
PRO
11
2.7k
HoneycombとOpenTelemetryでオブザーバビリティに入門してみる
sumiren
2
160
あらゆる商品を扱う商品データベースを再設計した話 / product db re-architecture
rince
8
3.8k
KubeCon EU: Unlocking new Platform Experiences with Open Interfaces
salaboy
1
380
社内共通ルールを値オブジェクトにして社内ライブラリとして運用してみた話
leveragestech
2
1.2k
2023 Japan AWS Jr.Championsに選出されての振り返りとこれから
hiropy877
1
130
Building a RAG app to chat with your data (on Azure)
pamelafox
0
130
Castor - Symfony Live 2024 - Paris
lyrixx
1
110
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
215
8.5k
BBQ
matthewcrist
78
8.7k
The Cost Of JavaScript in 2023
addyosmani
13
3.7k
Happy Clients
brianwarren
91
6.3k
Why You Should Never Use an ORM
jnunemaker
PRO
50
8.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
11
1.4k
Pencils Down: Stop Designing & Start Developing
hursman
115
11k
We Have a Design System, Now What?
morganepeng
42
6.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
272
12k
The Invisible Customer
myddelton
114
12k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
19
1.6k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
240
1.2M
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