In our standardization policy, we defined a common set of pagination parameters.
Moreover we stated that responses should always be enclosed in json objects, eg:
GET /timezones
{
"entries": [ "you", "can", "always", "add", "new", "keys" ]
}
string, number or array, because GET /dont-do-this
[ "you can't", "extend them"]
get /timezonesWe want to provide a /timezones path listing all the timezones supported
by get /echo
Our goal is the following:
/datetime/v1/timezones the API will return
the supported timezones in pytz.all_timezones;{
"entries": [ "Europe/Rome", "UTC", .. ],
"count": 5,
"offset": 10
}
200Remember: pagination should be implemented using a common set of parameters to use. Our choice is:
Edit the ex-08-pagination-ok.yaml and write the timezones specifications:
1- define the new Timezones schema to be used in the response;
2- define the new /timezones path supporting the get method:
summary and description fields3- get /timezones possible responses are:
200 returning a Timezones in json format, with a complete example for mocks429 and 503 returning a problem+json4- this operation is not authenticated
5- don't forget operationId: get_timezones !
Hint: feel free to reuse as much yaml code as possible.
Run your spec in the terminal and check that it properly returns the mock objects.
Use
connexion run --mock all /code/notebooks/oas3/ex-08-pagination-ok.yaml
In [3]:
# Use this cell to test the output
!curl http://localhost:5000/datetime/v1/timezones -vk
OAS3 allows to specify path parameters:
{continent}parameters with the remaining details.REMEMBER: path parameters are always required so you must define a new path and a new operationId: get_timezones_by_continent
paths:
/timezones/{continent}:
get:
...
parameters:
- name: continent
in: path
required: true
schema:
type: string
/timezones:
...
definition without path-parameters
...
path parameter to /timezonesLet's add a continent path parameter to /timezones:
#/components/parameters/continent_path parameter definition;continent_path query parameter to get /timezones/{continent} path checking
the official OAS 3.0.2 documentation404 Not Found response in case the continent is not presentFinally, check that you can run the spec.
connexion run --mock all /code/notebooks/oas3/ex-08-pagination-ok.yaml
In [3]:
# Use this cell to test your api
Let's implement the get_timezones operation in api.py such that:
limit and offset parameters;Timezones object containing all the timezones in pytz.all_timezones between offset and offset+limit
In [ ]:
# Check that default works
!curl http://localhost:5000/datetime/v1/timezones -kv
Let's implement the get_timezones_by_continent operation in api.py such that:
get_timezones behavior;returns a Timezones object containing all the timezones in the given continent, eg:
Europe -> [ "Europe/London", "Europe/Rome", ... ]
In [ ]:
### Exercise solution
In [14]:
!grep '^def get_timezones' oas3/api-solution.py -A20 -B1
Now run the spec in a terminal using
cd /code/notebooks/oas3/
connexion run /code/notebooks/oas3/ex-08-pagination-ok.yaml
In [6]:
render_markdown(f'''
Play a bit with the [Swagger UI]({api_server_url('ui')})
and try making a request!
''')
In [22]:
# Use out-of-bound offset
!curl http://localhost:5000/datetime/v1/timezones?offset=800 -kv
# Pick in the middle
!curl 'http://localhost:5000/datetime/v1/timezones?offset=450&limit=2' -kv
# Pick in the middle
!curl 'http://localhost:5000/datetime/v1/timezones/Europe?limit=2' -kv
In [ ]: