Create Delightful SDKs with OpenAPI

A presentation at London Gophers in November 2020 in London, UK by Lorna Jane Mitchell

Slide 1

Slide 1

Generating SDKs from OpenAPI Lorna Mitchell, Vonage

Slide 2

Slide 2

What is OpenAPI? Machine-readable API description (YAML or JSON) • Describe endpoints, with verbs • Parameters, request bodies • Responses The standard formerly known as Swagger @lornajane

Slide 3

Slide 3

Why Describe Your APIs? • Publish API descriptions for developers to use • Generate API reference docs • Import to Postman • Use with mock servers • Generate SDKs and libraries @lornajane

Slide 4

Slide 4

Start with Docs OpenAPI encourages descriptions and examples • Separates content and presentation @lornajane

Slide 5

Slide 5

SDKs for Delightful Developer Experiences @lornajane

Slide 6

Slide 6

Don’t Make Me Think Reduce cognitive load on your users @lornajane

Slide 7

Slide 7

How to Generate Code This example from https://openapi-generator.tech/ docker run —rm -v ${PWD}:/local openapitools/openapi-generator-cli \ generate \ -i verify.yml \ —package-name verify \ -g go \ -o /local/out/golang/verify @lornajane

Slide 8

Slide 8

Generated Code @lornajane

Slide 9

Slide 9

Generated Code Generated libraries are better than nothing! If there is no library, a code generator gives a developer: • Abstracts away the HTTP handling • Simple validation (probably) • IDE autocomplete for the basic structure @lornajane

Slide 10

Slide 10

We Can Do Better @lornajane

Slide 11

Slide 11

About Generated Code @lornajane

Slide 12

Slide 12

Wrap Sharp Edges For example, the autocomplete example from earlier: ctx := context.WithValue( context.Background(), verify.ContextAPIKey, verify.APIKey{Key: client.apiKey} ) result, resp, err := verifyClient.DefaultApi.VerifyRequest( ctx, “json”, client.apiSecret, number, brand, verify.VerifyRequestOpts{}) @lornajane

Slide 13

Slide 13

Wrap Sharp Edges For the user, it’s more like: auth := vonage.CreateAuthFromKeySecret(API_KEY, API_SECRET) verifyClient := vonage.NewVerifyClient(auth) resp, _, _ := verifyClient.Request( PHONE, “GoTest”, vonage.VerifyOpts{}) @lornajane

Slide 14

Slide 14

Auth Helpers Supply auth details: // for basic key/secret auth := vonage.CreateAuthFromKeySecret(“abcd1234”, “VeryS3cr3t”) // for JWT auth privateKey, _ := ioutil.ReadFile(PATH_TO_PRIVATE_KEY_FILE) auth, _ := vonage.CreateAuthFromAppPrivateKey(APP_ID, privateKey) Quickly get a JWT: privateKey, _ := ioutil.ReadFile(“private.key”) g := jwt.NewGenerator(APPLICATION_ID, privateKey) token, _ := g.GenerateToken() @lornajane

Slide 15

Slide 15

Security Helpers SMS API webhooks use signatures from shared secrets if smsClient.checkSig(params, SIG_SECRET, “sha256”) { // data is plausible, do something cool } Make the easy thing also the Right Thing (TM) @lornajane

Slide 16

Slide 16

Builders For example, formatting JSON structures from strongly-typed languages MyNcco := ncco.Ncco{} talk := ncco.TalkAction{ Text: “Go library calling to say hello”, VoiceName: “Nicole” } MyNcco.AddAction(talk) endpoint := []ncco.PhoneEndpoint{Number: “44777000777”} connect := ncco.ConnectAction{Endpoint: endpoint, From: “44777000888”} MyNcco.AddAction(connect) result, _, err := client.CreateCall( vonage.CreateCallOpts{From: from, To: to, Ncco: MyNcco}) @lornajane

Slide 17

Slide 17

Delightful SDKs Wrappers are the “Delight” in the “Delightful SDK” @lornajane

Slide 18

Slide 18

Go The Extra Mile For added customer delight: • Code examples, for everything • Detailed tutorials, joining up more than one piece of the puzzle For your own delight: • Download stats • Usage stats @lornajane

Slide 19

Slide 19

SDKs for Delighted Devs OpenAPI lets us focus on the best bits! @lornajane

Slide 20

Slide 20

Thanks! References: • https://github.com/Vonage/vonage-go-sdk • http://spec.openapis.org/ • https://developer.nexmo.com/tools • https://openapi.tools Me: • https://lornajane.net @lornajane