Demo #3B: Producer and Consumer with Kafka with FastAPI
Now, I want to have a input to specify the position of the players. For example, I want to create 5 goalkeepers, rather than 5 random players. How would I achieve that?
Here's what you need to modify in your previous code to achieve this:
-
Modify the Command Class: Update the
CreatePeopleCommandclass in yourcommands.pyto include a field for the player's position. If you don't have a way to specify the position in the current structure, this is the step you'll need to take. -
Update the API Endpoint: Modify the
create_soccer_playersfunction inmain.pyto use the specified position instead of randomly selecting one.# main.py @app.post('/api/soccer_players', status_code=201, response_model=List[SoccerPlayer]) async def create_soccer_players(cmd: CreatePeopleCommand): ... # Remove the positions list, as you'll use the position from the cmd parameter # positions = ["Forward", "Midfielder", "Defender", "Goalkeeper"] # Generate random soccer player data based on the given count. for _ in range(cmd.count): soccer_player = SoccerPlayer( id=str(uuid.uuid4()), name=faker.name(), position=cmd.position, # Use the specified position. speed=random.randint(0, 10), stamina=random.randint(0, 10), strength=random.randint(0, 10), technique=random.randint(0, 10) ) ... -
Position Validation (Optional but Recommended): You might want to validate the incoming position to ensure it's one of the expected values. Add this check at the beginning of the
create_soccer_playersfunction:
With these changes, you can now specify the position of the players you want to create. When you make a request to your API, you can now send both the count and the position as part of the request body, like so:
This request would create 5 goalkeepers. Adjust the "position" value as needed for different positions.
Scripts used in the demo
- main.py
- commands.py
- entities.py (unchanged)
- requirements.txt (unchanged)
- config.ini (unchanged)
- consumer.py (unchanged)
Created: 2023-11-01