28. Application exercise: version 10
28.1. Introduction
In the examples of clients for the tax calculation server, the threads sent N requests sequentially if they had to process N taxpayers. The idea here is to send a single request encapsulating the N taxpayers. For each of them, the information [married, children, salary] must be sent. These can be sent as parameters:
- in the URL. This results in a long, meaningless URL;
- in the body of the HTTP request. We know that this body is hidden from the user using a browser;
In both cases, you can use a [GET] or [POST] request. We will use a POST request with the parameters embedded in the HTTP request body.
The client/server architecture has not changed:

28.2. The web server

The [http-servers/05] folder is initially created by copying the [http-servers/02] folder. We return to JSON exchanges between the client and the server. We have seen that switching from JSON to XML is very simple.
28.2.1. Configuration
The configuration [config, config_database, config_layers] remains the same as in previous versions. We won’t go over it again.
28.2.2. The main script [main]
The [main] script is identical to the one in the [http-servers/02] folder that we copied. Only one thing differs:
- line 2: the / URL is now accessed via a POST request;
28.2.3. The [index_controller]
The [index_controller] evolves as follows:
- Line 9: The controller receives:
- the client's request;
- the server configuration [config];
- lines 14–18: We retrieve the POST body. The parameters encapsulated in the HTTP request body can be encoded in various ways. We’ve already encountered one: [x-www-form-urlencoded]. Here, we’ll use another encoding: JSON;
- line 18: [request.data] retrieves the body of the HTTP request. Here, we retrieve text, and we know that this text is JSON representing a list of dictionaries [married, children, salary];
- lines 19–24: we retrieve this list of dictionaries;
- lines 22–24: if the JSON retrieval failed, we log the error;
- lines 26–28: if we find that the retrieved object is not a list or is an empty list, we log the error;
- lines 29–38: if a list was successfully retrieved, verify that it is indeed a list of dictionaries;
- lines 40–43: if an error occurred, we stop here and send an error response to the client;
- lines 45–69: we now check each of the dictionaries:
- they must contain the keys [married, children, salary];
- they must allow us to construct a valid [TaxPayer] object;
- lines 65–69: if an error is detected in a dictionary, it is added to that same dictionary under the key ‘error’;
- lines 72–75: the dictionaries containing errors have been collected in the list [list_errors]. If this list is not empty, then it is sent in an error response to the client;
- line 77: at this point, we know we can create a list of objects of type [TaxPayer] from the body of the request sent by the client;
- lines 84–91: We process the list of received dictionaries;
- line 86: from a dictionary, we create a [TaxPayer] object;
- line 89: we calculate the tax for this [TaxPayer];
- line 91: we know that [taxpayer] has been modified by the tax calculation. We convert it into a dictionary and add it to a list of results;
- line 93: this list of results is sent to the client;
28.2.4. Server Testing
We will test the server using a Postman client:
- We start the web server, the DBMS, and the mail server [hMailServer];
- We launch the Postman client and its console (Ctrl-Alt-C);

- in [1]: we send a [POST] request;
- in [2]: the server’s URL;
- in [3]: the body of the HTTP request;
- in [5]: we specify that this body should be sent as a JSON string;
- in [4]: we switch to [raw] mode to be able to copy and paste a JSON string;
- in [6]: paste the JSON string taken from one of the [results.json] files for the different versions. Then, for each taxpayer, keep only the properties [married, salary, children];

- in [7], we look at the HTTP headers that the Postman client will send to the server;
- in [8], we see that it will send a [Content-Type] header indicating that the request contains a JSON-encoded body. This is due to the choice made in [5] earlier;

- in [9-12]: we include the credentials expected by the server in the request;
We send this request. The server's response is as follows:

