Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • F FastAPI
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Sitemule
  • Public
  • workshops
  • open source on IBM i
  • FastAPI
  • Wiki
  • Part 1a. My first service

Part 1a. My first service · Changes

Page history
Update Part 1a. My first service authored Oct 29, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-1a.-My-first-service.md 0 → 100644
View page @ 835109fc
# 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
Clone repository
  • Part 1a. My first service
  • Part 1b. Testing my first service
  • Part 2a. Extending my first service
  • Part 2b. Organise my first service
  • Part 3a. Review my first service
  • Part 3b. Connecting my first service
  • Home