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"
}

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.1.tar.gz (8.7 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.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyswaggerapiwrap-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.7

File hashes

Hashes for pyswaggerapiwrap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a87e4b4750423fde2e86802579012b892d1eefd8e6981521bf17a679f43809b2
MD5 3c778f8757775e56ed95fae99a289385
BLAKE2b-256 8e0d328cd10380f5ed544d86a1d92421fa015b1891fc4c2a7eb26dfbad31a8ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyswaggerapiwrap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 afcb3df695ac9836f48f69e018d83820a61f4ea5a44254903843aca58fca731c
MD5 d58579437fd6b42d492e960c485870c1
BLAKE2b-256 73b36ee3010f9aeb99c87a256ae91182583117ad65c3215681d12037f9abd10b

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