|
1 | 1 | # Days 09-12 Building APIs with FastAPI
|
2 | 2 |
|
3 |
| -**Important**: at the time of recording the newest version of `apistar` was `0.5.41`, which let you build complete APIs. |
| 3 | +This exercise section is going to have you laughing, no doubt. You're going to use FastAPI to create an API that will return a random programming-related joke. |
4 | 4 |
|
5 |
| -If you look at the [project docs](https://docs.apistar.com) today though, you'll see the following note: |
| 5 | +--- |
6 | 6 |
|
7 |
| -> _Where did the server go?_ With version 0.6 onwards the API Star project is being focused as a framework-agnositic suite of API tooling. The plan is to build out this functionality in a way that makes it appropriate for use either as a stand-alone tool, or together with a large range of frameworks. The 0.5 branch remains available on GitHub, and can be installed from PyPI with `pip install apistar==0.5.41`. |
| 7 | +## Days 1-3: Watch the video lessons |
8 | 8 |
|
9 |
| -For this reason and in order for the demo project to work we pinned it to version `0.5.41`. |
| 9 | +The first segment of this 4-day block is to watch the videos and study FastAPI. Consider downloading and trying out the [code sample developed during the course](https://github.com/talkpython/100daysofweb-with-python-course/tree/master/days/009-012-modern-apis-with-fastapi/demo). |
10 | 10 |
|
11 |
| ---- |
| 11 | +## Day 4: Funny Business |
| 12 | + |
| 13 | +Now you have seen the videos from this chapter, you're ready to build an API using FastAPI! |
| 14 | + |
| 15 | +We are going to use the [pyjokes](https://pyjok.es) package. This is usually marketed as a CLI tool to get a joke in your terminal. But it also [has an API](https://pyjok.es/api/). It's this API that we can use to build a joke API. |
| 16 | + |
| 17 | +### Foundations |
| 18 | + |
| 19 | +Before we talk about the details of your journey, let me give you a couple of foundational concepts from FastAPI so that you don't need to search the web *too* much (programming always involves some searching or AI-asking). |
| 20 | + |
| 21 | +**FastAPI Starter Structure** |
| 22 | + |
| 23 | +Simple FastAPI apps usually look something like this: |
| 24 | + |
| 25 | +```python |
| 26 | +import fastapi |
| 27 | +import uvicorn |
| 28 | + |
| 29 | +api = fastapi.FastAPI() |
| 30 | + |
| 31 | +@api.get('/') |
| 32 | +def some_action(): |
| 33 | + return {"message": "Hello"} |
| 34 | + |
| 35 | +if __name__ == '__main__': |
| 36 | + uvicorn.run(api, port=8001, host="127.0.0.1") |
| 37 | +``` |
| 38 | + |
| 39 | +Incredible how simple these web apps are these days in their bare form. |
| 40 | + |
| 41 | +**Passing data to actions** |
| 42 | + |
| 43 | +The above api endpoint `some_action` is fun, but takes no data. If we wanted to pass a number, say `start_index`, we could do it like this using the `{}` wrappers in the URL: |
12 | 44 |
|
13 |
| -## Day 1-2: Watch the video lessons |
| 45 | +```python |
| 46 | +@api.get('/{start_index}') |
| 47 | +def some_action(start_index): |
| 48 | +``` |
14 | 49 |
|
15 |
| -The first half of this 4-day block is to watch the videos and study API Star. Consider downloading and trying out the code samples developed during the course. |
| 50 | +But this is a string and index implies an integer. We could cast it ourselves, but FastAPI does that for us with validation by simply specifying the type in code, some_action(start_index**: int**): |
16 | 51 |
|
17 |
| -## Day 3: Make your own API |
| 52 | +```python |
| 53 | +@api.get('/{start_index}') |
| 54 | +def some_action(start_index: int): |
| 55 | +``` |
18 | 56 |
|
19 |
| -Now you have seen the videos from this chapter, you're ready to build an API using API Star! |
| 57 | +**Constrained enums and string values** |
20 | 58 |
|
21 |
| -For this day, you will get a data set from [Mockaroo](https://mockaroo.com/) or any other resource and load it in a sensible data structure (in the demo I used a `list` of `dict`s). |
| 59 | +Finally, in this exercise, you'll work with a small set of valid string values. For example, the pyjokes methods take a `category` of **neutral**, **all**, or **chuck** (as in Norris). We can express that way better with an enum (StrEnum specifically). Note: This type is only available in Python 3.11 or higher. |
22 | 60 |
|
23 |
| -Data is everywhere, but if you don't get inspiration maybe you can use this [Marvel dataset](https://raw.githubusercontent.com/pybites/marvel_challenge/master/marvel-wikia-data.csv) we used for one of our code challenges. If you don't know how to parse CSV, no worries: the same repo [has code for this](https://github.com/pybites/marvel_challenge/blob/solution/marvel.py). |
| 61 | +```python |
| 62 | +class JokeCategory(enum.StrEnum): |
| 63 | + all = 'all' |
| 64 | + chuck_norris = 'chuck' |
| 65 | + neutral = 'neutral' |
| 66 | +``` |
24 | 67 |
|
25 |
| -Next make a virtual env, activate it and `pip install apistar==0.5.41` as shown in the videos (of course you can also use `pipenv` or `Anaconda`). |
| 68 | +It's preferable to use this as the type in FastAPI because FastAPI will automatically reject all inputs that are not one of those three string values. |
26 | 69 |
|
27 |
| -Then start to build your API using the skeleton from my demo. Try to implement the `GET` endpoint today (both all items and single item). |
| 70 | +### Your Joke API |
28 | 71 |
|
29 |
| -## Day 4: Flesh out (and test) the API |
| 72 | +Use what we've given you above along with `pyjokes`, which you'll need to install as a dependency along with fastapi and uvicorn, to build a FastAPI that will listen at then URL: |
30 | 73 |
|
31 |
| -Next continue your API implementing the other CRUD operations: `POST`, `PUT` (update) and `DELETE` (**note** `PUT` requests require a trailing slash!). Make sure you add `404`s (data not there) where applicable. |
| 74 | +`http://127.0.0.1:8001/api/laugh/chuck/en` |
32 | 75 |
|
33 |
| -Maybe you want to add some custom validations as well (like the manufacturer `enum` example in the lesson). See the [API Star docs](https://docs.apistar.com/type-system/). |
| 76 | +And pass in both the category and language (again see [the pyjokes API](https://pyjok.es/api/) for how to use it and valid inputs). |
34 | 77 |
|
35 |
| -Install [Postman](https://www.getpostman.com/) and run the different methods against various endpoints, do they all return the expected data and status codes? |
| 78 | +The response should be a JSON value such as: |
36 | 79 |
|
37 |
| -If you have time left try to write some tests to automate the previous step. I recommend using `pytest` but that is not required. You probably do want to use [Api Star's TestClient](https://github.com/encode/apistar/blob/version-0.5.x/docs/api-guide/testing.md) for convenience (*note* this works for the version we are using in this lesson = 0.5, [starting 0.6 it's deprecated](https://docs.apistar.com/#where-did-the-server-go)). |
| 80 | +```json |
| 81 | +{ |
| 82 | + "category": "chuck", |
| 83 | + "language": "en", |
| 84 | + "joke": "Every SQL statement that Chuck Norris codes has an implicit 'COMMIT' in its end." |
| 85 | +} |
| 86 | +``` |
38 | 87 |
|
39 |
| -Good luck and remember: _the learning is in the practice_. |
| 88 | +Be creative, explore and have fun. Remember: _the learning is in the practice_. |
40 | 89 |
|
41 | 90 | ### Time to share what you've accomplished!
|
42 | 91 |
|
|
0 commit comments