RESTful API design - HenglinShi/PWP GitHub Wiki

Important information for Deadline 3

‼️  This chapter should be completed by Deadline 3 (see course information at Lovelace)


📑  Chapter summary In this chapter, the students must design the RESTful API.The minimum requirements are summarized in the Minimum Requirements section of the Project Work Assignment. Note that if you do not meet Minimum Requirements this section wont be evaluated.

SECTION GOALS:

  • Understand REST principles
  • Understand connectedness and/or hypermedia
  • Design a small API
  • Write API documentation

You have two options:

  1. Implement the API using a non-hypermedia format (RESTful CRUD). In this case, it is mandatory that all your resources are connected. You cannot get full points in this section if you do not design your API using an hypermedia format
  2. Using an hypermedia format. Lots of examples provided in Exercise 2. You can get full points. In this case you need to clearly include in the documentation a profile with link relations and semantic descriptors.

The recommended step-by-step process  is:

  1. Identify all the resources to be exposed by the Web API. To that end, students should make an abstraction of the concepts defined in section 1. Be aware that a one-to-one mapping between the resources and the concepts defined in section 1 is not usually the best option. Sometimes it is better to pack several concepts in the same resource.
  2. Establish the resource hierarchy and assign a URI to each resource.
  3. Establish relations and possible actions among resources. Create the state diagram of your API.
  4. Expose each one of the resources to the uniform interface.
  5. Design the resource representation using adequate format.
    1. Define the media type and its extension (if any)
    2. Define the profiles (if you are using an hypermedia format). Try to reuse existing profiles as much as possible. For instance, utilize schemas defined in schemas.org.
    3. Do not forget to include the format of the requests. If you are using hypermedia format and the media type does not define the format of the requests, they must be defined in the profile. Otherwise, the request formats must be defined in the documentation.
  • Define protocol attributes: headers, possible response codes ... must be clearly specified both for requests and responses.
  • Define the error conditions. When errors are triggered?
    • Define the format of each HTTP error response, including message body, status code and headers. 
    • It is recommended to use a hypermedia type for the response. This blog post contains some good recommendations.


  • ✔️     Chapter evaluation (max 25 points) You can get a maximum of 25 points after completing this section. More detailed evaluation is provided after each heading.

    RESTful API design

    Resources and relations


    📑  Content that must be included in the section
    1. Fill the table below with a description of the API resources
    2. Include a state diagram of your application, in which each resource is a state. Describe also the state transitions. To build this diagram you should reuse the diagram created in DL1. You can use online tools such as draw.io or lucidchart to create the diagrams. You have an example in the following image


    ✔️     Evaluation criteria(max 2.5 points) You can get a maximum of 2.5 points after completing this section.
    • Resource table with URLs and short descriptions is provided: 0.5
    • State diagram with transitions exists (see below): 0.5
    • State diagram follows relationships from DL1: 0.5
    • State diagram is correct, states make sense, transitions are clear and all possible transitions documented: 1.0

    ✏️ This table might serve you as a guide

    Resource name Resource url Resource description
    accounts collection /api/accounts/
    account item /api/accounts/{account}
    jobs collection /api/jobs/
    jobs by account /api/accounts/{account}/jobs/
    job item /api/accounts/{account}/jobs/{job}
    results by job /api/accounts/{account}/jobs/{job}/results/
    result item /api/accounts/{account}/jobs/{job}/results/{result}

    ✏️ Draw here your state machine diagram API map

    Uniform interface


    📑  Content that must be included in the section Fill the following table with a description of how your resources are exposed to the uniform interface (GET, PUT/PATCH, POST and DELETE methods). You must describe the action executed in each request. For example, a GET request to the URL /messages/{message_id} "gets the body and the title of a specific message".


    ✔️     Evaluation criteria(max 0.75 point) You can get a maximum of 0.75 points after completing this section.
    • The uniform interface shows all possible requests and actions are described for each: 0.75

    ✏️ This table might serve you as a guide

    Resource url GET PUT PATCH POST DELETE
    accounts collection /api/accounts/ gets the list of all accounts adds a new account
    account item /api/accounts/{account} gets the object of an account edits an account deletes an account
    jobs collection /api/jobs/ gets the list of all jobs
    jobs by account /api/accounts/{account}/jobs/ gets the list of all jobs associated with an account adds a new job for an account
    job item /api/accounts/{account}/jobs/{job} gets the object of the job edits a job deletes a job
    results by job /api/accounts/{account}/jobs/{job}/results/ gets the list of all results associated with a job adds a new result for the associated job
    result item /api/accounts/{account}/jobs/{job}/results/{result} gets the object of the result edits a result delets a result

    API design


    📑  Content that must be included in the section

    Use any of the tools presented in Exercise 2 (e.g. Apiary) to document the API. Follow the format specified in that exercise also. You can take the TVflix service REST API or Cryptotrading API(created by previous year students) as a model.

    For all resources you must cover:

    • The possible HTTP methods exposed by this resource
    • The headers in the request and responses
    • The media type utilized (in the response Content-Type header). If you are utilizing your own media-type you must describe it in the section Own media type implementation.
    • If you are using an hypermedia type you must provide the profile utilized, including.
      • Link relations. Include methods and format of the requests if they are defined in the media type. Use as much as possible IANA defined relations.
      • Semantic descriptors. If you utilize a descriptor used in some other profile (e.g. schema.org) provide the link.
      • If you are extending other profiles, do not forget to link to the extended profile.
    • The format of the HTTP response body, providing a clear example. If necessary, comment the example.
    • The format of the HTTP request body (just for PUT/POST), providing a clear example. If necessary, comment the example.
    • The error conditions, status code and format of the error response, providing a clear example.


    ✔️     Evaluation criteria(max 13.75 points) You can get a maximum of 13.75 points in this section:
    • You are using a hypermedia API: 3.0
    • Each request has the correct media type: 0.5
    • The media type is used according to its specification: 1.0
    • Link relations are described (either in the profile for hypermedia types, or in the resource documentation for the CRUD approach): 1.0
    • Attributes are clearly explained ( either in the profile - for hypermedia types - or in the resource documentation - for the CRUD approach -): 1.0
    • Examples are provided for each request: 0.5
    • Examples provided for each request do not contain errors: 0.5
    • Examples are provided for each response: 0.75
    • Examples provided for each response do not contain errors: 1.0
    • Examples includes error responses 0.25
    • Examples provided in error responses do not contain errors 0.5
    • Examples provide all possible/reasonable error responses for each method 0.5
    • Examples have correct headers: 0.5
    • Profiles are linked in each response (only for hypermedia APIs, CRUD implementation receive 0 points in this section): 0.5
    • Examples use correct status codes: 1.25
    • Design is coherent: 1.0

    ✏️ Include here the link to the hypermedia API documentation https://github.com/HenglinShi/PWP/blob/master/apiary.apib

    REST conformance


    📑  Content that must be included in the section Explain briefly how your API meets REST principles. Focus specially in the four principles: Addressability, Uniform interface, Connectedness, Statlessness


    ✔️     Evaluation criteria(max 8.0 points) You can get a maximum of 8.0 points in this section:
    • The justification is clear and coherent, and shows an understanding of REST principles: 2.0
    • The Api is addressable (no errors regarding addressability): 1.0
    • The Api uses correctly the uniform interface: 1.0
    • The Api does not hold state in the server: 0.5
    • The different resources of the API are connected, that is there are not isolated resources: 2.0
      • isolated resource is a resource that either is not linked to from anywhere, or doesn't contain links itself
    • Protocol semantics are clearly provided either by the response or by the profile (If you are using a CRUD API you won't get points in this section): 1.5

    ✏️ Write here your text

    Adressibility: Every object in the system can be adressed through a unique identifier. For example account, job, and result can be adressed by unique identifiers.

    Uniform interface: All resources will be access and manipulated by restricted methods, i.e. GET, POST, PUT, and DELETE methods.

    Connectedness: All resources are connected. For example the account connects to jobs, and a job connects to results.

    Statelessness: The server will no store any information about the client, and the client will also not store information abou the sever. At every communication, all required information will be provided to the server, and server will return all information in the response. No other information will be stored in both ends.

    Resources allocation

    Task Student Estimated time
    Resources Description and API map Henglin SHI 2 hours
    Uniform Interface Description Amine ERRAFA 1 hour
    API Document on Accounts Collection, Account Item, and Job Collection resources Amine ERRAFA 10 hours
    API Document on Jobs by Account, Job Item, Result by Job, and Result Item resources Henglin SHI 10 hours
    ⚠️ **GitHub.com Fallback** ⚠️