Slide 1

Slide 1 text

REACTING TO CHANGE ! KYLE FULLER

Slide 2

Slide 2 text

BUILDING better API CLIENTS

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

DOZENS of APPS

Slide 6

Slide 6 text

HTTP / APIS

Slide 7

Slide 7 text

THERE’S AN API

Slide 8

Slide 8 text

COMPONENTS OF AN API CLIENT

Slide 9

Slide 9 text

perform(.RetrieveArticles) { result in switch result { case .Success(let representor): /* We've got some articles */ case .Error(let error): /* We hit an error */ } }

Slide 10

Slide 10 text

FOUNDATION

Slide 11

Slide 11 text

NSURLSession OR NSURLConnection

Slide 12

Slide 12 text

NSURLRequest

Slide 13

Slide 13 text

NSURLHTTPResponse

Slide 14

Slide 14 text

SIMPLE...

Slide 15

Slide 15 text

HTTP SEMANTICS

Slide 16

Slide 16 text

SERIALISATION (JSON)

Slide 17

Slide 17 text

CONVERT to VALUE-MODELS

Slide 18

Slide 18 text

struct Article { let title:String let body:String let author:Author let image:[NSURL] } struct Author { let name:String let twitter:String }

Slide 19

Slide 19 text

articles[0]["author"]["name"]

Slide 20

Slide 20 text

"/articles"

Slide 21

Slide 21 text

"name"

Slide 22

Slide 22 text

"nme"

Slide 23

Slide 23 text

- age + birthday

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

MAPPING

Slide 27

Slide 27 text

HTTP JSON MODEL MAPPING TESTS (STUBS & FIXTURES) DOMAIN LOGIC?

Slide 28

Slide 28 text

$ cloc **Model**.{h,m} **API**.{h,m} 95 text files. 83 unique files. 0 files ignored. ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Objective C 51 1420 499 5598 C/C++ Header 32 258 480 479 ------------------------------------------------------------------------------- SUM: 83 1678 979 6077 -------------------------------------------------------------------------------

Slide 29

Slide 29 text

6077 LINES OF CODE

Slide 30

Slide 30 text

ANDROID

Slide 31

Slide 31 text

15,000 LINES OF CODE

Slide 32

Slide 32 text

WINDOWS PHONE

Slide 33

Slide 33 text

20,000 LINES OF CODE

Slide 34

Slide 34 text

...

Slide 35

Slide 35 text

30,000 LINES OF CODE

Slide 36

Slide 36 text

BUILDING an API CLIENT

Slide 37

Slide 37 text

DOMAIN-SPECIFIC

Slide 38

Slide 38 text

INTEGRATED

Slide 39

Slide 39 text

MIXING PROTOCOL SEMANTICS

Slide 40

Slide 40 text

WHAT DOES THE API LOOK LIKE?

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

GETTING STARTED

Slide 45

Slide 45 text

TESTS

Slide 46

Slide 46 text

MOCK

Slide 47

Slide 47 text

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return ([request.URL.host isEqualToString:@"trunk.cocoapods.org"] && [request.URL.path isEqualToString:@"/api/v1/sessions"]); } withStubResponse:^ OHHTTPStubsResponse *(NSURLRequest *request) { NSString *fixture = OHPathForFileInBundle(@"sessions.json", nil); NSDictionary *headers = @{ @"Content-Type": @"application/json" }; return [OHHTTPStubsResponse responseWithFileAtPath:fixture statusCode:200 headers:headers]; }];

Slide 48

Slide 48 text

{ "created_at": "2014-04-21 23:08:00 +0200", "email": "[email protected]", "name": "Kyle Fuller", "sessions": [ { "created_at": "2014-05-04 15:20:19 +0200", "valid_until": "2014-09-09 15:20:19 +0200", "verified": false }, { "created_at": "2014-05-04 15:20:20 +0200", "valid_until": "2014-09-09 15:20:20 +0200", "verified": true } ] }

Slide 49

Slide 49 text

AND REPEAT

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

CAN’T WE USE THIS?

Slide 52

Slide 52 text

UNDERSTANDABLE by machines

Slide 53

Slide 53 text

POWERING YOUR TESTS with API BLUEPRINT

Slide 54

Slide 54 text

pod `CCLRequestReplay`

Slide 55

