swagccg-py2py
Swagger Client Code Generator. Using Python. For Python
Summary
There is a) professional SDK generation tools (OpenAPI code gen and others)
and b) tools like postman or simply curl
This tool aims to place itself a notch more useful than querying your API with curl.
Fork it and use it as a template.
Get the Code
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 codesparsing - assumes knowledge on HTTP
recommended reading
Mark Masse, REST API Design Rulebook - Designing Consistent RESTful Web Service Interfaces
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.
change log
v0.4.0 2020-27-20
breaking
-
remove "logic" around trying to load response content just return the fluffing response
-
completely remove refreshing related stuff
-
add typing where ease and added indentation for arguments (too long)
Metadata
Release files for swagccg 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| swagccg-0.4.0.tar.gz | 13.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swagccg-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.6 kB
Release files / swagccg-0.4.0.tar.gz
| Download URL | swagccg-0.4.0.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb4ecae4187a888989213c3c0d6a71a5b42013cf900b48ce5cbc8323a9662e32
|
|
BLAKE2b-256 checksum How to use checksums |
28e978e9e48d5bcb6046d549e21fead49e3d8fb4884a93f318f526cb6586ca2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / swagccg-0.4.0-py3-none-any.whl
| Download URL | swagccg-0.4.0-py3-none-any.whl |
|---|---|
| Size | 14.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e68c8377decd64a48b1b0eaee3922b24cce10a0efcff6b087381758469e095b3
|
|
BLAKE2b-256 checksum How to use checksums |
4d38ee68791046c0e0c44e563ee15519cc0b24a48053eed36eccc62fb6540862
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for swagccg 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.