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.4.tar.gz (10.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.4-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyswaggerapiwrap-0.1.4.tar.gz
  • Upload date:
  • Size: 10.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.4.tar.gz
Algorithm Hash digest
SHA256 e0d5c00420e3dac118ff6585feb4abe9477833078226a682a5da40098d4a84f0
MD5 28c1255a56423cfc278ac7c104c52a56
BLAKE2b-256 13cc8fcd28a72cc88586d196c5bd8a80dd22bb3936bd69a89864724235dd26d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyswaggerapiwrap-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b14f16408a76952d277c9ddc4189db3d6e582dddd9300eb6f3978b87ec979d9a
MD5 ed00670a0a5ba0036112dfd348213331
BLAKE2b-256 f8428ea94ffd814be5bc047e58e81d026dcfc70e958d8e3eb899147f0884ca78

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