- in [3], we received JSON;
- in [4], the taxpayers' tax;
Let’s examine the client/server dialogue that took place in the Postman console (Ctrl-Alt-C):
The Postman client sent the following text:
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 03c4aa28-5a5d-4bb5-ac51-7ad51968c71d
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 824
[
{
"marié": "oui",
"enfants": 2,
"salaire": 55555
},
{
"marié": "oui",
"enfants": 2,
"salaire": 50000
},
{
"marié": "oui",
"enfants": 3,
"salaire": 50000
},
{
"marié": "non",
"enfants": 2,
"salaire": 100000
},
{
"marié": "non",
"enfants": 3,
"salaire": 100000
},
{
"marié": "oui",
"enfants": 3,
"salaire": 100000
},
{
"marié": "oui",
"enfants": 5,
"salaire": 100000
},
{
"marié": "non",
"enfants": 0,
"salaire": 100000
},
{
"marié": "oui",
"enfants": 2,
"salaire": 30000
},
{
"marié": "non",
"enfants": 0,
"salaire": 200000
},
{
"marié": "oui",
"enfants": 3,
"salaire": 200000
}
]
- line 1: the POST request to the server;
- line 2: the HTTP authentication header;
- line 3: the client tells the server that it is sending a JSON string and that this string is 824 bytes long (line 11);
- lines 13–69: the JSON body of the request;
The server responded with the following text:
HTTP/1.0 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 1461
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:16:34 GMT
{"réponse": {"results": [{"marié": "oui", "enfants": 2, "salaire": 55555, "impôt": 2814, "surcôte": 0, "taux": 0.14, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 2, "salaire": 50000, "impôt": 1384, "surcôte": 0, "taux": 0.14, "décôte": 384, "réduction": 347}, {"marié": "oui", "enfants": 3, "salaire": 50000, "impôt": 0, "surcôte": 0, "taux": 0.14, "décôte": 720, "réduction": 0}, {"marié": "non", "enfants": 2, "salaire": 100000, "impôt": 19884, "surcôte": 4480, "taux": 0.41, "décôte": 0, "réduction": 0}, {"marié": "non", "enfants": 3, "salaire": 100000, "impôt": 16782, "surcôte": 7176, "taux": 0.41, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 3, "salaire": 100000, "impôt": 9200, "surcôte": 2180, "taux": 0.3, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 5, "salaire": 100000, "impôt": 4230, "surcôte": 0, "taux": 0.14, "décôte": 0, "réduction": 0}, {"marié": "non", "enfants": 0, "salaire": 100000, "impôt": 22986, "surcôte": 0, "taux": 0.41, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 2, "salaire": 30000, "impôt": 0, "surcôte": 0, "taux": 0.0, "décôte": 0, "réduction": 0}, {"marié": "non", "enfants": 0, "salaire": 200000, "impôt": 64210, "surcôte": 7498, "taux": 0.45, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 3, "salaire": 200000, "impôt": 42842, "surcôte": 17283, "taux": 0.41, "décôte": 0, "réduction": 0}]}}
- line 1: the request was successful;
- line 2: the body of the server's response is a JSON string. It is 1461 bytes long (line 3);
- line 7: the server's JSON response;
Now let’s test some error cases.
Case 1: we send anything
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 47652706-9744-46a0-a682-de010e5406c0
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 3
abc
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 125
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:43:27 GMT
{"réponse": {"erreurs": ["le corps du POST n'est pas une chaîne jSON valide : Expecting value: line 1 column 1 (char 0)"]}}
- line 13: the string [abc] was sent, which is not a valid JSON string (line 3);
- line 15: the server responds with a 400 error code;
- line 21: the server's JSON response;
Case 2: Let’s send a valid JSON string that is not a list
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 03b64735-9239-47b3-b92d-be7c9ebc7559
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 17
{"att1":"value1"}
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 97
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:50:11 GMT
{"réponse": {"erreurs": ["le corps du POST n'est pas une liste ou alors cette liste est vide"]}}
Case 3: Let's send a JSON string that is a list whose elements are not all dictionaries
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: a1528a5f-777c-413f-b3be-7d4e9955b12a
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 7
[0,1,2]
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 85
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:52:10 GMT
{"réponse": {"erreurs": ["le corps du POST doit être une liste de dictionnaires"]}}
Case 4: Let’s send a list of dictionaries with a dictionary that doesn’t have the correct keys
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: ba964d81-c9d9-46ff-a521-b4c4e5639484
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 19
[{"att1":"value1"}]
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 112
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:54:33 GMT
{"réponse": {"erreurs": [{"att1": "value1", "erreur": "MyException[2, la clé [att1] n'est pas autorisée]"}]}}
Case 5: Let's send a list of dictionaries with a dictionary containing missing keys:
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 98aec51d-f37d-4c14-81cd-c7ffcbbcdc65
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 18
[{"marié":"oui"}]
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 125
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:56:40 GMT
{"réponse": {"erreurs": [{"marié": "oui", "erreur": "le dictionnaire doit inclure les clés [marié, enfants, salaire]"}]}}
Case 6: Let’s send a list of dictionaries with one dictionary containing the correct keys but some with incorrect values:
POST / HTTP/1.1
Authorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 3083e601-dee4-4e15-9ea4-fc0328d0fcf0
Host: localhost:5000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 46
[{"marié":"x", "enfants":"x", "salaire":"x"}]
HTTP/1.0 400 BAD REQUEST
Content-Type: application/json; charset=utf-8
Content-Length: 167
Server: Werkzeug/1.0.1 Python/3.8.1
Date: Tue, 28 Jul 2020 07:59:32 GMT
{"réponse": {"erreurs": [{"marié": "x", "enfants": "x", "salaire": "x", "erreur": "MyException[31, l'attribut marié [x] doit avoir l'une des valeurs oui / non]"}]}}
28.3. The web client

