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
Building a Swift Web API and Application Together
Search
Kyle Fuller
March 02, 2017
Technology
2
1.8k
Building a Swift Web API and Application Together
Kyle Fuller
March 02, 2017
Tweet
Share
More Decks by Kyle Fuller
See All by Kyle Fuller
Design APIs and deliver what you promised
kylef
0
64
Preparing for the future of API Description Languages
kylef
0
83
Resilient API Design
kylef
1
280
Testing without Xcode - CMD+U 2016
kylef
0
220
End-to-end: Building a Web Service in Swift (MCE 2016)
kylef
2
450
Designing APIs for Humans - Write the Docs 2016
kylef
0
280
Embracing Change - MBLTDev 2015
kylef
3
630
Practical Declarative Programming (360 iDev 2015)
kylef
3
450
Building Resilient API Clients (SLUG)
kylef
2
13k
Other Decks in Technology
See All in Technology
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.6k
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.6k
ガバメントクラウド先行事業中間報告を読み解く
sugiim
1
1.4k
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
ガチ勢によるPipeCD運用大全〜滑らかなCI/CDを添えて〜 / ai-pipecd-encyclopedia
cyberagentdevelopers
PRO
3
210
Datachain会社紹介資料(2024年11月) / Company Deck
datachain
3
16k
新卒1年目が挑む!生成AI × マルチエージェントで実現する次世代オンボーディング / operation-ai-onboarding
cyberagentdevelopers
PRO
1
170
「視座」の上げ方が成人発達理論にわかりやすくまとまってた / think_ perspective_hidden_dimensions
shuzon
2
4.8k
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
コンテンツを支える 若手ゲームクリエイターの アートディレクションの事例紹介 / cagamefi-game
cyberagentdevelopers
PRO
1
130
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
480
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
740
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Testing 201, or: Great Expectations
jmmastey
38
7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Embracing the Ebb and Flow
colly
84
4.4k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Building Adaptive Systems
keathley
38
2.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Transcript
Building a Swift Web API and Application Together Kyle Fuller
Agenda —API Design —Building API in Swift —Deploying API in
Swift in production
API Design
What makes a good API?
What makes a good API? —Decoupled from implementation details —Able
to evolve without breaking existing clients
Example: Pagination
GET /posts
GET /posts{?page}
GET /posts?page=2
None
None
! How can we solve?
GET /posts{?before}
GET /posts?before=44
How do we introduce changes to our API?
Versioning APIs
/v1/posts{?page} /v2/posts{?before}
/posts{?page} /posts{?before}
API is coupled to implementation details
API is coupled to implementation details
What happens when you version an API?
What happens when you version an API?
How can we design the API without exposing implementation details?
REST Representational State Transfer
Anticipating change is one of the central themes of REST
Evolvability
Tight Coupling
"You can’t have evolvability if clients have their controls baked
into their design at deployment"
"Controls have to be learned on the fly. That’s what
hypermedia enables"
Hypermedia
Web Linking RFC 5988
GET /posts Link: </posts?before=30>; rel="next", </posts?before=120>; rel="last"
GET /posts?before=120 Link: </posts?before=90>; rel="prev", </posts>; rel="first"
WebLinking.swift https://github.com/kylef/WebLinking.swift
WebLinking: Checking for next link if let link = response.findLink(relation:
"next") { print("We have a next link with the URI: \(link.uri).") }
WebLinking: Introspecting Available Links for link in response.links { print("Relation:
\(link.relationType)") print("URI: \(link.uri)") }
application/hal+json
Blog Post { "title": "My First Blog Post", "body": "Lorem
Ipsum" }
Blog Post (Next Link) { "title": "My First Blog Post",
"body": "Lorem Ipsum", "_links": [ { "href": "/posts/2", "relation": "next" } ] }
Blog Post (Self) { "title": "My First Blog Post", "body":
"Lorem Ipsum", "_links": [ { "href": "/posts/1", "relation": "self" }, { "href": "/posts/2", "relation": "next" } ] }
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" }, ] }
Blog Post (Embedded Comments) { "_embed": { "comments": [ {
"author": "Kyle", "body": "That's a really interesting post!" "_links": [ { "href": "/posts/1/comments/1", "relation": "self" } ] } ] } }
application/ vnd.siren+json
Delete Post { "properties": { "title": "My First Blog Post",
"body": "Lorem Ipsum", }, "actions": [ { "name": "delete", "method": "DELETE", "href": "/posts/1" } ] }
Delete Post { "properties": { "title": "My First Blog Post",
"body": "Lorem Ipsum", }, "actions": [ { "name": "delete", "method": "DELETE", "href": "/posts/1" } ] }
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" } ] } ] }
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" } ] } ] }
Hypermedia —Remove implementation details from interface —Keep business logic on
back-end, not front-end
Building an API in Swift
Web Frameworks
Web Frameworks —Frank —IBM Kitura —Vapor
Frank vs Kitura vs Vapor
Frank get("users", *) { (request, username: String) in return "Hello
\(username)" }
Server APIs Working Group
Server APIs Working Group
Useful Tools —Templating Languages —Stencil —Data Persistence —Redis (Redbird) —PostgreSQL
Testing
XCTest class PersonTests: XCTestCase { let person = Person(name: "Kyle")
func testPersonName() { XCTAssertEqual(person.name, "Kyle") } func testPersonDescription() { XCTAssertEqual(person.description, "Kyle") } }
extension PersonTests: XCTestCaseProvider { var allTests : [(String, () throws
-> Void)] { return [ ("testPersonName", testPersonName), ("testPersonDescription", testPersonDescription), ] } } XCTMain([ PersonTests(), ])
Dredd https://github.com/apiaryio/dredd
API Blueprint # GET /hello + Response 200 (application/json) {
"name": "Kyle" }
Dredd Testing $ dredd \ apidescription.apib \ https://localhost:8080 ✔ GET
/hello ✔ API Matches API Description
Deployment
https://github.com/kylef/heroku-buildpack-swift
$ cat Package.swift import PackageDescription let package = Package( name:
"Hello", dependencies: [ .Package(url: "https://github.com/nestproject/Frank.git", majorVersion: 0, minor: 3), ] )
$ cat Sources/main.swift import Frank get { _ in return
"Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }
$ cat Sources/main.swift import Frank get { _ in return
"Hello World" } get(*) { (_, username: String) in return "Hello \(username)" }
$ cat .swift-version 3.0.2
$ cat .swift-version 3.0.2
$ swift build $ .build/debug/Hello [INFO] Listening at http://0.0.0.0:8000 (48827)
[INFO] Booting worker process with pid: 48828
$ cat Procfile web: Hello
$ 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'
None
None
None
Manual Deployment
Monitoring
Logging
print("ERROR: Connection to database failed \(error)")
Papertrail
None
None
Conclusion —API Design —Swift Web Services —Tools & Frameworks —Testing
—Deployment —Monitoring
Conclusion —API Design —Swift Web Services —Tools & Frameworks —Testing
—Deployment —Monitoring
kylefuller https://fuller.li/talks