Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

REST API

  • REST stands for Representational State Transfer.

  • Architectural style for designing web services.

  • REST services are stateless, which means that they do not maintain any state between requests. This makes them scalable and reliable.

  • For us they are mainly interfaces for information retrieval.

Accessing REST

  • REST services are based on the HTTP protocol, and they use a set of well-defined verbs to manipulate resources.

  • Some are open and free, some need an API key (free or subscription)

  • The four main verbs in REST are:

    • GET: Retrieve a resource.

    • POST: Create a resource.

    • PUT: Update a resource.

    • DELETE: Delete a resource.

Retrieving data (GET)

Creating data (POST)

  • For this example we assume there is an API server running locally.

  • We rely on the Flask framework (see flask_API.py) in the current folder.

    • Simple POST, GET, “UPDATE”, DELETE

  • Running a Flask instance from the terminal can be done similar to this:

conda activate tf_M1
python /Users/kristian/Documents/GitHub/IND320/D2Dbook/3_Data_sources/3_APIs/flask_API.py

Change data (UPDATE)

  • This command can mostly be exchanged with POST.

  • The Flask framework does not have a separate UPDATE (see implementation in flask_API.py).

Remove data (DELETE)

  • Let’s remove some data and try to read it again.

Exercise

  • Look at flask_API.py.

  • Add “try-except” to GET to return an empty JSON when an ‘id’ does not exist.

  • Are there other potential sources of error here?