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
2k
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
82
Preparing for the future of API Description Languages
kylef
0
98
Resilient API Design
kylef
1
320
Testing without Xcode - CMD+U 2016
kylef
0
240
End-to-end: Building a Web Service in Swift (MCE 2016)
kylef
2
480
Designing APIs for Humans - Write the Docs 2016
kylef
0
310
Embracing Change - MBLTDev 2015
kylef
3
670
Practical Declarative Programming (360 iDev 2015)
kylef
3
490
Building Resilient API Clients (SLUG)
kylef
2
13k
Other Decks in Technology
See All in Technology
Perl歴約10年のエンジニアがフルスタックTypeScriptに出会ってみた
papix
1
250
AIエージェント開発手法と業務導入のプラクティス
ykosaka
9
2.6k
ワールドカフェI /チューターを改良する / World Café I and Improving the Tutors
ks91
PRO
0
140
Microsoft の SSE の現在地
skmkzyk
0
270
OPENLOGI Company Profile
hr01
0
63k
30代からでも遅くない! 内製開発の世界に飛び込み、最前線で戦うLLMアプリ開発エンジニアになろう
minorun365
PRO
16
5k
より良い開発者体験を実現するために~開発初心者が感じた生成AIの可能性~
masakiokuda
0
230
意思決定を支える検索体験を目指してやってきたこと
hinatades
PRO
0
370
AIとSREで「今」できること
honmarkhunt
3
670
生成AIによるCloud Native基盤構築の可能性と実践的ガードレールの敷設について
nwiizo
7
1.4k
【Λ(らむだ)】最近のアプデ情報 / RPALT20250422
lambda
0
210
2025-04-14 Data & Analytics 井戸端会議 Multi tenant log platform with Iceberg
kamijin_fanta
0
160
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
68
11k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
178
53k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
136
33k
Code Reviewing Like a Champion
maltzj
523
40k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
19
1.2k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
5
590
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
35
2.7k
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