Skip to main content

Python Lightweight API (PyLapi)

Project description

PyLapi

Python Lightweight API (PyLapi) is a Python API builder. It takes only a few seconds to automatically generate a Python API from OpenAPI specifications or less than an hour for an experienced Python developer to create a custom Python API.

You can drive PyLapi in an automatic, semiautomatic, or manual way.

Automatic:

The auto-generated Python API contains fully functional resource classes with API methods embedded with OpenAPI specifications as user guide inline comments, so you don't need to read the lengthy OpenAPI Specification to study or use the API.

For more details, please see this tutorial on how to use the generated API.

Semiautomatic:

You can also manually "rewrite" some of the generated classes and methods in a separate code file to, for example, specify custom parameters, pre-process the request, or post-process the response. The PyLapi generator will merge your code into the generated Python API automatically.

PyLapi's code-rewrite feature allows you to specify different API URL endpoints for individual resources, effectively combining multiple API backend services into a single frontend API. All parameters and the request body are dynamically mapped based on the API method route, with the option to rewrite them if you so choose.

More details can be found in this tutorial.

Manual:

When OpenAPI Specifications are not available or you want to create a Python API your own way, you can inherit your root API class from PyLapi, which does all the heavy lifting for you, so your code would mostly be a pass.

If you want to manipulate the API requests and responses, only minimal coding is required. In the tutorial A ChatGPT Conversation with PyLapi, a fully functional Conversation resource is completed in 38 lines, allowing you to do cool things like this:

conversation.ask("Where is the Sydney Opera House?")
print(conversation.data.answers[0])
# Output:
# The Sydney Opera House is located in Sydney, Australia. Specifically, it is situated on Bennelong Point in the Sydney Harbour, close to the Sydney Harbour Bridge.

When OpenAPI Specifications are not available or you want to create a Python API your own way, you can inherit your API class from PyLapi, which does all the heavy lifting for you, so your code would mostly be a pass. For example,

More details can be found in this tutorial.

A PyLapi code example

Here is a sample of a PyLapi API:

from pylapi import PyLapi

class MyAPI(PyLapi):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.api_url = "https://api.example.com"
        self.api_auth_type = "Bearer"

# resource_name="invoice", resource_base_path="invoices"
@MyAPI.resource_class("invoice", "invoices")
class InvoiceResource(MyAPI):

    @MyAPI.resource_method("{invoice_number}", http_method="GET")
    def getInvoice(self): pass
    # To call: MyAPI.resource("invoice").getInvoice(...)
    # Request: GET https://api.example.com/invoices/{invoice_number}

The MyAPI API root class inherits the decorators @MyAPI.resource_class and @MyAPI.resource_methods from PyLapi, which prepares the API parameters and the request body in the API request, sends the request to the API URL endpoint, processes the response, and handles the resource data for you.


The PyLapi framework makes it very easy and natural for you to use a PyLapi-supported Python API. For example,

from myapi import MyAPI

MyAPI.auth(AUTH_ACCESS_TOKEN)

invoice = MyAPI.resource("invoice")
invoice.data = invoice.getInvoice("12345678") # using positional arguments
print(invoice)

item_list = invoice.getItemList()
product = MyAPI.resource("product")
invoice_total = 0.0
for item in item_list:
    product.data.number = item["product_number"]
    product.data = product.getProduct() # using implicit arguments from product.data
    subtotal = product.data.price * item["qty"]
    invoice_total += subtotal
    print(f"Item: {product.data.name}")
    print(f"  Price: ${product.data.price:0.2f}")
    print(f"  Qty: {item['qty']}")
    print(f"  Subtotal: ${subtotal:0.2f}")
print(f"Invoice Total: ${invoice_total:0.2f}")

Install PyLapi

Please follow these instructions to install PyLapi and its generator, replacing all variables accordingly.

To install the PyLapi class:

pip install pylapi

To generate a PyLapi supported Python API:

pylapi-autogen --template > myapi_config.py
# Configure myapi_config.py
pylapi-autogen myapi_config.py
# Output
# MyAPI generated and saved in ./myapi.py

More Information

PyLapi tutorial and user guide are available at the PyLapi GitHub repository. PyLapi API is documented on Read the Docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylapi-0.12.16.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

pylapi-0.12.16-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file pylapi-0.12.16.tar.gz.

File metadata

  • Download URL: pylapi-0.12.16.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pylapi-0.12.16.tar.gz
Algorithm Hash digest
SHA256 f7b2b8f7969ebf3cbafea60c2be1ae5d4315ce35034313e6a10f225d385a2df2
MD5 d881c01dd20834e38981a5afa6e97d04
BLAKE2b-256 5c14faf81d62698c870702d7c103cde736699a246c92730e3caee567179a2aa8

See more details on using hashes here.

File details

Details for the file pylapi-0.12.16-py3-none-any.whl.

File metadata

  • Download URL: pylapi-0.12.16-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pylapi-0.12.16-py3-none-any.whl
Algorithm Hash digest
SHA256 7e66079015e5b0a023f63f40ff9637c84cbf6032745e87c9ceb317aec6d85dec
MD5 5ab4036aeeec0bf02ae58a82fba94f26
BLAKE2b-256 50a7315766799f30e574ca09a9e7b2a50c9c3814a16e9a6b23de539a268027d0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page