Slide 1

Slide 1 text

Building a Swift Web API and Application Together Kyle Fuller

Slide 2

Slide 2 text

Agenda —API Design —Building API in Swift —Deploying API in Swift in production

Slide 3

Slide 3 text

API Design

Slide 4

Slide 4 text

What makes a good API?

Slide 5

Slide 5 text

What makes a good API? —Decoupled from implementation details —Able to evolve without breaking existing clients

Slide 6

Slide 6 text

Example: Pagination

Slide 7

Slide 7 text

GET /posts

Slide 8

Slide 8 text

GET /posts{?page}

Slide 9

Slide 9 text

GET /posts?page=2

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

! How can we solve?

Slide 13

Slide 13 text

GET /posts{?before}

Slide 14

Slide 14 text

GET /posts?before=44

Slide 15

Slide 15 text

How do we introduce changes to our API?

Slide 16

Slide 16 text

Versioning APIs

Slide 17

Slide 17 text

/v1/posts{?page} /v2/posts{?before}

Slide 18

Slide 18 text

/posts{?page} /posts{?before}

Slide 19

Slide 19 text

API is coupled to implementation details

Slide 20

Slide 20 text

API is coupled to implementation details

Slide 21

Slide 21 text

What happens when you version an API?

Slide 22

Slide 22 text

What happens when you version an API?

Slide 23

Slide 23 text

How can we design the API without exposing implementation details?

Slide 24

Slide 24 text

REST Representational State Transfer

Slide 25

Slide 25 text

Anticipating change is one of the central themes of REST

Slide 26

Slide 26 text

Evolvability

Slide 27

Slide 27 text

Tight Coupling

Slide 28

Slide 28 text

"You can’t have evolvability if clients have their controls baked into their design at deployment"

Slide 29

Slide 29 text

"Controls have to be learned on the fly. That’s what hypermedia enables"

Slide 30

Slide 30 text

Hypermedia

Slide 31

Slide 31 text

Web Linking RFC 5988

Slide 32

Slide 32 text

GET /posts Link: ; rel="next", ; rel="last"

Slide 33

Slide 33 text

GET /posts?before=120 Link: ; rel="prev", ; rel="first"

Slide 34

Slide 34 text

WebLinking.swift https://github.com/kylef/WebLinking.swift

Slide 35

Slide 35 text

WebLinking: Checking for next link if let link = response.findLink(relation: "next") { print("We have a next link with the URI: \(link.uri).") }

Slide 36

Slide 36 text

WebLinking: Introspecting Available Links for link in response.links { print("Relation: \(link.relationType)") print("URI: \(link.uri)") }

Slide 37

Slide 37 text

application/hal+json

Slide 38

Slide 38 text

Blog Post { "title": "My First Blog Post", "body": "Lorem Ipsum" }

Slide 39

Slide 39 text

Blog Post (Next Link) { "title": "My First Blog Post", "body": "Lorem Ipsum", "_links": [ { "href": "/posts/2", "relation": "next" } ] }

Slide 40

Slide 40 text

Blog Post (Self) { "title": "My First Blog Post", "body": "Lorem Ipsum", "_links": [ { "href": "/posts/1", "relation": "self" }, { "href": "/posts/2", "relation": "next" } ] }

Slide 41

Slide 41 text

Blog Post (Comments) { "title": "My First Blog Post", "body": "Lorem Ipsum", "_links": [ { "href": "/posts/1", "relation": "self" }, { "href": "/posts/2", "relation": "next" }, { "href": "/posts/1/comments", "relation": "comments" }, ] }

Slide 42

Slide 42 text

Blog Post (Embedded Comments) { "_embed": { "comments": [ { "author": "Kyle", "body": "That's a really interesting post!" "_links": [ { "href": "/posts/1/comments/1", "relation": "self" } ] } ] } }

Slide 43

Slide 43 text

application/ vnd.siren+json

Slide 44

Slide 44 text

Delete Post { "properties": { "title": "My First Blog Post", "body": "Lorem Ipsum", }, "actions": [ { "name": "delete", "method": "DELETE", "href": "/posts/1" } ] }

Slide 45

Slide 45 text

Delete Post { "properties": { "title": "My First Blog Post", "body": "Lorem Ipsum", }, "actions": [ { "name": "delete", "method": "DELETE", "href": "/posts/1" } ] }

