HTTP APIs as first class procedures in your language: cutting out SDK complexity.

166
APIconUK 2014 1 / 149 2014/09/24 [email protected] @databasescaling Thursday, 25 th September 2014

description

Andy Bennett, Founder, Knodium This talk looks at how to make an HTTP API available as a self-contained procedure or function, just like any other library or API code that you might be used to using or importing into your programs. Whether you're implementing an SDK for your own API or just trying to bind to an adhoc API that you want to use, this talk will cover how to do it in an elegant and simple way without lots of adhoc code duplication, needless boilerplate or irrelevant detail.

Transcript of HTTP APIs as first class procedures in your language: cutting out SDK complexity.

Page 1: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 1 / 1492014/09/24

[email protected]@databasescaling

Thursday, 25th September 2014

Page 2: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 2 / 1492014/09/24

[email protected]@databasescaling

HTTP APIs As First Class Procedures

Thursday, 25th September 2014

Page 3: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 3 / 1492014/09/24

HTTP APIs As First Class Procedures

[email protected]@databasescaling

Cutting out SDK Complexity

Thursday, 25th September 2014

Page 4: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 4 / 1492014/09/24

andyjpb

http://www.obsoletecomputermuseum.org/oliv_m24/m24.jpg

Page 5: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 5 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

Page 6: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 6 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

Page 7: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 7 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

Page 8: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 8 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

Page 9: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 9 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

Page 10: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 10 / 1492014/09/24

GSM AT Command Set

cmd AT+CREG "Network Registration (ver.2)" set AT+CREG=[<creg_stat>],<creg_urc>,<ci> read AT+CREG? +CREG:<creg_urc>,<creg_stat>[,<lac>,<ci>] test AT+CREG=? +CREG

int AT_CREG_test(int port, struct AT_result* result) { int r = 0;

if ((r = sendcmd(port, "AT+CREG=?", result))) return r; return recvresp(port, result);}

Page 11: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 11 / 1492014/09/24

Execution Tracing

DBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) {

/* Increment the cursor to the next row in the direction * that the cursor is travelling and return the row to * the caller. */

...

}

Page 12: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 12 / 1492014/09/24

Execution Tracing

DBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result);

Page 13: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 13 / 1492014/09/24

Execution Tracing

DBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result);

Page 14: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 14 / 1492014/09/24

Execution Tracing

DBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result);

Page 15: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 15 / 1492014/09/24

Execution TracingDBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) { DBrow *(*original_proc)(DB*, DBcursor*, DBresult*); DBrow *original_result;

log(“Entering CursorNext”); DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

original_proc = dlsym(RTLD_NEXT, “CursorNext”); original_result = (*original_proc)(env, cursor, result);

DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

Log(“Leaving CursorNext”);

return (original_result);}

Page 16: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 16 / 1492014/09/24

Execution TracingDBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) { DBrow *(*original_proc)(DB*, DBcursor*, DBresult*); DBrow *original_result;

log(“Entering CursorNext”); DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

original_proc = dlsym(RTLD_NEXT, “CursorNext”); original_result = (*original_proc)(env, cursor, result);

DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

Log(“Leaving CursorNext”);

return (original_result);}

Page 17: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 17 / 1492014/09/24

Execution TracingDBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) { DBrow *(*original_proc)(DB*, DBcursor*, DBresult*); DBrow *original_result;

log(“Entering CursorNext”); DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

original_proc = dlsym(RTLD_NEXT, “CursorNext”); original_result = (*original_proc)(env, cursor, result);

DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

Log(“Leaving CursorNext”);

return (original_result);}

Page 18: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 18 / 1492014/09/24

Execution TracingDBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) { DBrow *(*original_proc)(DB*, DBcursor*, DBresult*); DBrow *original_result;

log(“Entering CursorNext”); DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

original_proc = dlsym(RTLD_NEXT, “CursorNext”); original_result = (*original_proc)(env, cursor, result);

DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

Log(“Leaving CursorNext”);

return (original_result);}

Page 19: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 19 / 1492014/09/24

Execution TracingDBrow *CursorNext(DB *env, DBcursor* cursor, DBresult* result) { DBrow *(*original_proc)(DB*, DBcursor*, DBresult*); DBrow *original_result;

log(“Entering CursorNext”); DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

original_proc = dlsym(RTLD_NEXT, “CursorNext”); original_result = (*original_proc)(env, cursor, result);

DB_serialise(env); Cursor_serialise(cursor); Result_serialise(result);

Log(“Leaving CursorNext”);

return (original_result);}

