Skip to main content

Python wrapper for api with a swagger

Project description

PYSwaggerAPIWrap

PYSwaggerAPIWrap Logo

Description

PYSwaggerAPIWrap is a Python package designed to streamline interaction with online APIs that expose Swagger documentation. With PYSwaggerAPIWrap, you can easily generate Python wrappers for any API documented via Swagger, making it simpler to integrate and utilize APIs in your code.

Features

  • Universal Support: Compatible with any API that uses Swagger for documentation.
  • Automatic Generation: Automatically creates the necessary Python classes and methods for API interaction.
  • Easy Integration: Seamlessly integrate API calls into your Python project with an intuitive interface.
  • Error Handling: Manages API call errors and provides clear, helpful messages.

Python Version

The version of Python used for the package is Python 3.9.7, but there are no specific constraints on the Python version.

Installation

You can install PYSwaggerAPIWrap using pip:

pip install pyswaggerapiwrap

Complete Tutorial

For a complete tutorial, please refer to the notebook available at this link: Tutorial

Http client

The following lines of code import the HttpClient class from the PySwaggerAPIWrap.http_client module and set up an HTTP client with a specific endpoint and authentication token.

from pyswaggerapiwrap.http_client import HttpClient

ENDPOINT = "https://petstore.swagger.io/v2"
AUTH_TOKEN = "special-key"

http_client = HttpClient(base_url=ENDPOINT, auth_token=AUTH_TOKEN)

The http_client module allows you to make classic HTTP requests using the requests library. It supports both POST and GET methods for interacting with web services.

Using this class, we can retrieve a pandas DataFrame containing all the information from the Swagger documentation with the following method:

routes_dict = http_client.get_routes_df(swagger_route="/swagger.json")

get_routes_df

API DataFrame Filter

Through the API filter class, we can wrap this dictionary to navigate through all the APIs and find the one we are interested in.

First, we create the object by passing the DataFrame as follows:

from pyswaggerapiwrap.api_filter import APIDataFrameFilter

api_filter = APIDataFrameFilter(routes_dict)

filter method

