Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Resilient API Clients (SLUG)

Building Resilient API Clients (SLUG)

An introduction to building API clients and servers which can evolve independently. This talk will cover connecting to an API without upfront knowledge about any implementation details such as URIs, HTTP "verbs" and content types but instead an understanding of the semantic meaning of what the API does. By leaning controls on the fly, we can allow a client and server to change independently over time.

Kyle Fuller

June 25, 2015
Tweet

More Decks by Kyle Fuller

Other Decks in Technology

Transcript

  1. /v1

  2. /v2

  3. HAL { "_embedded": { "choices": [ { "_links": { "self":

    { "self": "/questions/1/choices/1" } }, "choice": "Swift", "votes": 2048 } ] } }
  4. Siren { "actions": [ { "name": "create", "fields": [ {

    "name": "question", "type": "text", "title": "Question" }, { "name": "choices", "type": "array[text]", "title": "Choices" } ], "method": "POST", "type": "application/json", "href": "/questions" } ] }
  5. func viewQuestion(question:Representor<HTTPTransition>) { println(question.attributes["question"]) if let choices = question.representors["choices"] {

    map(choices, viewChoice) } else { println("-> This question does not have any choices.") } if let delete = question.transitions["delete"] { // User may delete this question } }
  6. func viewChoice(choice:Representor<HTTPTransition>) { let text = choice.attributes["choice"] let votes =

    choice.attributes["votes"] println('-> \(text) (\(votes))') if let vote = choice.transitions["vote"] { // User may vote on this choice } }
  7. if let create = questions.transitions["create"] { // We may create

    a new question for attribute in create.attributes { // Creation takes `attribute.name` } } else { // Gracefully handle the lack of being able to create a question }
  8. ## Questions Collection [/questions] ### List All Questions [GET] +

    Relation: questions + Response 200 (application/json) + Attributes (array[Question])
  9. ### Create a New Question [POST] + Relation: create +

    Request (application/json) + Attributes + question (string, required) - The question + choices (array[string]) - A collection of choices. + Response 201 (application/json) + Attributes (Question)
  10. ## Question [/questions/{question_id}] + Attributes + question: `Favourite programming language?`

    (string, required) + published_at: `2014-11-11T08:40:51.620Z` (string) - An ISO8601 date when the question was published + choices (array[Choice], required) - An array of Choice objects
  11. ### Vote on a Choice [POST] This action allows you

    to vote on a question's choice. + Relation: vote + Response 201