|
| 1 | +### RESTful API with Python 3 and FastAPI |
| 2 | +### https://github.com/nanotaboada/python-samples-fastapi-restful |
| 3 | +### |
| 4 | +### Requires the REST Client extension for VS Code: |
| 5 | +### https://marketplace.visualstudio.com/items?itemName=humao.rest-client |
| 6 | +### |
| 7 | +### Key design note: |
| 8 | +### squad_number = natural key (domain-meaningful, preferred for lookups) |
| 9 | +### id (UUID) = surrogate key (internal, opaque, used for CRUD operations) |
| 10 | + |
| 11 | +@baseUrl = http://localhost:9000 |
| 12 | + |
| 13 | +# ------------------------------------------------------------------------------ |
| 14 | +# Health |
| 15 | +# ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +### GET /health/ — liveness check |
| 18 | +GET {{baseUrl}}/health/ |
| 19 | +Accept: application/json |
| 20 | + |
| 21 | +# ------------------------------------------------------------------------------ |
| 22 | +# POST /players/ — Create |
| 23 | +# Thiago Almada (squad 16): not seeded in the database, used as the creation |
| 24 | +# fixture. No id in the body; the server generates a UUID v4 on creation. |
| 25 | +# ------------------------------------------------------------------------------ |
| 26 | + |
| 27 | +### POST /players/ — Create a new Player |
| 28 | +POST {{baseUrl}}/players/ |
| 29 | +Content-Type: application/json |
| 30 | + |
| 31 | +{ |
| 32 | + "firstName": "Thiago", |
| 33 | + "middleName": "Ezequiel", |
| 34 | + "lastName": "Almada", |
| 35 | + "dateOfBirth": "2001-04-26T00:00:00.000Z", |
| 36 | + "squadNumber": 16, |
| 37 | + "position": "Attacking Midfield", |
| 38 | + "abbrPosition": "AM", |
| 39 | + "team": "Atlanta United FC", |
| 40 | + "league": "Major League Soccer", |
| 41 | + "starting11": false |
| 42 | +} |
| 43 | + |
| 44 | +# ------------------------------------------------------------------------------ |
| 45 | +# GET /players/ — Retrieve all |
| 46 | +# ------------------------------------------------------------------------------ |
| 47 | + |
| 48 | +### GET /players/ — Retrieve all Players |
| 49 | +GET {{baseUrl}}/players/ |
| 50 | +Accept: application/json |
| 51 | + |
| 52 | +# ------------------------------------------------------------------------------ |
| 53 | +# GET /players/{player_id} — Retrieve by UUID (surrogate key, internal) |
| 54 | +# Emiliano Martínez (squad 23): UUID v5, seeded by seed_001. |
| 55 | +# ------------------------------------------------------------------------------ |
| 56 | + |
| 57 | +### GET /players/{player_id} — Retrieve a Player by UUID |
| 58 | +GET {{baseUrl}}/players/b04965e6-a9bb-591f-8f8a-1adcb2c8dc39 |
| 59 | +Accept: application/json |
| 60 | + |
| 61 | +# ------------------------------------------------------------------------------ |
| 62 | +# GET /players/squadnumber/{squad_number} — Retrieve by Squad Number (natural key, domain) |
| 63 | +# Preferred lookup for external consumers. |
| 64 | +# ------------------------------------------------------------------------------ |
| 65 | + |
| 66 | +### GET /players/squadnumber/{squad_number} — Retrieve a Player by Squad Number |
| 67 | +GET {{baseUrl}}/players/squadnumber/10 |
| 68 | +Accept: application/json |
| 69 | + |
| 70 | +# ------------------------------------------------------------------------------ |
| 71 | +# PUT /players/{player_id} — Update |
| 72 | +# Emiliano Martínez (squad 23): UUID v5, seeded by seed_001. |
| 73 | +# ------------------------------------------------------------------------------ |
| 74 | + |
| 75 | +### PUT /players/{player_id} — Update an existing Player |
| 76 | +PUT {{baseUrl}}/players/b04965e6-a9bb-591f-8f8a-1adcb2c8dc39 |
| 77 | +Content-Type: application/json |
| 78 | + |
| 79 | +{ |
| 80 | + "firstName": "Emiliano", |
| 81 | + "middleName": null, |
| 82 | + "lastName": "Martínez", |
| 83 | + "dateOfBirth": "1992-09-02T00:00:00.000Z", |
| 84 | + "squadNumber": 23, |
| 85 | + "position": "Goalkeeper", |
| 86 | + "abbrPosition": "GK", |
| 87 | + "team": "Aston Villa FC", |
| 88 | + "league": "Premier League", |
| 89 | + "starting11": true |
| 90 | +} |
| 91 | + |
| 92 | +# ------------------------------------------------------------------------------ |
| 93 | +# DELETE /players/{player_id} — Delete |
| 94 | +# Thiago Almada (squad 16): created by POST above. Since the UUID is generated |
| 95 | +# at runtime, retrieve it first via squad number, then substitute it below. |
| 96 | +# ------------------------------------------------------------------------------ |
| 97 | + |
| 98 | +### Step 1 — Look up Almada's UUID by squad number |
| 99 | +GET {{baseUrl}}/players/squadnumber/16 |
| 100 | +Accept: application/json |
| 101 | + |
| 102 | +### Step 2 — Delete Almada using the UUID returned above |
| 103 | +# Replace {player_id} with the id field from the response above. |
| 104 | +DELETE {{baseUrl}}/players/{player_id} |
0 commit comments