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 3b. Connecting my first service

Part 3b. Connecting my first service · Changes

Page history
Update Part 3b. Connecting my first service authored Oct 30, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-3b.-Connecting-my-first-service.md
View page @ e39de721
......@@ -8,9 +8,7 @@ For Python to do that, we'll need a few things.
unixODBC (`yum install unixODBC`), or use ACS.
If you plan on doing local development, and I highly recommend that you do!
You'll need to install the ODBC driver IBM ships for your operating system.
_Currently, Win, Mac, Linux and IBM i naturally_
If you plan on doing local development, and I highly recommend that you do! You'll need to install the ODBC driver IBM ships for your operating system. _Currently, Win, Mac, Linux and IBM i naturally_
You can download that, from the same place you downloaded ACS, [currently that is here](https://www.ibm.com/support/pages/ibm-i-access-client-solutions), but as you know, it changes...
......@@ -18,4 +16,84 @@ Finally, we'll need [PyODBC](https://pypi.org/project/pyodbc/) and [IBM_Db](http
For an in-depth guide, [review the documentation](https://ibmi-oss-docs.readthedocs.io/en/latest/odbc/installation.html)
Most of this, is configured on your chroot environment, so we'll just `pip install pyodbc`...
\ No newline at end of file
Most of this, is configured on your chroot environment, so we'll just `pip install pyodbc`...
## Secrets, everyone has them.
We'll also need some credentials, for now we'll settle for having a single connection.
Still, we shouldn't distribute valid credentials, so create a file called _config.py_ and enter the following variables into it
In vim type \`:e config.py\` - it will create a new empty buffer to wok with.
```
DB2_HOST=
DB2_USER=
DB2_PASS=
```
The values of these, can be filled out with your credentials for now.
Now, `:w` to save.
The config.py file is .gitignore'd, so it wont be added to the repository unless you force it.\
Instead, we'll distribute a config.py.template file, that contains the variables that we want, but without any value associated.
Usually, we would then simply copy that file, and edit it as needed.\
Alternatively, we could create a DSN, outside the project, which could be referenced in the connection string.
For now, we'll go DSN'less.
All that is left, is to use the above, to create a connection in our service, in vim type \`:e db2.py\` - it will create a new empty buffer to work with.
`i`nsert the following code in the file
```
# Get the variables defined in config.py
from config import DB2_HOST, DB2_USER, DB2_PASS
import pyodbc
# Name of the ODBC driver
driver = '{IBM i Access ODBC Driver}'
# Create a connection, using this information
__internalConn = pyodbc.connect(
DRIVER=driver,
SYSTEM=DB2_HOST,
UID=DB2_USER,
PWD=DB2_PASS
)
# Define a function, using that connection.
def getProducts():
c1 = __internalConn.cursor()
return c1.execute('select * from ICEBREAK.PRODUCT')
```
Leave _insert-mode_ and save the file, then switch to the _api_router.py_ file
Here we added to following function
```
# Import the getProducts function
from db2 import getProducts
# And define an endpoint to use it.
@router.post("/query")
def read_item(query: Optional[str] = Form(...)):
cursor = getProducts()
return {
'results': [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
}
```
Save, and press **F5** in vim, just in case.
## Good things to those who wait.
Let's test if that works, switch to your browser, and head into the docs endpoint...
\ 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