Fetching the data
Fetching and storing data from the Laws.Africa Content API.
In this section
Working with the Laws.Africa Content API
Creating a management command to fetch data from the Content API
Using the Laws.Africa Content API
If you haven't already, follow the instructions at the Laws.Africa Developer Guide for setting up an account and getting your API token.
Visit https://api.laws.africa/v3/akn/za-cpt/.json to see the data that we'll be ingesting. Log into your Laws.Africa if necessary.
The https://api.laws.africa/v3/akn/za-cpt/.json 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.
Multiple points-in-time
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
'sexpression
has its ownurl
, 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.
Create a management command
Rather than fetching and saving data on the command line, let's write a Django management command that we can run from inside the app to fetch the data from the Laws.Africa Content API.
This command should do the following:
Start at the https://api.laws.africa/v3/akn/za-cpt/.json endpoint and work with the
results
list.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 aWork
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 usecreate
.
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 thepoints_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 theurl
for the expression.For the table of contents, append
/toc.json
to theurl
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:
Last updated