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
180
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
AWS Emulator を評価する: 手元で動く AWS の API の仕組み、始め方、確かめ方
inariku
PRO
2
770
OCI Artifact による自動運転タクシーに向けた OTA 基盤の構築
biwashi
0
130
常時稼働エージェントの作り方(Claude公式ブログを資料化)
minorun365
PRO
16
28k
文書画像からの情報抽出を行うエージェントを、軌跡の正解データ無しで作れるか
sansantech
PRO
2
210
gradle-best-practices 使ってみた
mikanichinose
0
160
信頼性を犠牲にせず、PR マージからデプロイまで人の介入を完全にゼロにするまでの道のり
knwoop
2
720
CloudTrail を推したい
kazzpapa3
1
140
Kansai WordPress Meetup@大阪『ブロックテーマを作る上で知っておくべきこと
torounit
0
250
共通基盤の実装に Kotlin - Quarkus を選定した理由と採用したアーキテクチャ
nealle
1
140
速くなるほど、深く考える AI時代のQAに学ぶ
kobayashiyorimitsu
0
270
落とし穴にハマってわかった 開発生産性を測るときに気をつけたいこと
munakata
3
420
ラクスル会社紹介資料|エンジニア向け / RAKSUL Introduction for Engineers
raksulrecruiting
0
140
Featured
See All Featured
Technical Leadership for Architectural Decision Making
baasie
4
650
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Accessibility Awareness
sabderemane
1
310
Highjacked: Video Game Concept Design
rkendrick25
PRO
2
580
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
550
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2.1k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
1
310
The Cost Of JavaScript in 2023
addyosmani
56
10k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
590