|
|
# Using vim to create your first FastAPI service on IBM i.
|
|
|
|
|
|
To insert text into a file using vim, press `i` on the keyboard, you are now in _insert mode_.
|
|
|
|
|
|
Note the difference in the bottom left corner...
|
|
|
|
|
|
![image](uploads/b06524b53cd8ce401e78df3610e798cf/image.png)
|
|
|
|
|
|
## Type the the following code into **main.py**
|
|
|
**(or not, be lazy, you know the chef has prepared something**...)
|
|
|
|
|
|
```python
|
|
|
from typing import Optional
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/")
|
|
|
def read_root():
|
|
|
return {"Hello": "World"}
|
|
|
|
|
|
|
|
|
@app.get("/items/{item_id}")
|
|
|
def read_item(item_id: int, q: Optional[str] = None):
|
|
|
return {"item_id": item_id, "q": q}
|
|
|
```
|
|
|
|
|
|
## What does all that do?
|
|
|
|
|
|
`app` is the name of our services entry-point, and is also used to annotate endpoints in our service.
|
|
|
|
|
|
In this instance, the main endpoint `/` typically this is a file called _index.html_ on your web-server
|
|
|
|
|
|
The endpoint `/items/{item_id}` uses _path-variables_ to input arguments to our function,\
|
|
|
anything typed in curly braces, is copied into the function argument with the same name.
|
|
|
|
|
|
You'll notice there is an additional argument in the function, called `q` this is a query-string variable,\
|
|
|
anything not otherwise defined, is treated by FastAPI as a query-string argument.\
|
|
|
Finally, this q argument, is defined as being optional, meaning the client is not required to provide it, and defaults to `None`.
|
|
|
|
|
|
## Save changes, and run the service
|
|
|
|
|
|
Leave _insert mode_, by pressing `Esc`,
|
|
|
|
|
|
Save your changes by type `:w` \
|
|
|
and exit vim by typing `:q`
|
|
|
|
|
|
( This can be shorten to `:wq` )
|
|
|
|
|
|
You are now back in the shell, where we will run this program using.
|
|
|
|
|
|
|
|
|
[Proceed to the next page...](Part-1b.-Testing-my-first-service) |
|
|
\ No newline at end of file |