The [http-clients/05] file (version 10) is initially obtained by copying the [http-clients/02] file (version 7). It is then modified.
28.3.1. The [dao] layer
The [dao] layer is implemented by the following [ImpôtsDaoWithHttpClient] class:
- lines 1–26: the code remains the same as in version 7 and other versions;
- lines 27–70: a new method [calculate_tax_in_bulk_mode] is introduced, whose purpose is to calculate the tax for a list of taxpayers;
- line 28: [taxpayers] is this list of taxpayers;
- lines 31–39: we convert a list of [TaxPayer] objects into a list of dictionaries using the [map] function;
- lines 34–38: the lambda function used transforms an object of type [TaxPayer] into a dictionary of type [dict] with only the keys [married, children, salary]. To do this, we use the parameter named [included_keys] from the [BaseEntity.asdict] method. Note that to determine the exact names of the properties to include in the [excluded_keys, included_keys] parameters, you must use the predefined dictionary [taxpayer.__dict__];
- lines 41–48: connect to the server and retrieve its HTTP response;
- lines 44, 48:
- We use the static method [requests.post] to send a POST request to the server;
- the parameter named [json] is used to indicate that the POST body is a JSON string. This will have two consequences:
- the object assigned to the named parameter [json], in this case a list of dictionaries, will be converted to a JSON string;
- the header
will be included in the POST’s HTTP headers;
- line 59: the server’s JSON response is deserialized into the [result] dictionary;
- lines 61–63: any error sent by the server is handled;
- line 65: the tax calculation results are in a list of dictionaries;
- lines 67–69: these results are used to update the initial list of taxpayers [taxpayers] originally received by the method on line 28;
- line 70: here, the initial list of taxpayers has been updated with the tax calculation results;
28.3.2. The main script [main]
The main script [main] evolves as follows: only the function [thread_function] executed by the threads created by the client is modified. The rest of the code remains unchanged.
- lines 9–10: whereas previously we had a loop that passed each taxpayer in turn to the [dao.calculate_tax] method, here we make a single call to the [dao.calculate_tax_in_bulk_mode] method, passing all taxpayers to it;
28.3.3. Client Execution
We will compare the execution times of versions:
- 7, where each taxpayer is the subject of an HTTP request;
- 10 (this one), where taxpayers are grouped into a single HTTP request;
First, version 6. To compare the two versions, we set the server’s [sleep_time] property to zero so that there is no forced waiting for threads. The client logs are as follows:
2020-07-28 14:20:45.811347, Thread-1 : début du thread [Thread-1] avec 4 contribuable(s)
2020-07-28 14:20:45.811347, Thread-1 : début du calcul de l'impôt de {"id": 1, "marié": "oui", "enfants": 2, "salaire": 55555}
…
2020-07-28 14:20:45.913065, Thread-3 : fin du calcul de l'impôt de {"id": 11, "marié": "oui", "enfants": 3, "salaire": 200000, "impôt": 42842, "surcôte": 17283, "taux": 0.41, "décôte": 0, "réduction": 0}
2020-07-28 14:20:45.913065, Thread-3 : fin du thread [Thread-3]
The client's execution time to calculate the tax for 11 taxpayers is therefore [913065-811347= 101718], i.e., approximately 102 milliseconds.
Let’s do the same with version 10 (server sleep_time set to zero). The client logs are then as follows:
2020-07-28 14:25:31.871428, Thread-1 : début du calcul de l'impôt des 4 contribuables
2020-07-28 14:25:31.873594, Thread-2 : début du calcul de l'impôt des 3 contribuables
2020-07-28 14:25:31.877429, Thread-3 : début du calcul de l'impôt des 3 contribuables
2020-07-28 14:25:31.882855, Thread-4 : début du calcul de l'impôt des 1 contribuables
2020-07-28 14:25:31.930723, Thread-2 : {"réponse": {"results": [{"marié": "non", "enfants": 3, "salaire": 100000, "impôt": 16782, "surcôte": 7176, "taux": 0.41, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 3, "salaire": 100000, "impôt": 9200, "surcôte": 2180, "taux": 0.3, "décôte": 0, "réduction": 0}, {"marié": "oui", "enfants": 5, "salaire": 100000, "impôt": 4230, "surcôte": 0, "taux": 0.14, "décôte": 0, "réduction": 0}]}}
….
2020-07-28 14:25:31.935958, Thread-4 : fin du calcul de l'impôt des 1 contribuables
2020-07-28 14:25:31.935958, Thread-1 : fin du calcul de l'impôt des 4 contribuables
The client’s execution time to calculate the tax for 11 taxpayers is therefore [935958-871428= 64530 ns] (line 8 – line 1), i.e., approximately 65 milliseconds. This new version 10 thus delivers a performance gain of approximately 57% over version 7.
28.3.4. Tests of the client’s [dao] layer

The [TestHttpClientDao] test for the client in version 10 is very similar to that in version 7:
- line 14: instead of calling the [dao.calculate_tax] method, we call the [dao.calculate_tax_in_bulk_mode] method, passing it a list (indicated by the square brackets) of taxpayers;
All tests pass.