Skip to content

Commit

Permalink
Copy templates from opeapi
Browse files Browse the repository at this point in the history
  • Loading branch information
sokoli1 committed Jan 24, 2024
1 parent 1dfbb1a commit 2defb15
Show file tree
Hide file tree
Showing 6 changed files with 648 additions and 868 deletions.
13 changes: 10 additions & 3 deletions template/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
This Python package is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:

- API version: {{appVersion}}
- Package version: {{packageVersion}}
{{^hideGenerationTimestamp}}
- Build date: {{generatedDate}}
{{/hideGenerationTimestamp}}
- Build package: {{generatorClass}}
{{#infoUrl}}
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}
Expand All @@ -20,11 +22,12 @@ Python {{{generatorLanguageVersion}}}
## Installation & Usage
### pip install

You can install this package directly from the [Python Package Index](https://pypi.org/) using:
If the python package is hosted on a repository, you can install directly using:

```sh
$ pip install {{packageName}}
pip install git+https://{{gitHost}}/{{{gitUserId}}}/{{{gitRepoId}}}.git
```
(you may need to run `pip` with root permission: `sudo pip install git+https://{{gitHost}}/{{{gitUserId}}}/{{{gitRepoId}}}.git`)

Then import the package:
```python
Expand All @@ -45,8 +48,12 @@ Then import the package:
import {{{packageName}}}
```

### Tests

Execute `pytest` to run the tests.

## Getting Started

Please follow the [installation procedure](#installation--usage) and then run the following:

{{> README_common }}
{{> common_README }}
88 changes: 21 additions & 67 deletions template/README_common.mustache
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
```python
{{#apiInfo}}{{#apis}}{{#-last}}{{#hasHttpSignatureMethods}}import datetime{{/hasHttpSignatureMethods}}{{/-last}}{{/apis}}{{/apiInfo}}
import time
import {{{packageName}}}
from {{{packageName}}}.rest import ApiException
from pprint import pprint
{{#apiInfo}}
{{#apis}}
{{#-first}}
from {{apiPackage}} import {{classFilename}}
{{#imports}}
{{{import}}}
{{/imports}}
{{#operations}}
{{#operation}}
{{#-first}}
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
{{> python_doc_auth_partial}}

# Enter a context with an instance of the API client
with {{{packageName}}}.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{{packageName}}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = {{classFilename}}.{{{classname}}}(api_client)
api_instance = {{{packageName}}}.{{{classname}}}(api_client)
{{#allParams}}
{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}}

try:
{{#summary}} # {{{.}}}
{{/summary}} {{#returnType}}api_response = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}={{paramName}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}
pprint(api_response){{/returnType}}
except {{{packageName}}}.ApiException as e:
{{#summary}}
# {{{.}}}
{{/summary}}
{{#returnType}}api_response = {{/returnType}}{{#asyncio}}await {{/asyncio}}api_instance.{{{operationId}}}({{#allParams}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}={{paramName}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
{{#returnType}}
print("The response of {{classname}}->{{operationId}}:\n")
pprint(api_response)
{{/returnType}}
except ApiException as e:
print("Exception when calling {{classname}}->{{operationId}}: %s\n" % e)
{{/-first}}
{{/operation}}
{{/operations}}
{{/-first}}
{{/apis}}
{{/apiInfo}}
```

To generate a token using the [python-keycloak](https://pypi.org/project/python-keycloak/) library:
```python
from keycloak import KeycloakOpenID

clientId = "YOUR_CLIENT_ID"
clientSecret = "YOUR_CLIENT_SECRET"
serverUrl = "https://auth.phoenixnap.com/auth/"
realmName = "BMC"
grantType = "client_credentials"

keycloakOpenId = KeycloakOpenID(server_url=serverUrl,
realm_name=realmName,
client_id=clientId,
client_secret_key=clientSecret)

ACCESS_TOKEN = keycloakOpenId.token(grant_type=grantType)['access_token']
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
```

## Documentation for API Endpoints

All URIs are relative to *{{basePath}}*
All URIs are relative to *{{{basePath}}}*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
Expand All @@ -69,14 +42,14 @@ Class | Method | HTTP request | Description
{{#models}}{{#model}} - [{{{classname}}}]({{modelDocPath}}{{{classname}}}.md)
{{/model}}{{/models}}

<a id="documentation-for-authorization"></a>
## Documentation For Authorization

{{^authMethods}}
All endpoints do not require authorization.
{{/authMethods}}
{{^authMethods}}Endpoints do not require authorization.{{/authMethods}}
{{#hasAuthMethods}}Authentication schemes defined for the API:{{/hasAuthMethods}}
{{#authMethods}}
{{#last}} Authentication schemes defined for the API:{{/last}}
## {{{name}}}
<a id="{{name}}"></a>
### {{{name}}}

{{#isApiKey}}
- **Type**: API key
Expand Down Expand Up @@ -108,23 +81,4 @@ Class | Method | HTTP request | Description
## Author

{{#apiInfo}}{{#apis}}{{#-last}}{{infoEmail}}
{{/-last}}{{/apis}}{{/apiInfo}}

## Notes for Large OpenAPI documents
If the OpenAPI document is large, imports in {{{packageName}}}.apis and {{{packageName}}}.models may fail with a
RecursionError indicating the maximum recursion limit has been exceeded. In that case, there are a couple of solutions:

Solution 1:
Use specific imports for apis and models like:
- `from {{{packageName}}}.api.default_api import DefaultApi`
- `from {{{packageName}}}.model.pet import Pet`

Solution 2:
Before importing the package, adjust the maximum recursion limit as shown below:
```
import sys
sys.setrecursionlimit(1500)
import {{{packageName}}}
from {{{packageName}}}.apis import *
from {{{packageName}}}.models import *
```
{{/-last}}{{/apis}}{{/apiInfo}}
31 changes: 20 additions & 11 deletions template/__init__package.mustache
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
# coding: utf-8

# flake8: noqa

{{>partial_header}}

__version__ = "{{packageVersion}}"

# import apis into sdk package
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}}
{{/apis}}{{/apiInfo}}
# import ApiClient
from {{packageName}}.api_response import ApiResponse
from {{packageName}}.api_client import ApiClient

# import Configuration
from {{packageName}}.configuration import Configuration
{{#hasHttpSignatureMethods}}
from {{packageName}}.signing import HttpSigningConfiguration
{{/hasHttpSignatureMethods}}

# import exceptions
from {{packageName}}.exceptions import OpenApiException
from {{packageName}}.exceptions import ApiAttributeError
from {{packageName}}.exceptions import ApiTypeError
from {{packageName}}.exceptions import ApiValueError
from {{packageName}}.exceptions import ApiKeyError
from {{packageName}}.exceptions import ApiAttributeError
from {{packageName}}.exceptions import ApiException
from {{packageName}}.version import VERSION
{{#hasHttpSignatureMethods}}
from {{packageName}}.signing import HttpSigningConfiguration
{{/hasHttpSignatureMethods}}

# import models into sdk package
{{#models}}
{{#model}}
from {{modelPackage}}.{{classFilename}} import {{classname}}
{{/model}}
{{/models}}
{{#recursionLimit}}

__import__('sys').setrecursionlimit({{{.}}})
{{/recursionLimit}}
__version__ = VERSION
{{/recursionLimit}}
Loading

0 comments on commit 2defb15

Please sign in to comment.