Building Resilient API Clients

Building Resilient API Clients

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

June 18, 2015
Tweet

Transcript

  1. 2.
  2. 3.
  3. 4.
  4. 6.
  5. 7.
  6. 8.
  7. 9.
  8. 10.
  9. 11.
  10. 21.

    /v1

  11. 22.

    /v2

  12. 25.
  13. 26.
  14. 31.
  15. 40.
  16. 41.
  17. 57.
  18. 59.

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

    { "self": "/questions/1/choices/1" } }, "choice": "Swift", "votes": 2048 } ] } }
  19. 60.
  20. 62.

    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" } ] }
  21. 65.
  22. 66.
  23. 69.
  24. 76.

    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 } }
  25. 77.

    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 } }
  26. 79.

    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 }
  27. 80.
  28. 84.
  29. 87.
  30. 88.
  31. 89.
  32. 90.
  33. 92.

    ## Questions Collection [/questions] ### List All Questions [GET] +

    Relation: questions + Response 200 (application/json) + Attributes (array[Question])
  34. 93.

    ### 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)
  35. 94.

    ## 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
  36. 95.

    { "id": 1, "question": "Favourite programming language?", "choices": [ {

    "id": 1, "choice": "Swift", "votes": 128 }, { "id": 2, "choice": "Objective-C", "votes": 2 } ] }
  37. 98.

    ### Vote on a Choice [POST] This action allows you

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