Slide 55 text

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return ([request.URL.host isEqualToString:@"trunk.cocoapods.org"] && [request.URL.path isEqualToString:@"/api/v1/sessions"]); } withStubResponse:^ OHHTTPStubsResponse *(NSURLRequest *request) { NSString *fixture = OHPathForFileInBundle(@"sessions.json", nil); NSDictionary *headers = @{ @"Content-Type": @"application/json" }; return [OHHTTPStubsResponse responseWithFileAtPath:fixture statusCode:200 headers:headers]; }]; [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return ([request.URL.host isEqualToString:@"trunk.cocoapods.org"] && [request.URL.path isEqualToString:@"/api/v1/pods"]); } withStubResponse:^ OHHTTPStubsResponse *(NSURLRequest *request) { NSString *fixture = OHPathForFileInBundle(@"pods.json", nil); NSDictionary *headers = @{ @"Content-Type": @"application/json" }; return [OHHTTPStubsResponse responseWithFileAtPath:fixture statusCode:200 headers:headers]; }]; [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { return ([request.URL.host isEqualToString:@"trunk.cocoapods.org"] && [request.URL.path isEqualToString:@"/api/v1/pod/../owners"]); } withStubResponse:^ OHHTTPStubsResponse *(NSURLRequest *request) { NSString *fixture = OHPathForFileInBundle(@"pod-querykit-owners.json", nil); NSDictionary *headers = @{ @"Content-Type": @"application/json" }; return [OHHTTPStubsResponse responseWithFileAtPath:fixture statusCode:200 headers:headers]; }]; ...

Slide 56

Slide 56 text

{ "created_at": "2014-04-21 23:08:00 +0200", "email": "[email protected]", "name": "Kyle Fuller", "sessions": [ { "created_at": "2014-05-04 15:20:19 +0200", "valid_until": "2014-09-09 15:20:19 +0200", "verified": false }, { "created_at": "2014-05-04 15:20:20 +0200", "valid_until": "2014-09-09 15:20:20 +0200", "verified": true } ] } { "name": "QueryKit", "version": "0.8.3", "authors": { "Kyle Fuller": "[email protected]" }, "social_media_url": "http://twitter.com/kylefuller", "source": { "git": "https://github.com/QueryKit/QueryKit.git", "tag": "0.8.3" }, "source_files": [ "QueryKit/*.{h}", "QueryKit/ObjectiveC/*.{h,m}" ], "requires_arc": true } ...

Slide 57

Slide 57 text

let URL = NSBundle.mainBundle().URIForResource("palaver", withExtension:"apib.json") requestReplayManager.addRecordings(fromBlueprintURL:URL, error:nil)

Slide 58

Slide 58 text

GET /sessions

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

EMBRACE CHANGE

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

WE MADE A MISTAKE.

Slide 63

Slide 63 text

WE WANT TO CHANGE... ENDPOINT URI.

Slide 64

Slide 64 text

NEGOTIATE AT RUN-TIME

Slide 65

Slide 65 text

WE WANT TO CHANGE... PAGINATION LOGIC.

Slide 66

Slide 66 text

FOLLOWING LINKS

Slide 67

Slide 67 text

TRANSITION BETWEEN STATES

Slide 68

Slide 68 text

"Paging is achieved with the published_before parameter."

Slide 69

Slide 69 text

published_before

Slide 70

Slide 70 text

[ { "published_date": "2013-04-30T16:05:45+00:00", ... } ]

Slide 71

Slide 71 text

GET /articles?published_before=2013-04-30T16:05:45+00:00

Slide 72

Slide 72 text

SERVER LOGIC

Slide 73

Slide 73 text

Next Page =

Slide 74

Slide 74 text

Prevent BREAKING CHANGES

Slide 75

Slide 75 text

BUILDING BETTER CLIENTS from THE API

Slide 76

Slide 76 text

FLEXIBILITY

Slide 77

Slide 77 text

CASCADED REQUESTS

Slide 78

Slide 78 text

GET /categories GET /authors GET /articles

Slide 79

Slide 79 text

BANDWIDTH

Slide 80

Slide 80 text

LARGE DATA

Slide 81

Slide 81 text

$ curl -I http://api.something.com/articles HTTP/1.1 200 OK Content-Type: application/json Content-Length: 12524998

Slide 82

Slide 82 text

BREAK IT UP

Slide 83

Slide 83 text

PAGINATION

Slide 84

Slide 84 text

/list VS /list/detail

Slide 85

Slide 85 text

USE Last-Modified

Slide 86

Slide 86 text

USE Cache-Control

Slide 87

Slide 87 text

USE STANDARDS

Slide 88

Slide 88 text

LET’S BUILD BETTER APIS TOGETHER

Slide 89

Slide 89 text

KYLE FULLER