Build a small FastAPI-based microservice that integrates with the Star Wars API (SWAPI) and returns a list of all character names.
-
Implement the logic in
services.py
:-
Use the SWAPI people endpoint
-
Collect all names and return them as a JSON list in this format:
{ "names": ["Luke Skywalker", "Darth Vader", "..."] }
-
-
Add Unit Tests in
tests/test_main.py
:- Use
pytest
andpytest-httpx
- At least one test should check that the
/people
endpoint works as expected
- Use
-
Containerize the application:
- Write a
Dockerfile
for the service - Create a
docker-compose.yml
file that builds the image and exposes the app on port8000
- Write a
-
Test usage without container:
This can be used for testing before containerization
uvicorn app.main:app --reload
After starting the service, the following request:
GET http://localhost:8000/people
Should return:
{
"names": ["Luke Skywalker", "Darth Vader", "..."]
}
- Async HTTP calls (using e.g.
httpx
) - Proper error handling if external API fails
- Unit tests that pass
- Docker setup that runs the app and allows testing
- Avoid hardcoding