Generating and deploying Python code

Generating Python-based server code from your OpenAPI specification

Important: Ensure that you have created OpenAPI document beforehand.

The CodeGenerator tool creates Python FastAPI server code based on your OpenAPI specification. This code helps you quickly scaffold API endpoints defined in your OpenAPI file, so that you can focus on implementing business logic instead of writing boilerplate code. Follow the steps below to generate Python server code from your OpenAPI specification.

The CodeGeneration tool enables you to specify the type of server code you wish to generate. Currently, API Assistant supports two server types:
  • Simple Server: A minimalist server providing the basic setup to start building your service. For example:
    Generate some Python FastAPI server code based on the following openapi @
  • Database CRUD Operations Server: An extension of the Simple Server that incorporates database logic to perform Create, Read, Update, and Delete (CRUD) operations, suitable for applications that require persistent data management. For example:
    Build FastAPI backend code, including some database integration, using the OpenAPI file @
To generate a python code, complete the following steps:
  1. Log into API Assistant. For details, see Getting started.
  2. Enter the following prompt, and click Send:
    Generate FastAPI server code from the openapi document @
    and select the required file, for example, purchaseorder_apispec.yaml.
    Note: Entering @ after the prompt displays a list of OpenAPI documents available in your workspace.
  3. Click Send. The CodeGenerator tool communicates with the API Assistant sources and displays the proposed plan.

    To view the plan details, expand the arrow icon .

  4. Click Start to begin code generation. Once complete, you can view a brief summary of the next steps required to download the generated code as well as how to run and deploy it.
  5. Click the download icon to save the ZIP file containing the code.
  6. In VS Code, open the Terminal and run the following command to unzip the downloaded file into a folder, for example, app-code.
    unzip PostgreSQL\ purchaseorder-python.zip
  7. Open the README.md file to view the instructions on how to get started with the downloaded code. The README.md provides detailed information on how to build, install, customize, and deploy the application.
  8. Go to the Explorer pane, open the database-props file.
  9. Replace the existing password with 9Iron-head, and save the file.
  10. Follow the steps in the Installation and Running the Server sections of the README.md file to build, install, and run the server.
    1. Open a Terminal window and navigate to the extracted folder:
      cd app-code
    2. Create a virtual environment
      python3 -m venv venv
    3. Activate the Virtual Environment.
      1. For Unix or macOS:
        source ./venv/bin/activate
        
      2. For Windows:
        .\venv\Scripts\activate
        
    4. Install the required packages listed in requirements.txt
      pip install -r requirements.txt
    5. Run the server using the following command
      PYTHONPATH=src uvicorn openapi_server.main:app --host 0.0.0.0 --port 8080
      
      The server will be accessible at: http://localhost:8080
  11. To deploy the application, run the deploy.py script with the required input arguments as shown below.
    python deploy.py <IBM Cloud API key> <IBM Cloud Resource Group name> <IBM Cloud Project name> <ICR Namespace name>
    
    Replace the placeholders with the following:
    • <IBM Cloud API key>: The IAM API key for your IBM Cloud account.
    • <IBM Cloud Resource Group name>: The resource group to be used within the specified IBM Cloud account.
    • <IBM Cloud Project name>: A custom name for your IBM Code Engine project, application, and associated artifacts. This value will serve as a prefix for all IBM Code Engine artifacts created during deployment.
    • <ICR Namespace name>: The name of an existing IBM Container Registry (ICR) namespace within your IBM Cloud account. This namespace will serve as the destination where IBM Code Engine pushes the built container image for your application source code. Ensure this namespace exists in the global region of the ICR (for example, icr.io/<ICR Namespace name>).
    For example,
    python deploy.py <ibm-cloud-apikey> default xxx-app-default testfest
    (replace uniqueapikey with API key, and xxx-app-default with your unique app name)
  12. Once the script completes, a success message similar to the following will appear if the deployment was successful:
    App <IBM Cloud Project name>-app deployed successfully.
    App <IBM Cloud Project name>-app URL: https://<IBM Cloud Project name>-app.1o0xvbar1wkk.us-south.codeengine.appdomain.cloud
    
  13. Copy the app URL from the success message and paste it into a browser.
  14. Open the openapi.yaml file, copy the endpoint from the GET request, and append the endpoint to the app URL in the browser's address bar.
  15. Run the request in the browser to view the application result.
  16. To modify the business logic, refer to the Customizing Logic section in README.md.