Api presentation

30
API An introduction

Transcript of Api presentation

Page 1: Api presentation

APIAn introduction

Page 2: Api presentation

WEB REST JSON API● WEB - Set of HTTP Endpoints

● REST - Descriptive URLs, nouns and verbs, emphasis on readability

● JSON - Output format (JavaScript Object Notation)

● API - Application Programming Interface

Page 3: Api presentation

Other WS-Protocols● SOAP● XML-RPC● ...

● Clean protocol (less complexity)● Reduces overhead of XML envelopes

creation/handling

Page 4: Api presentation

Other Output Formats● XML● CSV● ...

● Less verbosity● Client direct interaction (most clients handle

Javascript/JSON interaction)

Page 5: Api presentation

Who is it for?● Desktop Applications● Mobile Applications● Third-Party Web Applications● Everything that HTTPs

Purpose Interaction of external services with our backend

Page 6: Api presentation

Who is it for? (2)End UserClient Service Developer Team

Goal Maximize end user productivity

Page 7: Api presentation

Modules

● Input - Output - Error Output● Documentation● User/App Identification

Page 8: Api presentation

Input - Output - Error OutputThink of each HTTP Endpoint as a method

● What should it receive?● What should it respond/return?● How should it behave when something was

unexpected?

Page 9: Api presentation

Input - Output - Error Output (2)http://api.platform.com/banjos/1

● What it received ○ 1 (identifier of the banjo)

● What will it respond? ○ Depends of your design decision, as long as its

JSON (ex: {“state” : “ok”, “banjo” : {“id” : 1, “brand”: “Les Paulanjo”}}

● How should it handle errors?○ Depends of your design decision, as long as its

JSON (ex: {“state” : “error”, “error” : “No banjo 1”}

Page 10: Api presentation

Input - Output - Error Output (3)In Rightclearing:

All responses have a “state” parameter (possible values either “ok” or “error”)

GET - response definition per resource/actionPOST - returns Id of the newly-created resourcePUT - nothing relevantDELETE - nothing relevant

Page 11: Api presentation

Input - Output - Error Output (4)Errors In Rightclearing:● single error:

○ error: error code○ error_description: error message○ error_uri: uri for the error documentation

● multiple errors:○ errors: collection of errors○ for each error:

■ code: error code■ messages: collection or error messages■ uri: uri for the error documentation

Page 12: Api presentation

Input - Output - Error Output (5)Room for Improvement

● “single error” spec is only used by Oauth endpoints (follows its spec)

● “multiple errors” spec is used everywhere else (convention/ience for multiple validation message problem)

Could one find a standardization of error messages? This might get confusing

Page 13: Api presentation

DocumentationUnder construction...

● APIs are not Apps (no incremental learning here, no navigating)

● End User must know what can he use, how can he use, where can he use.

Straightforwardness is the key - be very clear and concise in describing functionality

Page 14: Api presentation

Documentation (2)For each endpoint:● Description● Route● Request Method● Request Headers● Request Parameters● Response Headers● Response Body● Example

Page 15: Api presentation

Documentation (3)For each error (still under development):● Description● Solutions/Workarounds● ...(?)

https://docs.google.com/document/d/1daK5zRlPZDQ2tV6TFUgibEmejLjrkSavD_6ktU0hpQE/edit

Page 16: Api presentation

User/App IdentificationMany approaches:● app password● Open ID● Oauth● etc...

Page 17: Api presentation

User/App Identification(Oauth)● Manager owns resources in Rightclearing

(Resource Owner)● Allows binding of Resource Owner’s

accounts in other services with his Rightclearing Account (easy identification)

● Resource Owner can define a set of permissions per client application

● Resources are the Resource Owner’s responsibility

Page 18: Api presentation

User/App Identification(Oauth) (2)● Doesn't provide security (SSL does)● Authorization Protocol, yet needs

authentication● ...

● no clear better alternative● a lot of existing libraries on the protocol in

most of programming languages

Page 19: Api presentation

API Application Ecosystem● Framework - Sinatra

● Common modules libraries - rc-logic

● Architecture - MVDispatcher / Façade

● SDKs - rc-sdk-ruby (for now)

● Integration - Oauth Authorizations

Page 20: Api presentation

Framework - SinatraAdvantages

● Minimal● Thread-safe● Does not make assumptions● Flexible● Lots of extensions / well-developed

ecosystem● Supports HTTP very well

Page 21: Api presentation

Framework - Sinatra (2)Disadvantages

● Sinatra specific, found none, maybe later

(Rails dependent behaviour in certain gems are more a gem-specific disadvantage than the other way round)

Page 22: Api presentation

Common modules libraries rc-logic

● We want to process user input, access/handle resources, provide JSON output

● Sinatra/Ruby handle user input/output, AR/filesystem libraries handle resources

Data Integrity must be kept cross-application (a resource in the API is the same as in the main web app)

Page 23: Api presentation

Common modules libraries rc-logic (2)What is shared?

● Common Data Model Mapping / Integration● Common Data Model/File handling libraries● Common configuration (database conf, app

conf, filesystem conf)

Page 24: Api presentation

Architecture - MVDispatcher / Façade● Models - API-specific models or Extensions of

common models with API-specific behaviour● Views - Handle model view representation

(JSON-visible attributes, model delegations, attribute formatting)

● Helpers - encapsulated logic from filters/routes conveniently packed

● Filters - route pre-filtering● Apis - Façades for sub-components/APIs● API - Where everything is bound

Page 25: Api presentation

Architecture - MVDispatcher / Façade (2)

Page 26: Api presentation

SDKs - rc-sdk-rubyLibrary to ease the integration of a possible external app with the API using language-specific HTTP/Oauth libraries.

● Two Entities - App and User● DSL for building REST Requests● Provides Responses Handling

Existing SDK only for Ruby (already used for restorm integration with the API)

Page 27: Api presentation

SDKs - rc-sdk-ruby (2)

Javascript SDK(???)(Java? PHP? Python? etc...)

Page 28: Api presentation

IntegrationProvides API access to external clients

● Handled in the Web App (Oauth flow)● Web App supplies resource owner

authorizations● API delivers access tokens

Page 29: Api presentation

Integration (2)

Page 30: Api presentation

Questions?