Page 20: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 20 / 1492014/09/24

Knodium

Page 21: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 21 / 1492014/09/24

Knodium

Page 22: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 22 / 1492014/09/24

Knodium

Page 23: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 23 / 1492014/09/24

Knodium

Page 24: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 24 / 1492014/09/24

Knodium

Page 25: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 25 / 1492014/09/24

Knodium

Page 26: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 26 / 1492014/09/24

Knodium

Page 27: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 27 / 1492014/09/24

Knodium

Page 28: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 28 / 1492014/09/24

Knodium

Page 29: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 29 / 1492014/09/24

Knodium

Page 30: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 30 / 1492014/09/24

Knodium

Page 31: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 31 / 1492014/09/24

Knodium

Page 32: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 32 / 1492014/09/24

Knodium

Page 33: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 33 / 1492014/09/24

Knodium

Page 34: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 34 / 1492014/09/24

Knodium

Page 35: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 35 / 1492014/09/24

Knodium

Page 36: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 36 / 1492014/09/24

Knodium

Page 37: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 37 / 1492014/09/24

Knodium

Page 38: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 38 / 1492014/09/24

Knodium

Page 39: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 39 / 1492014/09/24

Knodium

Page 40: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 40 / 1492014/09/24

Knodium

Page 41: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 41 / 1492014/09/24

Knodium

Page 42: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 42 / 1492014/09/24

Scheme

Page 43: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 43 / 1492014/09/24

REST

Read the HTTP spec!

Page 44: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 44 / 1492014/09/24

REST

"Finally, I describe the lessons learned from applying REST to the design of the Hypertext Transfer Protocol and Uniform Resource

Identifier standards, and from their subsequent deployment in Web

client and server software."

-- Roy Fielding

Page 45: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 45 / 1492014/09/24

GETRetrieves a representation of the requested resource (current state)

POSTCreate a new instance of a resource

PUTUpdate an existing instance of a resource

DELETERemove an existing instance of a resource

REST

Page 46: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 46 / 1492014/09/24

http://static.squarespace.com/static/518f5d62e4b075248d6a3f90/t/51bb91f3e4b0405092e56e27/1371247103965/not-restful.jpg

Page 47: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 47 / 1492014/09/24

DCE

"The Distributed Computing Environment (DCE) is a software

system developed in the early 1990s"

Page 48: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 48 / 1492014/09/24

"This system allows programmers to write distributed software as if it were

all working on the same computer, without having to worry about the

underlying network code."

DCE/RPC

Page 49: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 49 / 1492014/09/24

"This system allows programmers to write distributed software as if it were

all working on the same computer, without having to worry about the

underlying network code."

DCE/RPC

Page 50: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 50 / 1492014/09/24

Adhoc Binding

“I'm looking for a pretty generic, fully featured REST client/API in

python. Not bare bones, but plush, nice to use.”

http://stackoverflow.com/questions/4355997/is-there-a-generic-python-library-to-consume-rest-based-services

Page 51: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 51 / 1492014/09/24

Adhoc Binding

“...an HTTP resource kit for Python. It allows you to easily access to HTTP resource and build objects around it.”

Page 52: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 52 / 1492014/09/24

Adhoc Binding

“...an HTTP resource kit for Python. It allows you to easily access to HTTP resource and build objects around it.”

“[It] is a full HTTP client using pure socket calls and its own HTTP parser.”

Page 53: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 53 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 54: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 54 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 55: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 55 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 56: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 56 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 57: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 57 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 58: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 58 / 1492014/09/24

Adhoc Binding

api = API('http://myrestful/api/v1')

# GET /resourceapi.resource.get()# GET /resource (with accept header = application/json)api.resource.get(format='json')# GET /resource?attr=valueapi.resource.get(attr=value)

# POST /resourceapi.resource.post(attr=value, attr2=value2, ...)# GET /resource/id/resource_collectionapi.resoure(id).resource_collection().get()

Page 59: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 59 / 1492014/09/24

SDKs

Page 60: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 60 / 1492014/09/24

SDKs

● HTTP Clients

Page 61: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 61 / 1492014/09/24

SDKs

● HTTP Clients● REST Clients

Page 62: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 62 / 1492014/09/24

SDKs

● HTTP Clients● REST Clients● Socket Pools

Page 63: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 63 / 1492014/09/24

SDKs

● HTTP Clients● REST Clients● Socket Pools● Request Builders

Page 64: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 64 / 1492014/09/24

SDKs

● HTTP Clients● REST Clients● Socket Pools● Request Builders● URI Concatenators

