Using Flask, build the three endpoints described below:
get_logs — expose an endpoint that returns stored log entries. It must accept a date filter and paginate the results.send_logs — provide an endpoint that accepts batches of log entries. Validate the incoming structure and return useful errors for malformed payloads.poll_for_command — create an endpoint clients can poll to learn about new log-processing commands. It should reply with any pending command or indicate that none is available.Implement the code for all three endpoints. Think through edge cases such as invalid dates, missing pagination parameters, malformed batch payloads, and repeated polling. Include sample requests and expected responses for each endpoint.
The examples below show the expected request/response shape for each endpoint.
get_logs
Input:
GET /get_logs?date=2023-01-01&page=2 HTTP/1.1
Output:
HTTP/1.1 200 OK
Content-Type: application/json
{
"date": "2023-01-01",
"page": 2,
"page_size": 10,
"logs": [
{
"id": 11,
"timestamp": "2023-01-01T08:15:00Z",
"level": "INFO",
"message": "User login"
}
]
}
Explanation: the second page of 2023-01-01 logs is returned; if no further entries exist, logs can be [].
send_logs
Input:
POST /send_logs HTTP/1.1
Content-Type: application/json
{
"logs": [
{
"timestamp": "2023-01-01T09:00:00Z",
"level": "ERROR",
"message": "DB timeout"
}
]
}
Output:
HTTP/1.1 200 OK
Content-Type: application/json
{
"received": 1,
"status": "accepted"
}
Explanation: the batch contains one valid log entry, so it is accepted; malformed or missing fields should produce an HTTP 4xx response.
poll_for_command
Input:
GET /poll_for_command HTTP/1.1
Output:
HTTP/1.1 200 OK
Content-Type: application/json
{
"command": {
"command_id": 7,
"type": "process_logs",
"params": {
"date": "2023-01-01"
}
}
}
Explanation: this is the next queued command; when the queue is empty, command can be null.