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

Building Resilient API Clients

Building Resilient API Clients

Kyle Fuller

June 18, 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. { "id": 1, "question": "Favourite programming language?", "choices": [ {

    "id": 1, "choice": "Swift", "votes": 128 }, { "id": 2, "choice": "Objective-C", "votes": 2 } ] }
  12. ### Vote on a Choice [POST] This action allows you

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