Page 65: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 65 / 1492014/09/24

SDKs

● HTTP Clients● REST Clients● Socket Pools● Request Builders● URI Concatenators● OAuth Implementations

Page 66: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 66 / 1492014/09/24

SDKs

Page 67: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 67 / 1492014/09/24

SDKs● PHP● Python● Ruby● .NET● Java● C, C++● Perl● Android, iOS, Windows Mobile, Blackberry● Javascript

Page 68: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 68 / 1492014/09/24

ctypes

https://docs.python.org/2/library/ctypes.html

Page 69: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 69 / 1492014/09/24

ctypes

>>> from ctypes import *>>> cdll.LoadLibrary("libc.so.6")<CDLL 'libc.so.6', handle 7f408bf8a000 at 21eb8d0>>>> libc = CDLL("libc.so.6")

>>> my_printf = libc.printf

>>> my_printf("Hello World")Hello World11

>>> my_printf("Hello %s", "Andy")Hello Andy10

Page 70: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 70 / 1492014/09/24

ctypes

>>> from ctypes import *>>> cdll.LoadLibrary("libc.so.6")<CDLL 'libc.so.6', handle 7f408bf8a000 at 21eb8d0>>>> libc = CDLL("libc.so.6")

>>> my_printf = libc.printf

>>> my_printf("Hello World")Hello World11

>>> my_printf("Hello %s", "Andy")Hello Andy10

Page 71: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 71 / 1492014/09/24

ctypes

>>> from ctypes import *>>> cdll.LoadLibrary("libc.so.6")<CDLL 'libc.so.6', handle 7f408bf8a000 at 21eb8d0>>>> libc = CDLL("libc.so.6")

>>> my_printf = libc.printf

>>> my_printf("Hello World")Hello World11

>>> my_printf("Hello %s", "Andy")Hello Andy10

Page 72: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 72 / 1492014/09/24

ctypes

>>> from ctypes import *>>> cdll.LoadLibrary("libc.so.6")<CDLL 'libc.so.6', handle 7f408bf8a000 at 21eb8d0>>>> libc = CDLL("libc.so.6")

>>> my_printf = libc.printf

>>> my_printf("Hello World")Hello World11

>>> my_printf("Hello %s", "Andy")Hello Andy10

Page 73: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 73 / 1492014/09/24

ctypes

● Code Loading● Calling Conventions● Marshalling● Memory Models

Page 74: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 74 / 1492014/09/24

● HTTP Implementation● HTTP Client● URI / URL Library● Media Format Libraries

Page 75: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 75 / 1492014/09/24

● HTTP Implementation● HTTP Client● URI / URL Library● Media Format Libraries

● A Way To Put It All Together

Page 76: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 76 / 1492014/09/24

Calling Conventions

https://api-content.dropbox.com/1/files/auto/<path>

Page 77: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 77 / 1492014/09/24

Calling Conventions

https://api-content.dropbox.com/1/files/auto/<path>

https://example.com/price/<date>/<€>/<£>

Page 78: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 78 / 1492014/09/24

Calling Conventions

https://api-content.dropbox.com/1/files/auto/<path>

https://example.com/price/<date>/<€>/<£>

Page 79: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 79 / 1492014/09/24

Calling Conventions

files(<path>)

price(<date>, <€>, <£>)

Page 80: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 80 / 1492014/09/24

Calling Conventions

https://example.com/trades?tick=<n>

https://api.dropbox.com/1/delta?cursor=<x>

Page 81: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 81 / 1492014/09/24

Calling Conventions

trades(tick=<n>)

delta(cursor=<x>)

Page 82: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 82 / 1492014/09/24

https://.../1/revisions/auto/<path>?rev_limit=<y>

Calling Conventions

https://example.com/trades/<stock>?tick=<y>

Page 83: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 83 / 1492014/09/24

revisions(<path>, rev_limit=<y>)

Calling Conventions

trades(<stock>, tick=<y>)

Page 84: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 84 / 1492014/09/24

Calling Conventions