We can use the filter method to filter our APIs in several ways:

  1. By api_type: In this case, the APIs are divided based on the first key. For example, in the APIs from the notebook (“https://petstore.swagger.io/v2”), we have pet, user, and store.
  2. By route_pattern: This allows us to retrieve all APIs that contain the specified string within their route.
  3. By method: We can filter by HTTP methods such as GET and POST.
api_filter.filter(method="GET", api_type="pet", route_pattern="id")

get_routes_df

Api as attributes

Additionally, the api_filter class dynamically allows the indexing of APIs, where various APIs are assigned through a tree structure within the object. This means we can access:

api_filter.store

and we will have all the APIs with api_type equal to store. Furthermore, we can index the APIs directly as attributes. In this case, the actual API names are modified to comply with class attribute rules. To understand, let’s show an example:

The API located at the route "store/inventory" and of type GET can be accessed with the following command:

api_filter.store.get_inventory

The API names follow these rules:

  1. They start with the method: get, post, etc.
  2. Slashes / are replaced with underscores _.
  3. Attributes such as {petId} are replaced with with__petId.

To clarify, let’s provide another example:

The API located at the route "/pet/{petId}/uploadImage" and of type POST can be accessed through the api_filter object as follows:

api_filter.pet.post_with_petId_uploadImage

Run API

To call an API once we have identified the one we are interested in, we simply call the run method. This method dynamically takes both path and query parameters as inputs.

To understand, let’s show two examples:

1. method=GET, route="/store/inventory"

data = api_filter.store.get_inventory.run(http_client)
data
{
  "totvs": 5,
  "sold": 10,
  "string": 623,
  "unavailable": 2,
  "invalid_status": 2,
  "pending": 14,
  "available": 306,
  "peric": 18
}

2. method=GET, route="/pet/{petId}"

data = api_filter.pet.get_with_petId.run(http_client=http_client, petId=1)
data
{
  "id": 1,
  "category": {
    "id": 1,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": ["string"],
  "tags": [
    {
      "id": 1,
      "name": "string"
    }
  ],
  "status": "available"
}

Save and reload the status

PySwaggerAPIWrap allows you to save the state of the package so that you don't have to wrap the documentation from the swagger each time but load it from file directly. Of course, if the API state changes, you will have to save the updated state again.

Example of Use

Below you will find an explanation of how to do it (find the same example in the notebook called status_save_and_reload).

First, let's go configure the variables as done in the previous tutorial:

ENDPOINT = "https://petstore.swagger.io/v2"
AUTH_TOKEN = "special-key"

http_client = HttpClient(base_url=ENDPOINT, auth_token=AUTH_TOKEN)
routes_dict = http_client.get_routes_df(swagger_route="/swagger.json")

api_filter = APIDataFrameFilter(routes_dict)

Save the status

from pyswaggerapiwrap.status import Status, save_status, load_status

file_path = os.path.join("resources", "saved_data", "status.psw")
save_status(file_path, http_client=http_client, routes_dict=routes_dict)

Similarly, we can reload status in this way:

new_api_filter, new_http_client = load_status(file_path)

Compared original objects with the reloaded objects

  1. Run original api
api_filter.store.get_inventory.run(http_client)
{'sold': 9,
 '66363': 2,
 'BPksnQx1oalC9IhhUfUc4lpFlfB0rht0N8IJsBgthNaqdrj43uk0YX48yzYadN81gZAYYDEjTBaqMOhLogiBmZbAoh1sVuN77FPmnKSpMrgsv3lsBFbFyNOBRbY4QllcbvR6OtnK0gsYKmp2uaGwx9E5dWLSd0KcT2O6jfWBqlf1WUXOJ3y1tSevyIN2RBio2Pv4VQqV4g2qFmiMeCLnESxnLY4Y37PbS4d8sGyp9HmdYGSPcABDQUZhjzva7qI': 2,
 'string': 262,
 'pKEOm5831.422150269268744D4W': 2,
 '46673': 2,
 'pending': 3,
 'available': 349,
 '6RVyxbvDJeoCKjOfsGaGPvABhe4c8FdSO14XolrcUVZR4eczMiL0JSll02CbDLzry9B21plW5mO9j7RmDdKAUFZ35mjVJuB39kZUdzvHsltJ4HwzeYV29U2Qdy8ICD9d4HBEbc8VR0qkwettCs7N5tp9HB9cpZq15lf5wiVu3qpJWiqQqLNMmC1ITLUK26JCkr0WOYpJKKyulerTdx36PLzsVKGaJDVnzQ16jOOAYqQI5UjICxTWvYYYjvtoH401CvYrYivyu2LQmPOcKxe4YUtAbzKbHYl8V3yTINJKzlpmviHgEkkJNbk3HpBvobnpyG27Gi92fubcXvGpfDXIMftwRMTWsEbIfoS89KvjXMoXTRNV91D1s974yLatX4R7qw05bGCHT1S7wig6MpAdhBKABTqw9Zd0854QfdyGDR9XCwc2NUoAfSlIC5nKLDzBNBujQw0W7TIHZq3aaSvwEtUxBouXAnHGkK6i31irXIRDJvQrTq9nfKRWamCh2JyCIQ4MHqJ9qW8ONGgxYMGTeCYmAzDzoF5fciFLSgYGYC7R2ntr6JYH3RcIh64O92VPZnhUMwhivVkdofQUCuXemQqp1uwBsERVOKoEaoRQbkUl5PZw7I7cCc8THLddOr5aH0dWoZ4QuwxwlDD3WvlvC9Y70NRYdiPYUIPJ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ggBCwnL5nw5hUoQ3MGExBUTDJ8BUrIETCIyaGrkYawPmkJbow8IkZy1d9g3tqpk2Qj2PdD3OGFo3SruYuwwdeSnkBKqPzLclh11xIehM8DrZRgADyCeusL5VPFFWKYKIXDEa7Fk5My74JG9Wqenn20IHY3FlGbLodUJQqZn7arh0htQoGUn2iHJJrKVzysgBVGKZmQtsA8UfmcdQyE2yqvdXSb68MMKqm3EZ9Gf3dIseQF5TZoEpi1ad9uewofRAAhwTo7V4uBobGHLawazVeL2xYGt8Emryk9v563lXSDkCo6nF5JnsDWgOK7GvRxh8DcuwofaWE03Q1p0pkD': 2,
 'j8rUgCA680.50590458135171.60975683573379370N77C': 2}
  1. Run reloaded api
new_api_filter.store.get_inventory.run(new_http_client)
{'sold': 9,
 '66363': 2,
 'BPksnQx1oalC9IhhUfUc4lpFlfB0rht0N8IJsBgthNaqdrj43uk0YX48yzYadN81gZAYYDEjTBaqMOhLogiBmZbAoh1sVuN77FPmnKSpMrgsv3lsBFbFyNOBRbY4QllcbvR6OtnK0gsYKmp2uaGwx9E5dWLSd0KcT2O6jfWBqlf1WUXOJ3y1tSevyIN2RBio2Pv4VQqV4g2qFmiMeCLnESxnLY4Y37PbS4d8sGyp9HmdYGSPcABDQUZhjzva7qI': 2,
 'string': 264,
 'pKEOm5831.422150269268744D4W': 2,
 '46673': 2,
 'pending': 3,
 'available': 349,
 '6RVyxbvDJeoCKjOfsGaGPvABhe4c8FdSO14XolrcUVZR4eczMiL0JSll02CbDLzry9B21plW5mO9j7RmDdKAUFZ35mjVJuB39kZUdzvHsltJ4HwzeYV29U2Qdy8ICD9d4HBEbc8VR0qkwettCs7N5tp9HB9cpZq15lf5wiVu3qpJWiqQqLNMmC1ITLUK26JCkr0WOYpJKKyulerTdx36PLzsVKGaJDVnzQ16jOOAYqQI5UjICxTWvYYYjvtoH401CvYrYivyu2LQmPOcKxe4YUtAbzKbHYl8V3yTINJKzlpmviHgEkkJNbk3HpBvobnpyG27Gi92fubcXvGpfDX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Ni3W4rKrbRS2Fcv56374lNcrnxJsE1N3BaLzBWfej8WCXjIhZxEkkZsSDz1P5cAF0yegCSrxvQss21rDiWmnfYB58ouXrJBw0sMa7tbSHzIJJ2ZyALXCQrcdkSmLDtg4IiTb3WgmuogJ2oe4tK2j21bbTgA8j6HwENOWVRDctSwJ3hXxu8jPhyNaxxLz8VMiaunNksQdMTZjBsbpeM53WEbrKLpNCTEqTQJpDxJ2cgZOpfiqh3YW54raVy94iqhIEcDvszynU4wJzkryRT7SFOIqpDrBvCdPSEwqcnBzqajIvCIka9CS7fl3o0uOyLVBoADrApxuuSxqB4EKggBCwnL5nw5hUoQ3MGExBUTDJ8BUrIETCIyaGrkYawPmkJbow8IkZy1d9g3tqpk2Qj2PdD3OGFo3SruYuwwdeSnkBKqPzLclh11xIehM8DrZRgADyCeusL5VPFFWKYKIXDEa7Fk5My74JG9Wqenn20IHY3FlGbLodUJQqZn7arh0htQoGUn2iHJJrKVzysgBVGKZmQtsA8UfmcdQyE2yqvdXSb68MMKqm3EZ9Gf3dIseQF5TZoEpi1ad9uewofRAAhwTo7V4uBobGHLawazVeL2xYGt8Emryk9v563lXSDkCo6nF5JnsDWgOK7GvRxh8DcuwofaWE03Q1p0pkD': 2,
 'j8rUgCA680.50590458135171.60975683573379370N77C': 2}

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

pyswaggerapiwrap-0.1.5.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyswaggerapiwrap-0.1.5-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file pyswaggerapiwrap-0.1.5.tar.gz.

File metadata

  • Download URL: pyswaggerapiwrap-0.1.5.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.7 Windows/10

File hashes

Hashes for pyswaggerapiwrap-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c99732e32244bd0cffb920b88e6fe0fd91dc2abb5d9d351a9d74dae73e67c57c
MD5 890b8dc4d5ce7a125f76ba555d8da421
BLAKE2b-256 37be753e70cb2362aaef963268f6dc2fc6264a014c10890aef1f2d66d2a47925

See more details on using hashes here.

File details

Details for the file pyswaggerapiwrap-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pyswaggerapiwrap-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.7 Windows/10

File hashes

Hashes for pyswaggerapiwrap-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 53a3b05936e056fb10f8c8020f06a764d522d0fb1375245585db0198e254cb33
MD5 6f9ed2cafa8c32a7fce0e9914ce04887
BLAKE2b-256 065a6f204f462f556bbf7f812e2115d132e1e3467aeb9c890e76d0ae10daebd0

See more details on using hashes here.

Supported by

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