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.2.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.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyswaggerapiwrap-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 3156d1df0cef8c1fbb0a0731d7713cf6b77d8eda4ee6461fb62115f9e63c40f3
MD5 bddb555c62649b44c1ae471784a948a0
BLAKE2b-256 fab771f82bb7af341e8526644b0c3a0e7c37a2453296fdd7148d388c684dc9af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyswaggerapiwrap-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d290cdb041018117af1cdbd2d801805d721a86df1eeafaf8ae529b6367f47a4c
MD5 eb517d3a239f1f71e2a084ad21123a8f
BLAKE2b-256 90cb371900f825f5fca842d337ed62e7ecd7bf6ee22ed2914ffaf417269e27c5

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