Fetching the data
Fetching and storing data from the Laws.Africa Content API.
Last updated
Fetching and storing data from the Laws.Africa Content API.
Last updated
Working with the Laws.Africa Content API
Creating a management command to fetch data from the Content API
If you haven't already, follow the instructions at the for setting up an account and getting your API token.
Visit to see the data that we'll be ingesting. Log into your Laws.Africa if necessary.
The endpoint returns a list of all the by-laws for the City of Cape Town, in South Africa. Each work is a by-law.
The items in the results
array are the most recent expression of each by-law. Each item in the array has the complete information of both the expression and the work.
Legislation changes over time (also called "points in time"), and may also be available in different languages. The content API returns the most recent (latest) available version, in the country's default language (English in this example).
Other points-in-time (expressions) may also be available. These are listed in the points_in_time
attribute. It contains the dates and expressions available at those dates. There will only be different expressions at the same date if there are different languages. Each entry includes a URL with the full details of that particular expression.
Each point_in_time
's expression
has its own url
, to which you can append .json
to fetch the JSON details of the expression.
If you don't append .json
, it will return the XML of the expression.
This command should do the following:
The API token needs to be provided each time an API call is made.
The results may be paginated when getting all works in a place, so it's important to check for next
in the response.
For each result, create a Work
object in the database using the data in the result.
In the example below, we use the update_or_create
method, in case a Work
object with the given FRBR URI already exists in the database. This allows us to run the command multiple times. You may wish to simply use create
.
Next, create the relevant Expression
objects, with the related work being the one that has already been created for the current result.
A work can have multiple expressions, or no expressions.
You need to look at the list of expressions
inside each entry in the points_in_time
list to get the expression details.
The metadata for each expression is listed in the place's results
, but the content of each expression and its table of contents require separate API calls:
For the HTML content, append .html
to the url
for the expression.
For the table of contents, append /toc.json
to the url
for the expression.
Create the management/commands/
directories inside reader
:
This command takes your API key as a parameter and stores the content from the API in the database:
Rather than fetching and saving data on the command line, let's write a that we can run from inside the app to fetch the data from the Laws.Africa Content API.
Start at the endpoint and work with the results
list.