Slide 46

Slide 46 text

Create Comment { "properties": { "title": "My First Blog Post", "body": "Lorem Ipsum", }, "actions": [ { "name": "comment", "method": "POST", "href": "/posts/1/comments", "fields": [ { "name": "author", "type": "string" }, { "name": "message", "type": "string" } ] } ] }

Slide 47

Slide 47 text

Create Comment (Logged in) { "properties": { "title": "My First Blog Post", "body": "Lorem Ipsum", }, "actions": [ { "name": "comment", "method": "POST", "href": "/posts/1/comments", "fields": [ { "name": "message", "type": "string" } ] } ] }

Slide 48

Slide 48 text

Hypermedia —Remove implementation details from interface —Keep business logic on back-end, not front-end

Slide 49

Slide 49 text

Building an API in Swift

Slide 50

Slide 50 text

Web Frameworks

Slide 51

Slide 51 text

Web Frameworks —Frank —IBM Kitura —Vapor

Slide 52

Slide 52 text

Frank vs Kitura vs Vapor

Slide 53

Slide 53 text

Frank get("users", *) { (request, username: String) in return "Hello \(username)" }

Slide 54

Slide 54 text

Server APIs Working Group

Slide 55

Slide 55 text

Server APIs Working Group

Slide 56

Slide 56 text

Useful Tools —Templating Languages —Stencil —Data Persistence —Redis (Redbird) —PostgreSQL

Slide 57

Slide 57 text

Testing

Slide 58

Slide 58 text

XCTest class PersonTests: XCTestCase { let person = Person(name: "Kyle") func testPersonName() { XCTAssertEqual(person.name, "Kyle") } func testPersonDescription() { XCTAssertEqual(person.description, "Kyle") } }

Slide 59

Slide 59 text

extension PersonTests: XCTestCaseProvider { var allTests : [(String, () throws -> Void)] { return [ ("testPersonName", testPersonName), ("testPersonDescription", testPersonDescription), ] } } XCTMain([ PersonTests(), ])

Slide 60

Slide 60 text

Dredd https://github.com/apiaryio/dredd

Slide 61

Slide 61 text

API Blueprint # GET /hello + Response 200 (application/json) { "name": "Kyle" }

Slide 62

Slide 62 text

Dredd Testing $ dredd \ apidescription.apib \ https://localhost:8080 ✔ GET /hello ✔ API Matches API Description

Slide 63

Slide 63 text

Deployment

Slide 64

Slide 64 text

https://github.com/kylef/heroku-buildpack-swift

Slide 65

Slide 65 text

$ cat Package.swift import PackageDescription let package = Package( name: "Hello", dependencies: [ .Package(url: "https://github.com/nestproject/Frank.git", majorVersion: 0, minor: 3), ] )

Slide 66

Slide 66 text

$ cat Sources/main.swift import Frank get { _ in return "Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }

Slide 67

Slide 67 text

$ cat Sources/main.swift import Frank get { _ in return "Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }

Slide 68

Slide 68 text

$ cat .swift-version 3.0.2

Slide 69

Slide 69 text

$ cat .swift-version 3.0.2

Slide 70

Slide 70 text

$ swift build $ .build/debug/Hello [INFO] Listening at http://0.0.0.0:8000 (48827) [INFO] Booting worker process with pid: 48828

Slide 71

Slide 71 text

$ cat Procfile web: Hello

Slide 72

Slide 72 text

$ heroku create --buildpack https://github.com/kylef/heroku-buildpack-swift.git $ git push heroku master remote: -----> Swift app detected remote: -----> Installing 3.0.2 remote: -----> Installing clang-3.7.0 remote: -----> Building Package remote: -----> Copying binaries to 'bin'

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Manual Deployment

Slide 77

Slide 77 text

Monitoring

Slide 78

Slide 78 text

Logging

Slide 79

Slide 79 text

print("ERROR: Connection to database failed \(error)")

Slide 80

Slide 80 text

Papertrail

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Conclusion —API Design —Swift Web Services —Tools & Frameworks —Testing —Deployment —Monitoring

Slide 84

Slide 84 text

Conclusion —API Design —Swift Web Services —Tools & Frameworks —Testing —Deployment —Monitoring

Slide 85

Slide 85 text

kylefuller https://fuller.li/talks