Skip to content

Commit

Permalink
[PYTHON] Api doc python with multiple authentications (OpenAPITools#928)
Browse files Browse the repository at this point in the history
* [PYTHON] better code snippet for API usage with authentication

* update python samples
  • Loading branch information
Matthieu Berthomé authored and wing328 committed Aug 31, 2018
1 parent 5ad471c commit 6bc3c70
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,14 @@ Method | HTTP request | Description
{{{notes}}}{{/notes}}

### Example
```python
from __future__ import print_function
import time
import {{{packageName}}}
from {{{packageName}}}.rest import ApiException
from pprint import pprint
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}{{/authMethods}}

# create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}({{{packageName}}}.ApiClient(configuration))
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}}
{{#hasAuthMethods}}{{#authMethods}}
{{#isBasic}}* Basic Authentication ({{name}}): {{/isBasic }}{{#isApiKey}}* Api Key Authentication ({{name}}): {{/isApiKey }}{{#isOAuth}}* OAuth Authentication ({{name}}): {{/isOAuth }}
{{> api_doc_example }}
{{/authMethods}}
{{/hasAuthMethods}}
{{^hasAuthMethods}}

# create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}()
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}}
{{> api_doc_example }}
{{/hasAuthMethods}}

try:
{{#summary}} # {{{.}}}
{{/summary}} {{#returnType}}api_response = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}={{paramName}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}
pprint(api_response){{/returnType}}
except ApiException as e:
print("Exception when calling {{classname}}->{{operationId}}: %s\n" % e)
```

### Parameters
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
Name | Type | Description | Notes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
```python
from __future__ import print_function
import time
import {{{packageName}}}
from {{{packageName}}}.rest import ApiException
from pprint import pprint
{{#hasAuthMethods}}{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.api_key['{{{keyParamName}}}'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
configuration = {{{packageName}}}.Configuration()
configuration.access_token = 'YOUR_ACCESS_TOKEN'{{/isOAuth}}

# create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}({{{packageName}}}.ApiClient(configuration))
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}}
{{/hasAuthMethods}}
{{^hasAuthMethods}}

# create an instance of the API class
api_instance = {{{packageName}}}.{{{classname}}}()
{{#allParams}}{{paramName}} = {{{example}}} # {{{dataType}}} | {{{description}}}{{^required}} (optional){{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/allParams}}
{{/hasAuthMethods}}

try:
{{#summary}} # {{{.}}}
{{/summary}} {{#returnType}}api_response = {{/returnType}}api_instance.{{{operationId}}}({{#allParams}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}={{paramName}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}
pprint(api_response){{/returnType}}
except ApiException as e:
print("Exception when calling {{classname}}->{{operationId}}: %s\n" % e)
```
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.2.2-SNAPSHOT
2 changes: 2 additions & 0 deletions samples/client/petstore/python-asyncio/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ Fake endpoint for testing various parameters 假端點 偽のエンドポイン
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

### Example

* Basic Authentication (http_basic_test):
```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ To test class name in snake case
To test class name in snake case

### Example

* Api Key Authentication (api_key_query):
```python
from __future__ import print_function
import time
Expand Down
18 changes: 18 additions & 0 deletions samples/client/petstore/python-asyncio/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Method | HTTP request | Description
Add a new pet to the store

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -70,6 +72,8 @@ void (empty response body)
Deletes a pet

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -123,6 +127,8 @@ Finds Pets by status
Multiple status values can be provided with comma separated strings

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -175,6 +181,8 @@ Finds Pets by tags
Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -227,6 +235,8 @@ Find pet by ID
Returns a single pet

### Example

* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -279,6 +289,8 @@ Name | Type | Description | Notes
Update an existing pet

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -328,6 +340,8 @@ void (empty response body)
Updates a pet in the store with form data

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -381,6 +395,8 @@ void (empty response body)
uploads an image

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -435,6 +451,8 @@ Name | Type | Description | Notes
uploads an image (required)

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/python-asyncio/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Returns pet inventories by status
Returns a map of status codes to quantities

### Example

* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.2.2-SNAPSHOT
2 changes: 2 additions & 0 deletions samples/client/petstore/python-tornado/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ Fake endpoint for testing various parameters 假端點 偽のエンドポイン
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

### Example

* Basic Authentication (http_basic_test):
```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ To test class name in snake case
To test class name in snake case

### Example

* Api Key Authentication (api_key_query):
```python
from __future__ import print_function
import time
Expand Down
18 changes: 18 additions & 0 deletions samples/client/petstore/python-tornado/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Method | HTTP request | Description
Add a new pet to the store

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -70,6 +72,8 @@ void (empty response body)
Deletes a pet

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -123,6 +127,8 @@ Finds Pets by status
Multiple status values can be provided with comma separated strings

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -175,6 +181,8 @@ Finds Pets by tags
Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -227,6 +235,8 @@ Find pet by ID
Returns a single pet

### Example

* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -279,6 +289,8 @@ Name | Type | Description | Notes
Update an existing pet

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -328,6 +340,8 @@ void (empty response body)
Updates a pet in the store with form data

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -381,6 +395,8 @@ void (empty response body)
uploads an image

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down Expand Up @@ -435,6 +451,8 @@ Name | Type | Description | Notes
uploads an image (required)

### Example

* OAuth Authentication (petstore_auth):
```python
from __future__ import print_function
import time
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/python-tornado/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Returns pet inventories by status
Returns a map of status codes to quantities

### Example

* Api Key Authentication (api_key):
```python
from __future__ import print_function
import time
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.2.2-SNAPSHOT
2 changes: 2 additions & 0 deletions samples/client/petstore/python/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ Fake endpoint for testing various parameters 假端點 偽のエンドポイン
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

### Example

* Basic Authentication (http_basic_test):
```python
from __future__ import print_function
import time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ To test class name in snake case
To test class name in snake case

### Example

* Api Key Authentication (api_key_query):
```python
from __future__ import print_function
import time
Expand Down
Loading

0 comments on commit 6bc3c70

Please sign in to comment.