Exercises part 1
Exercise 1 - Songs
Create a new file song.py
. In this file, create a dictionary which contains your favourite song. The dictionary should contain both the name of the artist and the title of the song. Next, ask one of your neighbours what his/her favorite song is. Create a new dictionary containing this song (artist + title).
Create a GET request that will return your favourite song and which can be addressed in the following manner:
http://localhost:8000/song/me
The returned JSON should have the following structure:
{
"artist": "Against The Current",
"title": "Weapon"
}
Create another GET request that will return the favourite song of your neighbour in the same JSON structure. You should address it using the following url:
http://localhost:8000/song/neighbour
The returned JSON should have the following structure, similar to the structure of the first GET request:
{
"artist": "The Corrs",
"title": "Breathless"
}
Make sure that each GET request returns a different song.
Exercise 2 - Random number generator
Create a new file randomizer.py
. This API will contain several endpoints which give functionality for random numbers.
Create a first GET request with the following endpoint:
/percentage
The result of this GET request will be a JSON object that gives a random number between 0 and 100 (included). For example:
{
"percentage": 95
}
Create a second GET request. For this request, the upper and lower limit of the random number will no longer be hard coded. Instead, give the user or application that is addressing your API the opportunity to make use of his/her own limits. For example:
/percentage/10/50
The endpoint shown above will give a random number between 10 and 50 (included). The returned JSON should have the same structure as in the first GET request.
Make sure that the upper limit is greater than the lower limit. If that is not the case, return the following error message:
{
"error": "The upper limit must be greater than the lower limit!",
"lower limit": 57,
"upper limit": 50
}
Please notice that the upper and lower limit are passed in the result as well. So for the example above, the following url would be used:
/percentage/57/50
Create a third endpoint in which you can not only pass the limits, but also the amount of random numbers that should be returned.
In the example that follows, the lower limit is 10, the upper limit is 50 and the amount of random numbers to be generated is 3.
/percentage/10/50/3
The result could be as follows:
{
"percentages": [
11,
14,
47
]
}
Make sure that the amount of numbers to generate (third parameter) is an integer. If not, the default data conversion error of FastAPI should be thrown:
{
"detail": [
{
"loc": [
"path",
"amount"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
Make sure that the upper limit is greater than the lower limit, similar to the check you performed in the previous endpoint.
Make sure that the amount of random numbers is greater than 0. Otherwise, return the following error:
{
"error": "The amount must be greater than 0!",
"amount": -3
}
Tips
Make your code as efficient as possible: first check for errors and only perform other logic after all possible errors have been checked.
Exercise 3 - Device information
Create a new file deviceinfo.py
. In the API that you will program for this exercise, you will read out information from the device the API is running on, similar to exercise 5 of the previous chapter.
You will create several endpoints, which will return specific information from the device (e.g. platform, processor, etc.).
Info
The platform, release, version, instruction set and processor can be retrieved from the device using the ‘platform’ library in Python. More information about this library can be found in its documentation: https://docs.python.org/3/library/platform.html
Platform
Create a GET request for the following endpoint:
/device/platform
This endpoint should return the platform of the device on which the API is running, together with the moment on which this information was retrieved.
Info
To get the current date and time (now), check the documentation of Python for the datetime library: https://docs.python.org/3/library/datetime.html
The structure of the returned information should be as follows:
{
"platform": "Windows",
"datetime": "2022-09-20T13:15:55.972538"
}
Platform, release and version
Create a GET request for the following endpoint, where the parameter can be either true
or false
:
/device/platform/true
This endpoint should return the same information as the previous endpoint, but with some additional information.
In case the value of the parameter is true
, the release and version of the device should be added to the returned JSON. In case the value is false
, only the release should be added.
For example, in case the parameter is true
:
{
"platform": "Windows",
"release": "10",
"version": "10.0.19044",
"datetime": "2022-09-20T13:32:51.777666"
}
In case the parameter is false
:
{
"platform": "Windows",
"release": "10",
"datetime": "2022-09-20T13:33:22.480324"
}
In case another value is passed as parameter (e.g. test
):
{
"detail": [
{
"loc": [
"path",
"show_version"
],
"msg": "value could not be parsed to a boolean",
"type": "type_error.bool"
}
]
}
Processor
Create a new endpoint /device/processor
which will give information about the processor, as shown below.
{
"processor": "Intel64 Family 6 Model 140 Stepping 1, GenuineIntel",
"datetime": "2022-09-20T13:38:44.157265"
}
Interfaces
Create a new endpoint /device/interfaces/all
which will return all virtual and physical interfaces of the device. Note that only interfaces with an IP address are included in the list. Try to find out yourself how you can filter the necessary information.
Info
All virtual and physical network interfaces can be found using the net_if_addrs() function of the psutil library.
{
"interfaces": [
{
"interface": "Ethernet 2",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "LAN-verbinding* 1",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "LAN-verbinding* 2",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "Wi-Fi",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "Bluetooth-netwerkverbinding",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "vEthernet (WSL)",
"IP address": "THE IP ADDRESS",
"netmask": "THE NETMASK"
},
{
"interface": "Loopback Pseudo-Interface 1",
"IP address": "127.0.0.1",
"netmask": "255.0.0.0"
}
],
"datetime": "2022-09-20T13:40:37.973822"
}
Create another endpoint in which it is possible to pass the index of the interface you want to retrieve.
A possible way of addressing that endpoint would be:
/device/interfaces/6
Considering the JSON given back by the previous endpoint, index 6 would return the following information:
{
"interface": "Loopback Pseudo-Interface 1",
"IP address": "127.0.0.1",
"netmask": "255.0.0.0"
}
In case the index does not exist (below 0 or too high), the following message should be returned:
{
"error": "invalid index"
}