(define (name pos-arg-1 pos-arg-2 #!key key-arg-1) … result)

Page 85: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 85 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

Page 86: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 86 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

(add 19 47)

Page 87: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 87 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

(add 19 47)

66

Page 88: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 88 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

(add 19 47)

66

(define (raise n #!key power) (expt n power))

Page 89: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 89 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

(add 19 47)

66

(define (raise n #!key power) (expt n power))

(raise 2 power: 4)

Page 90: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 90 / 1492014/09/24

Calling Conventions

(define (add a b) (+ a b))

(add 19 47)

66

(define (raise n #!key power) (expt n power))

(raise 2 power: 4)

16

Page 91: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 91 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 92: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 92 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 93: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 93 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 94: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 94 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 95: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 95 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 96: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 96 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 97: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 97 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 98: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 98 / 1492014/09/24

Putting It All Together

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 99: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 99 / 1492014/09/24

https://api.dropbox.com/1/account/info

Putting It All Together

Page 100: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 100 / 1492014/09/24

https://api.dropbox.com/1/account/info

Putting It All Together

(define-method (account/info #!key locale) "https://api.dropbox.com/1/account/info" #f read-json)

Page 101: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 101 / 1492014/09/24

https://api.dropbox.com/1/account/info

Putting It All Together

(define-method (account/info #!key locale) "https://api.dropbox.com/1/account/info" #f read-json)

#;> account/info#<procedure (? #!key locale)

Page 102: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 102 / 1492014/09/24

https://api.dropbox.com/1/account/info

Marshalling / Unmarshalling

(define-method (account/info #!key locale) "https://api.dropbox.com/1/account/info" #f read-json)

#;> account/info#<procedure (? #!key locale)

Page 103: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 103 / 1492014/09/24

https://api.dropbox.com/1/account/info

Marshalling / Unmarshalling

Page 104: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 104 / 1492014/09/24

https://api.dropbox.com/1/account/info

Marshalling / Unmarshalling

{ "referral_link": "https://www.dropbox.com/referrals/r1a2n3d4m5s6t7", "display_name": "John P. User", "uid": 12345678, "team": { "name": "Acme Inc." }, "country": "US", "quota_info": { "shared": 253738410565, "quota": 107374182400000, "normal": 680031877871 }}

Page 105: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 105 / 1492014/09/24

https://api.dropbox.com/1/account/info

Marshalling / Unmarshalling

((referral_link . "https://www.dropbox.com/referrals/r1a2n3d4m5s6t7") (display_name . "John P. User") (uid . 12345678) (country . "US") (quota_info (shared . 253738410565) (quota . 107374182400000) (normal . 680031877871)))

Page 106: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 106 / 1492014/09/24

Marshalling / Unmarshalling

(account/info)

Page 107: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 107 / 1492014/09/24

{ "referral_link": "https://www.dropbox.com/referrals/r1a2n3d4m5s6t7", "display_name": "John P. User", "uid": 12345678, "team": { "name": "Acme Inc." }, "country": "US", "quota_info": { "shared": 253738410565, "quota": 107374182400000, "normal": 680031877871 }}

Marshalling / Unmarshalling

(account/info)

read-string

Page 108: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 108 / 1492014/09/24

Marshalling / Unmarshalling

(account/info)

((referral_link . "https://www.dropbox.com/referrals/r1a2n3d4m5s6t7") (display_name . "John P. User") (uid . 12345678) (country . "US") (quota_info (shared . 253738410565) (quota . 107374182400000) (normal . 680031877871)))

read-json

Page 109: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 109 / 1492014/09/24

(define-method (trades stock #!key tick) "https://examples.com/trades")

#f read-json)

https://example.com/trades/<stock>?tick=<y>

Calling Conventions

Page 110: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 110 / 1492014/09/24

Calling Conventions

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 111: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 111 / 1492014/09/24

Calling Conventions

(use rest-bind)

(define-method

(procedure-name path-arg... #!key query-arg...)

uri-or-request

body-writer

body-reader

#!optional header-reader)

Page 112: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 112 / 1492014/09/24

https://example.com/trades/<stock>?tick=<y>

Calling Conventions

(define-method (trades stock #!key tick) "https://examples.com/trades")

#f read-json)

Page 113: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 113 / 1492014/09/24

https://example.com/trades/<stock>?tick=<y>

Calling Conventions

(define-method (trades stock #!key tick) "https://examples.com/trades")

#f read-json)

Page 114: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 114 / 1492014/09/24

https://example.com/trades/<stock>?tick=<y>

Calling Conventions

(define-method (trades stock #!key tick) (make-request method: 'PUT uri: (uri-reference: "https://examples.com/trades") #f read-json)

Page 115: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 115 / 1492014/09/24

https://example.com/trades/<stock>?tick=<y>

Calling Conventions

(define-method (trades stock #!key tick) (make-request method: 'PUT uri: (uri-reference: "https://examples.com/trades") headers: '(accept #(application/json ((q . 0.5))))) #f read-json)

Page 116: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 116 / 1492014/09/24

Putting It All Together

(trades 'LON:TSCO tick: 3)

Page 117: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 117 / 1492014/09/24

Putting It All Together

(trades 'LON:TSCO tick: 3)

((1411602130 . 19490 1411602133 . +6 1411602136 . +1 1411602139 . -2 1411602142 . +3 1411602145 . +1 1411602148 . -2 1411602151 . -1 1411602154 . 0 1411602157 . +1))

Page 118: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 118 / 1492014/09/24

● HTTP Implementation● HTTP Client● URI / URL Library● Media Format Libraries

● A Way To Put It All Together

Page 119: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 119 / 1492014/09/24

● HTTP Implementation● HTTP Client● URI / URL Library● Media Format Libraries

● A Way To Put It All Together

Page 120: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 120 / 1492014/09/24

● HTTP Implementation● HTTP Client● URI / URL Library● Media Format Libraries

● A Way To Put It All Together

66 lines

Page 121: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 121 / 1492014/09/24

Putting It All Together(module dropbox-lolevel (dropbox make-dropbox-app account/info files:get files:put files:post metadata delta revisions restore search shares media copy-ref thumbnails chunked_upload commit_chunked_upload fileops/copy fileops/create_folder fileops/delete fileops/move callback old-output-port)

Page 122: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 122 / 1492014/09/24

oauth

Dropbox

Page 123: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 123 / 1492014/09/24

oauth

Dropbox

A User

Page 124: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 124 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 125: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 125 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 126: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 126 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 127: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 127 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 128: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 128 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 129: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 129 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 130: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 130 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 131: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 131 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 132: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 132 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 133: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 133 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 134: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 134 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 135: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 135 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 136: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 136 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 137: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 137 / 1492014/09/24

oauth

(define dropbox (make-oauth-service-provider protocol-version: 1.0 credential-request-url: "https://api.dropbox.com/1/oauth/request_token" owner-auth-url: "https://www.dropbox.com/1/oauth/authorize" token-request-url: "https://api.dropbox.com/1/oauth/access_token" signature-method: 'plaintext))

(use oauth-client)

Page 138: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 138 / 1492014/09/24

(define dropbox-app (make-oauth-service service: dropbox client-credential: (make-oauth-credential "<token>" "<secret>")))

oauth

(use oauth-client)

Page 139: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 139 / 1492014/09/24

oauth

Dropbox

A User

Knodium

Page 140: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 140 / 1492014/09/24

Putting it all together

(with-oauth <app> <credential> ...)

Page 141: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 141 / 1492014/09/24

Putting it all together

(account/info)

Page 142: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 142 / 1492014/09/24

Putting it all together

(with-oauth <app> <credential> (account/info))

Page 143: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 143 / 1492014/09/24

Putting it all together

(account/info)

Page 144: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 144 / 1492014/09/24

Putting it all together

(account/info)

Page 145: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 145 / 1492014/09/24

Putting it all together

(account/info)

(display-name)

Page 146: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 146 / 1492014/09/24

Putting it all together

(account/info)

(display-name)

(web-page-logic)

Page 147: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 147 / 1492014/09/24

Putting it all together

(account/info)

(display-name)

(web-page-logic)

(request-routing)

Page 148: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 148 / 1492014/09/24

Putting it all together

(account/info)

(display-name)

(web-page-logic)

(request-routing)

(with-oauth <app> <credential>)

Page 149: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 149 / 1492014/09/24

?

Page 150: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 150 / 1492014/09/24

Page 151: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 151 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>

Page 152: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 152 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>?tick=<n>

Page 153: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 153 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>?tick=<n>

Page 154: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 154 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>?tick=<n>

Page 155: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 155 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>?tick=<n>

https://example.com/price/<x>/<y>

Page 156: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 156 / 1492014/09/24

URIs & URLs Are Not Strings

https://example.com/price/<x>/<y>?tick=<n>

Page 157: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 157 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 158: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 158 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 159: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 159 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 160: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 160 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 161: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 161 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 162: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 162 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 163: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 163 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 164: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 164 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

Page 165: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 165 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

ftp://example.com

//example.com/resource

//example.com

/book

?length=17;width=19

?depth=21&height=29#glossary

#chapter1

Page 166: HTTP APIs as first class procedures in your language: cutting out SDK complexity.

APIconUK 2014 166 / 1492014/09/24

URIs & URLs Are Not Strings

https://user:[email protected]/price?tick=3#appendix

ftp://example.com

//example.com/resource

//example.com

/book

?length=17;width=19

?depth=21&height=29#glossary

#chapter1