From the course: Build REST APIs with FastAPI
Unlock this course with a free trial
Join today to access over 24,300 courses taught by industry experts.
Solution: Query logs - Python Tutorial
From the course: Build REST APIs with FastAPI
Solution: Query logs
Let's take a look at my solution. So here is the instructions. So I'm using the built-in CSV module to encode CSVs. I'm getting datetime, so I can export datetime. HTTPStatus, StringIO, and for FastAPI, I'm getting request and response, and using Pydantic for the base model. And I'm importing the local DB, so I query the database. So I'm defining a log which has a level, time, and a message. And long response which is a count, offset, and logs which is a list of logs. And this is for the JSON replies. So I'm saying get logs and then I need the request. So I need the headers. So I need the request. The count integer defaults to 100 and the offset integer default to zero. Now I'm validating the count and the offset. I'm getting the mime type from the request headers, and if the mime_type is */*, this means the client didn't send anything. And then I'm defaulting to JSON. Then if it's not one of these mime types, I'm returning a status of bad request. The records are a list of the DB…