Skip to main content

Swagger Client Code Generator. Using Python. For Python.

Project description

Build Status Codacy Badge

swagccg-py2py

Swagger Client Code Generator. Using Python. For Python


Summary

Immediately testing new resources is important if resources are going to be co-dependent. While the tools at SwaggerHub are mind blowing they may represent an overkill for not yet production code. Typing a single query can be done in any browser or with tools like curl. This tool aims to place itself between those two categories.

On top of that, I would expect a programming language to be able to create its own tools. While Java is a mature and well established language, it might not be within the proficiency portfolio of each and everyone - not to mention the author.

Get the Code

🚨 not yet

pip install swagccg-py2py

or clone it into your development environment

git clone https://github.com/erkandem/swagccg-py2py.git

or download the zip

https://github.com/erkandem/swagccg-py2py/archive/master.zip

Getting started

The assumption here ist that you already have a swagger.json file.

The creation of a client comes down to:

python swagccg

If the config.json is not in your working directory you would have to add its location to the call:

python -m swagccg -c /location/of/your/config.json

the configuration file

config.json consists of two distinct parts. First, we would like to tell the script:

  • where we keep a swagger definition
  • where we would like the client module to be created
  • what name we would like the client class to have

Since this is rather a development tool we would like to switch between target hosts with little afford (i.e. environment variable). Therefore, we will offer it two targets which are later used to assamble resource URLs.

We'll set a local (i.e. development) and remote (i.e. deployed) set of:

  • port
  • base url (i.e IPv4, host, domain_name.tld, subdomain.domain_name.tld)
  • scheme (http or https)
{
  "swagger_path": "/home/abuser/apiclient/swagger.json",
  "target_path": "/home/abuser/apiclient/auto_client.py",
  "class_name": "Myclient",


  "api_port_local": "5000",
  "api_url_base_local": "127.0.0.1",
  "api_protocol_local": "http",

  "api_port_remote": "80",
  "api_url_base_remote": "deployed.com",
  "api_protocol_remote": "https"
}

Client Creation

python -m swagccg --c location/of/your/config.json

Client Usage

Ultimately, the usage of the client depends on your requirements. Nonetheless, the README would be incomplete without some usage examples:

from auto_client import MyApiClient # default names - set them in confi.json
from settings import credential_dict # if needed

client_instance = MyApiClient('remote')  # or 'local' 
client_instance.login_with_api(credential_dict) 
data = client_instance.get_something_r() 

or

import os
from pathlib import Path
from dotenv import load_dotenv
from auto_client import MyApiClient
#%%
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
client_instance = MyApiClient('remote')

#%% login of course depends on the server
client_instance.login_with_api({
    'username': os.getenv('API_USERNAME'),
    'password': os.getenv('API_PASSWORD')
})
param_dict = dict(name='value')
data = client_instance.get_something_r(fields_data=param_dict)

gotchas

  • authorization is highly custom
  • most of the swagger details are not parsed
  • models and mapping is omitted (marshmallow)
  • little to none HTTP status codes parsing
  • assumes knowledge on HTTP HEADER, BODY, METHOD
  • pass pass_through=True as parameter to receive the response object untouched

recommended reading

Mark Masse, REST API Design Rulebook - Designing Consistent RESTful Web Service Interfaces

Petstore - API

OpenAPI Specififcation

Contact

Email erkan.dem@pm.me

Issues: github.com/erkandem/swagccg-py2py/issues

Source: github.com/erkandem/swagccg-py2py

Documentation: github.com/erkandem/swagccg-py2py/README.md

License

My project is licensed under terms of MIT. For details please see the LICENSE

The examples and tests depend on the petstore. The attached petstore swagger by smartbear / OpenAPI Initiative is licensed with MIT and is part of the Apache 2.0 licensed repo.

Click Bait

Visitors who were interested in this repo also took a look at:

swagccg-m2m - MatLab to MatLab Client Code Generation

Because every programming language should be able to create its own tools.

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

swagccg-0.3.4.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

swagccg-0.3.4-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file swagccg-0.3.4.tar.gz.

File metadata

  • Download URL: swagccg-0.3.4.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for swagccg-0.3.4.tar.gz
Algorithm Hash digest
SHA256 4d9c6bd238675e219d6117c1a231531d59eed921fd0f7fde3827785ee3322219
MD5 737f382b676ac1c1e1a658091044063f
BLAKE2b-256 74303120a1ce56f4eb869ede337b1b42d6a7d8f22217dc884d0f99cb13b69fda

See more details on using hashes here.

File details

Details for the file swagccg-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: swagccg-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for swagccg-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6c05d7b1576fdaf0bf3234db7afaa8250a1c629a99de59f65f804a10b801caa2
MD5 24693941f2103956a26b7162b9b98fc5
BLAKE2b-256 4b8f91fc902a9c66fd9397a39dcb9ed95712ff76d402b50c7692c924ce06b4ed

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