SlideShare a Scribd company logo
Build Great Networked APIs
with Swift, OpenAPI, and gRPC
Tim Burks, Google
Swift Cloud Workshop No. 2
September 30, 2017
What do we do?
WE WRITE CODE
Build Great Networked APIs with Swift, OpenAPI, and gRPC
What do we want?
QUALTIY!
Protocol Buffers
a language-neutral, platform-neutral, extensible mechanism for serializing structured data.
Interface Builder for Data
message Person {
string name = 1;
int32 id = 2;
string email = 3;
message PhoneNumber {
string number = 1;
}
repeated PhoneNumber phone = 4;
}
Interface Builder: Developers specify
their interfaces using a special tool,
tooling compiles and integrates that into
their apps.
Protocol Buffers: Developers specify
their data structures using a special
language, tooling compiles and
integrates that into their apps.
Google API Discovery Format
Discovery Format Code Generators
github.com/google/google-api-go-client
github.com/google/google-api-python-client
Idiosyncracies and Lessons
Toolkit and the next generation of Google APIs
So far, so good.
REST (Discovery Format)
● 232 API descriptions at https://www.googleapis.com/discovery/v1/apis
● 10 generators listed at https://developers.google.com/api-client-library/
gRPC (Toolkit)
● 30+ gRPC-based APIs
● 7 target languages
OpenAPI 3.0
Industry standard format for describing for REST APIs
Originally designed for documentation, now with many other applications: API
Authoring, Validation, Documentation, Analysis, Search, Testing, Mocking,
Management, Code Generation
Consensus
OpenAPI-based code generators
● swagger-codegen (Open source, Smartbear + community)
○ 70+ targets
○ First commit: July 6, 2011
○ Used by Lyft and Square to generate SDKs
● AutoRest (Open source, Microsoft)
● oas-nodegen (Open source, Capital One)
● APIMatic (Proprietary, APIMatic)
more?
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Problems we’ve seen so far in open source code generators
● Missing or weak build system integration.
● Invalid service addresses (error in OpenAPI description).
● No auth.
● No documentation (not even a README!).
● No samples.
● No test harness.
● No assurance that the called service even works.
● Ugly generated code.
○ Machine-generated operation names.
○ Machine-generated struct names.
● Unwanted dependencies.
swagger-codegen often isn’t used “out of the box”:
“Generating client libraries involves customizing the provided language-specific
templates…
The amount of modification each template needs varies by language and we’re
looking forward to working with the Swagger Codegen community to share our
refinements.” Val Polouchkine, Lyft
“...Swagger Codegen is a pretty active project. If you don’t check in your
templates, things are gonna break unexpectedly because Swagger Codegen just
uses the latest and greatest templates that are out there. So if you don’t sort of
manually make sure that those things work, you’re gonna have an issue there.”
Tristan Sokol, Square
Code generation pipeline
API Description
Generatable
API Description
Language-
specific model
Generated
API support
code
render idiomatic API
support code according
to user preferences
(re. build systems,
dependencies)
verify model,
define structures and
entry points,
name everything
filter language-
specific reserved words,
(optionally) define file
structure for generated code
typical monolithic
code generation pipeline
API Description
(OpenAPI)
Generatable
API Description
(internal data
structures)
Language-
specific model
(internal data
structures)
Generated
API support
code
All-in-one repo and package
Problems with monolithic code generators
● Long build times: changing one target requires rebuilding everything.
● Long test times: new builds must be tested for every target language.
● For stability, teams may prefer to archive their own generator builds.
● Forks will abound.
● Quality is uneven.
● Versioning is hard.
● Complexity and potentially unfamiliar build systems deter contributors.
protoc
code generation pipeline
API Description
(.proto)
Generatable
API Description
(binary
FileDescriptors)
Language-
specific model
(internal data
structures)
Generated
API support
code
language target-
specific plugins
protoc
protoc plugin definition
Why does protoc have a plug-in architecture?
● Fast build times: changing one target only requires rebuilding its plugin.
● Fast test times: new builds need only be tested for the affected targets.
● For stability, teams can archive their own protoc and plugin builds.
● New plugins can abound.
● Separately-maintained plugins can offer different maturity levels.
● Separately-maintained plugins can be appropriately versioned.
● Separately-maintained plugins can be in languages that contributors
prefer.
What’s the catch?
Plugins require a well-defined interchange format.
Fortunately, we have two great tools for that.
gnostic
gnostic processed and
verified protobuf
representation of
OpenAPI description
protoc +
pluginsOpenAPI
.proto
reusable data structures
and reader for protobuf
OpenAPI descriptions
gnostic apps
and plugins
OpenAPI
description
gnostic-
generator
OpenAPI
.proto and
compiler
code
OpenAPI
JSON
schema
Kubernetes OpenAPI: .json vs .pb
Format Size Deserialization time Download time
(at 80 Mbps)
Json 1653 KB >500 ms 165.3 ms
Proto binary 914 KB 9.3 ms 91.4 ms
Proto binary compressed 96 KB 13.5 ms 1.3 ms
Source: mehdy@google.com
gnostic
code generation pipeline
API Description
(OpenAPI v3)
Generatable
API Description
(Normalized/
Annotated
OpenAPI v3?)
Language-
specific model
(Normalized/
Annotated
OpenAPI v3?)
Generated
API support
code
target-specific
plugins
gnostic +
linter
let anyone write code generation plugins...
● in their own repositories
● with their own versioning
● in whatever implementation language they choose
API code generation is a community problem that needs community-based
solutions...
Code generators should
belong to their communities.
gRPC
@grpcio
RPC: Use Cases
Direct RPCs:
Microservices
RPCs to
access APIs
Google APIs
OSS APIs
Mobile
Web
Desktop
RPCs
Datacenters Cloud
Service 1
Service 2
Service 3
Service 4
Containers
@grpcio
What is gRPC?
● HTTP/2 based RPC framework developed by Google
● Open, Multiplatform, Secure, Performant
Multiplatform
● Idiomatic APIs in popular languages (C++, Go, Java, Python, Node.js, C#, Ruby, PHP)
● Supports mobile devices (Android Java, iOS Obj-C, Swift)
● Linux, Windows, Mac OS X
● (web browser support in development)
OpenSource
● developed fully in open on GitHub: https://github.com/grpc/
@grpcio
● Builds on Apple’s swift-protobuf and grpc-core.
● Includes:
○ gRPC framework (C and Swift components)
○ generated code surface
○ protoc plugin for code generation
● Full-service gRPC:
○ All four gRPC API styles are supported.
○ gRPC framework supports both clients and servers.
○ Plugin generates client and server code in separate files.
○ Testing on MacOS and Ubuntu.
● Audiences: client and server developers, inside/outside Google
gRPC for Swift (https://github.com/grpc/grpc-swift)
@grpcio
package echo;
service Echo {
// Immediately returns an echo of a request.
rpc Get(EchoRequest) returns (EchoResponse) {}
// Splits a request into words and returns each word in a stream of messages.
rpc Expand(EchoRequest) returns (stream EchoResponse) {}
// Collects a stream of messages and returns them concatenated when the caller closes.
rpc Collect(stream EchoRequest) returns (EchoResponse) {}
// Streams back messages as they are received in an input stream.
rpc Update(stream EchoRequest) returns (stream EchoResponse) {}
}
message EchoRequest {
// The text of a message to be echoed.
string text = 1;
}
message EchoResponse {
// The text of an echo response.
string text = 1;
}
gRPC Swift sample service
@grpcio
/// To build a server, implement a class that conforms to this protocol.
public protocol Echo_EchoProvider {
func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
func collect(session : Echo_EchoCollectSession) throws
func update(session : Echo_EchoUpdateSession) throws
}
gRPC Swift server protocol
@grpcio
// get returns requests as they were received.
func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse {
return Echo_EchoResponse(text:"Swift echo get: " + request.text)
}
...
// update streams back messages as they are received in an input stream.
func update(session : Echo_EchoUpdateSession) throws -> Void {
while true {
do {
let request = try session.Receive()
try session.Send(Echo_EchoResponse(text:"Swift echo update: (request.text)"))
} catch Echo_EchoServerError.endOfStream {
break
}
}
try session.Close()
}
}
gRPC Swift server sample
@grpcio
gRPC Swift unary client sample
// Unary
if client == "get" {
var requestMessage = Echo_EchoRequest(text:message)
let responseMessage = try service.get(requestMessage) // blocking
print("get received: " + responseMessage.text)
}
@grpcio
gRPC Swift bidirectional streaming client sample (1/2)
// Bidirectional streaming
if client == "update" {
let sem = DispatchSemaphore(value: 0)
let updateCall = try service.update() // blocking
DispatchQueue.global().async {
while true {
do {
let responseMessage = try updateCall.Receive() // blocking
print("Received: (responseMessage.text)")
} catch Echo_EchoClientError.endOfStream {
sem.signal()
break
}
}
}
...
@grpcio
gRPC Swift bidirectional streaming client sample (2/2)
...
let parts = message.components(separatedBy:" ")
for part in parts {
let requestMessage = Echo_EchoRequest(text:part)
try updateCall.Send(requestMessage)
sleep(1)
}
try updateCall.CloseSend()
// Wait for the call to complete.
sem.wait()
}
Build Great Networked APIs with Swift, OpenAPI, and gRPC
gRPC-Swift TODO
● Build system integration
○ Package Manager
○ Cocoapods?
○ Carthage?
● gRPC interoperability tests
● Samples that wrap Google APIs
○ Google Cloud Speech API
○ Google Datastore API
github.com/grpc/grpc-swift/issues
More gRPC Information
Website: http://grpc.io
Sources: https://github.com/grpc/grpc
Mailing list: https://groups.google.com/d/forum/grpc-io
Ecosystem: https://github.com/grpc-ecosystem
github.com/googleapis/gnostic
plugins for OpenAPI-based code generation
github.com/grpc/grpc-swift
Fast streaming APIs in Swift
github.com/google/auth-library-swift
OAuth support for Google Cloud
http://twitter.com/timburks
timburks@google.com

More Related Content

What's hot (20)

PDF
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Md.Zahidur Rahman
 
PDF
gRPC: The Story of Microservices at Square
Apigee | Google Cloud
 
ODP
IDE as a Front-end and Fast time-to-market language support in Eclipse IDE re...
Mickael Istria
 
PDF
Dependency management in golang
Ramit Surana
 
PDF
Understanding how concurrency work in os
GenchiLu1
 
PPTX
Modern webservices using gRPC and Protocol Buffers in Golang
OmidHojabri1
 
PPTX
Web technology slideshare
GuruAbirami2
 
PDF
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Jung Kim
 
PPTX
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
Maarten Balliauw
 
PPTX
C compilation process
RajKumar Rampelli
 
PDF
Clang Analyzer Tool Review
Doug Schuster
 
PPT
Communication between Java and Python
Andreas Schreiber
 
PDF
Thesis - LLVM toolchain support as a plug-in for Eclipse CDT
TuononenP
 
PPTX
How to integrate python into a scala stack
Fliptop
 
PDF
p4alu: Arithmetic Logic Unit in P4
Kentaro Ebisawa
 
PDF
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
smarru
 
PDF
Engineering Efficiency in LINE
Huy Do
 
PPT
What's New in Groovy 1.6?
Guillaume Laforge
 
PDF
Swift2 smalltalk osxdev
Jung Kim
 
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Md.Zahidur Rahman
 
gRPC: The Story of Microservices at Square
Apigee | Google Cloud
 
IDE as a Front-end and Fast time-to-market language support in Eclipse IDE re...
Mickael Istria
 
Dependency management in golang
Ramit Surana
 
Understanding how concurrency work in os
GenchiLu1
 
Modern webservices using gRPC and Protocol Buffers in Golang
OmidHojabri1
 
Web technology slideshare
GuruAbirami2
 
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Jung Kim
 
Microservices for building an IDE – The innards of JetBrains Rider - TechDays...
Maarten Balliauw
 
C compilation process
RajKumar Rampelli
 
Clang Analyzer Tool Review
Doug Schuster
 
Communication between Java and Python
Andreas Schreiber
 
Thesis - LLVM toolchain support as a plug-in for Eclipse CDT
TuononenP
 
How to integrate python into a scala stack
Fliptop
 
p4alu: Arithmetic Logic Unit in P4
Kentaro Ebisawa
 
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
smarru
 
Engineering Efficiency in LINE
Huy Do
 
What's New in Groovy 1.6?
Guillaume Laforge
 
Swift2 smalltalk osxdev
Jung Kim
 

Similar to Build Great Networked APIs with Swift, OpenAPI, and gRPC (20)

PDF
OpenAPI and gRPC Side by-Side
Tim Burks
 
PDF
LF_APIStrat17_OpenAPI and gRPC Side-by-Side
LF_APIStrat
 
PDF
Creating Great REST and gRPC API Experiences (in Swift)
Tim Burks
 
PDF
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays
 
PDF
Power-up services with gRPC
The Software House
 
PPTX
What I learned about APIs in my first year at Google
Tim Burks
 
PDF
Cloud Native API Design and Management
AllBits BVBA (freelancer)
 
PDF
Building REST APIs using gRPC and Go
Alvaro Viebrantz
 
PDF
Enforcing API Design Rules for High Quality Code Generation
Tim Burks
 
PDF
Implementing OpenAPI and GraphQL services with gRPC
Tim Burks
 
PPTX
Building your First gRPC Service
Jessie Barnett
 
PDF
Taming Cloud APIs with Swift
Tim Burks
 
PDF
gRPC Overview
Varun Talwar
 
PDF
Microservices in Go_Dessi_Massimiliano_Codemotion_2017_Rome
Massimiliano Dessì
 
PDF
gRPC or Rest, why not both?
Mohammad Murad
 
PPTX
The Right Kind of API – How To Choose Appropriate API Protocols and Data Form...
Nordic APIs
 
PDF
Usable APIs at Scale
Tim Burks
 
PDF
Building Language Agnostic APIs with gRPC - JavaDay Istanbul 2017
Mustafa AKIN
 
PPTX
Yotpo microservices
Ron Barabash
 
PDF
API Design in the Modern Era - Architecture Next 2020
Eran Stiller
 
OpenAPI and gRPC Side by-Side
Tim Burks
 
LF_APIStrat17_OpenAPI and gRPC Side-by-Side
LF_APIStrat
 
Creating Great REST and gRPC API Experiences (in Swift)
Tim Burks
 
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays
 
Power-up services with gRPC
The Software House
 
What I learned about APIs in my first year at Google
Tim Burks
 
Cloud Native API Design and Management
AllBits BVBA (freelancer)
 
Building REST APIs using gRPC and Go
Alvaro Viebrantz
 
Enforcing API Design Rules for High Quality Code Generation
Tim Burks
 
Implementing OpenAPI and GraphQL services with gRPC
Tim Burks
 
Building your First gRPC Service
Jessie Barnett
 
Taming Cloud APIs with Swift
Tim Burks
 
gRPC Overview
Varun Talwar
 
Microservices in Go_Dessi_Massimiliano_Codemotion_2017_Rome
Massimiliano Dessì
 
gRPC or Rest, why not both?
Mohammad Murad
 
The Right Kind of API – How To Choose Appropriate API Protocols and Data Form...
Nordic APIs
 
Usable APIs at Scale
Tim Burks
 
Building Language Agnostic APIs with gRPC - JavaDay Istanbul 2017
Mustafa AKIN
 
Yotpo microservices
Ron Barabash
 
API Design in the Modern Era - Architecture Next 2020
Eran Stiller
 
Ad

More from Tim Burks (6)

PDF
Governing APIs at Scale
Tim Burks
 
PDF
Build your next REST API with gRPC
Tim Burks
 
PDF
Networked APIs with swift
Tim Burks
 
PDF
Interpreting Objective C
Tim Burks
 
PDF
Deep Geek Diving into the iPhone OS and Frameworks
Tim Burks
 
PDF
Building Open Radar
Tim Burks
 
Governing APIs at Scale
Tim Burks
 
Build your next REST API with gRPC
Tim Burks
 
Networked APIs with swift
Tim Burks
 
Interpreting Objective C
Tim Burks
 
Deep Geek Diving into the iPhone OS and Frameworks
Tim Burks
 
Building Open Radar
Tim Burks
 
Ad

Recently uploaded (20)

PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 

Build Great Networked APIs with Swift, OpenAPI, and gRPC

  • 1. Build Great Networked APIs with Swift, OpenAPI, and gRPC Tim Burks, Google Swift Cloud Workshop No. 2 September 30, 2017
  • 5. What do we want?
  • 7. Protocol Buffers a language-neutral, platform-neutral, extensible mechanism for serializing structured data.
  • 8. Interface Builder for Data message Person { string name = 1; int32 id = 2; string email = 3; message PhoneNumber { string number = 1; } repeated PhoneNumber phone = 4; } Interface Builder: Developers specify their interfaces using a special tool, tooling compiles and integrates that into their apps. Protocol Buffers: Developers specify their data structures using a special language, tooling compiles and integrates that into their apps.
  • 10. Discovery Format Code Generators
  • 14. Toolkit and the next generation of Google APIs
  • 15. So far, so good. REST (Discovery Format) ● 232 API descriptions at https://www.googleapis.com/discovery/v1/apis ● 10 generators listed at https://developers.google.com/api-client-library/ gRPC (Toolkit) ● 30+ gRPC-based APIs ● 7 target languages
  • 16. OpenAPI 3.0 Industry standard format for describing for REST APIs Originally designed for documentation, now with many other applications: API Authoring, Validation, Documentation, Analysis, Search, Testing, Mocking, Management, Code Generation
  • 18. OpenAPI-based code generators ● swagger-codegen (Open source, Smartbear + community) ○ 70+ targets ○ First commit: July 6, 2011 ○ Used by Lyft and Square to generate SDKs ● AutoRest (Open source, Microsoft) ● oas-nodegen (Open source, Capital One) ● APIMatic (Proprietary, APIMatic) more?
  • 20. Problems we’ve seen so far in open source code generators ● Missing or weak build system integration. ● Invalid service addresses (error in OpenAPI description). ● No auth. ● No documentation (not even a README!). ● No samples. ● No test harness. ● No assurance that the called service even works. ● Ugly generated code. ○ Machine-generated operation names. ○ Machine-generated struct names. ● Unwanted dependencies.
  • 21. swagger-codegen often isn’t used “out of the box”: “Generating client libraries involves customizing the provided language-specific templates… The amount of modification each template needs varies by language and we’re looking forward to working with the Swagger Codegen community to share our refinements.” Val Polouchkine, Lyft “...Swagger Codegen is a pretty active project. If you don’t check in your templates, things are gonna break unexpectedly because Swagger Codegen just uses the latest and greatest templates that are out there. So if you don’t sort of manually make sure that those things work, you’re gonna have an issue there.” Tristan Sokol, Square
  • 22. Code generation pipeline API Description Generatable API Description Language- specific model Generated API support code render idiomatic API support code according to user preferences (re. build systems, dependencies) verify model, define structures and entry points, name everything filter language- specific reserved words, (optionally) define file structure for generated code
  • 23. typical monolithic code generation pipeline API Description (OpenAPI) Generatable API Description (internal data structures) Language- specific model (internal data structures) Generated API support code All-in-one repo and package
  • 24. Problems with monolithic code generators ● Long build times: changing one target requires rebuilding everything. ● Long test times: new builds must be tested for every target language. ● For stability, teams may prefer to archive their own generator builds. ● Forks will abound. ● Quality is uneven. ● Versioning is hard. ● Complexity and potentially unfamiliar build systems deter contributors.
  • 25. protoc code generation pipeline API Description (.proto) Generatable API Description (binary FileDescriptors) Language- specific model (internal data structures) Generated API support code language target- specific plugins protoc
  • 27. Why does protoc have a plug-in architecture? ● Fast build times: changing one target only requires rebuilding its plugin. ● Fast test times: new builds need only be tested for the affected targets. ● For stability, teams can archive their own protoc and plugin builds. ● New plugins can abound. ● Separately-maintained plugins can offer different maturity levels. ● Separately-maintained plugins can be appropriately versioned. ● Separately-maintained plugins can be in languages that contributors prefer.
  • 28. What’s the catch? Plugins require a well-defined interchange format. Fortunately, we have two great tools for that.
  • 29. gnostic gnostic processed and verified protobuf representation of OpenAPI description protoc + pluginsOpenAPI .proto reusable data structures and reader for protobuf OpenAPI descriptions gnostic apps and plugins OpenAPI description gnostic- generator OpenAPI .proto and compiler code OpenAPI JSON schema
  • 30. Kubernetes OpenAPI: .json vs .pb Format Size Deserialization time Download time (at 80 Mbps) Json 1653 KB >500 ms 165.3 ms Proto binary 914 KB 9.3 ms 91.4 ms Proto binary compressed 96 KB 13.5 ms 1.3 ms Source: [email protected]
  • 31. gnostic code generation pipeline API Description (OpenAPI v3) Generatable API Description (Normalized/ Annotated OpenAPI v3?) Language- specific model (Normalized/ Annotated OpenAPI v3?) Generated API support code target-specific plugins gnostic + linter
  • 32. let anyone write code generation plugins... ● in their own repositories ● with their own versioning ● in whatever implementation language they choose API code generation is a community problem that needs community-based solutions...
  • 33. Code generators should belong to their communities.
  • 34. gRPC
  • 35. @grpcio RPC: Use Cases Direct RPCs: Microservices RPCs to access APIs Google APIs OSS APIs Mobile Web Desktop RPCs Datacenters Cloud Service 1 Service 2 Service 3 Service 4 Containers
  • 36. @grpcio What is gRPC? ● HTTP/2 based RPC framework developed by Google ● Open, Multiplatform, Secure, Performant Multiplatform ● Idiomatic APIs in popular languages (C++, Go, Java, Python, Node.js, C#, Ruby, PHP) ● Supports mobile devices (Android Java, iOS Obj-C, Swift) ● Linux, Windows, Mac OS X ● (web browser support in development) OpenSource ● developed fully in open on GitHub: https://github.com/grpc/
  • 37. @grpcio ● Builds on Apple’s swift-protobuf and grpc-core. ● Includes: ○ gRPC framework (C and Swift components) ○ generated code surface ○ protoc plugin for code generation ● Full-service gRPC: ○ All four gRPC API styles are supported. ○ gRPC framework supports both clients and servers. ○ Plugin generates client and server code in separate files. ○ Testing on MacOS and Ubuntu. ● Audiences: client and server developers, inside/outside Google gRPC for Swift (https://github.com/grpc/grpc-swift)
  • 38. @grpcio package echo; service Echo { // Immediately returns an echo of a request. rpc Get(EchoRequest) returns (EchoResponse) {} // Splits a request into words and returns each word in a stream of messages. rpc Expand(EchoRequest) returns (stream EchoResponse) {} // Collects a stream of messages and returns them concatenated when the caller closes. rpc Collect(stream EchoRequest) returns (EchoResponse) {} // Streams back messages as they are received in an input stream. rpc Update(stream EchoRequest) returns (stream EchoResponse) {} } message EchoRequest { // The text of a message to be echoed. string text = 1; } message EchoResponse { // The text of an echo response. string text = 1; } gRPC Swift sample service
  • 39. @grpcio /// To build a server, implement a class that conforms to this protocol. public protocol Echo_EchoProvider { func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws func collect(session : Echo_EchoCollectSession) throws func update(session : Echo_EchoUpdateSession) throws } gRPC Swift server protocol
  • 40. @grpcio // get returns requests as they were received. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse { return Echo_EchoResponse(text:"Swift echo get: " + request.text) } ... // update streams back messages as they are received in an input stream. func update(session : Echo_EchoUpdateSession) throws -> Void { while true { do { let request = try session.Receive() try session.Send(Echo_EchoResponse(text:"Swift echo update: (request.text)")) } catch Echo_EchoServerError.endOfStream { break } } try session.Close() } } gRPC Swift server sample
  • 41. @grpcio gRPC Swift unary client sample // Unary if client == "get" { var requestMessage = Echo_EchoRequest(text:message) let responseMessage = try service.get(requestMessage) // blocking print("get received: " + responseMessage.text) }
  • 42. @grpcio gRPC Swift bidirectional streaming client sample (1/2) // Bidirectional streaming if client == "update" { let sem = DispatchSemaphore(value: 0) let updateCall = try service.update() // blocking DispatchQueue.global().async { while true { do { let responseMessage = try updateCall.Receive() // blocking print("Received: (responseMessage.text)") } catch Echo_EchoClientError.endOfStream { sem.signal() break } } } ...
  • 43. @grpcio gRPC Swift bidirectional streaming client sample (2/2) ... let parts = message.components(separatedBy:" ") for part in parts { let requestMessage = Echo_EchoRequest(text:part) try updateCall.Send(requestMessage) sleep(1) } try updateCall.CloseSend() // Wait for the call to complete. sem.wait() }
  • 45. gRPC-Swift TODO ● Build system integration ○ Package Manager ○ Cocoapods? ○ Carthage? ● gRPC interoperability tests ● Samples that wrap Google APIs ○ Google Cloud Speech API ○ Google Datastore API github.com/grpc/grpc-swift/issues
  • 46. More gRPC Information Website: http://grpc.io Sources: https://github.com/grpc/grpc Mailing list: https://groups.google.com/d/forum/grpc-io Ecosystem: https://github.com/grpc-ecosystem
  • 47. github.com/googleapis/gnostic plugins for OpenAPI-based code generation github.com/grpc/grpc-swift Fast streaming APIs in Swift github.com/google/auth-library-swift OAuth support for Google Cloud http://twitter.com/timburks [email protected]