Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-nextgen] Add ApiResponse object #15367

Merged
merged 5 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public void processOpts() {
}

supportingFiles.add(new SupportingFile("api_client.mustache", packagePath(), "api_client.py"));
supportingFiles.add(new SupportingFile("api_response.mustache", packagePath(), "api_response.py"));

if ("asyncio".equals(getLibrary())) {
supportingFiles.add(new SupportingFile("asyncio/rest.mustache", packagePath(), "rest.py"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __version__ = "{{packageVersion}}"
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}}
{{/apis}}{{/apiInfo}}
# import ApiClient
from {{packageName}}.api_response import ApiResponse
from {{packageName}}.api_client import ApiClient
from {{packageName}}.configuration import Configuration
from {{packageName}}.exceptions import OpenApiException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ from typing import overload, Optional, Union, Awaitable{{/asyncio}}
{{/imports}}

from {{packageName}}.api_client import ApiClient
from {{packageName}}.api_response import ApiResponse
from {{packageName}}.exceptions import ( # noqa: F401
ApiTypeError,
ApiValueError
Expand Down Expand Up @@ -62,10 +63,6 @@ class {{classname}}(object):
{{/allParams}}
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
:param _preload_content: if False, the urllib3.HTTPResponse object will
wing328 marked this conversation as resolved.
Show resolved Hide resolved
be returned without reading/decoding response
data. Default is True.
:type _preload_content: bool, optional
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -83,7 +80,7 @@ class {{classname}}(object):
return self.{{operationId}}_with_http_info({{#allParams}}{{paramName}}, {{/allParams}}**kwargs) # noqa: E501

@validate_arguments
def {{operationId}}_with_http_info(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs): # noqa: E501
def {{operationId}}_with_http_info(self, {{#allParams}}{{paramName}} : {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, {{/allParams}}**kwargs) -> ApiResponse: # noqa: E501
"""{{{summary}}}{{^summary}}{{operationId}}{{/summary}} # noqa: E501

{{#notes}}
Expand All @@ -101,13 +98,9 @@ class {{classname}}(object):
{{/allParams}}
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
:param _return_http_data_only: response data without head status code
and headers
:param _return_http_data_only: response data instead of ApiResponse
object with status code, headers, etc
:type _return_http_data_only: bool, optional
:param _preload_content: if False, the urllib3.HTTPResponse object will
be returned without reading/decoding response
data. Default is True.
:type _preload_content: bool, optional
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand Down Expand Up @@ -150,7 +143,6 @@ class {{classname}}(object):
[
'async_req',
'_return_http_data_only',
'_preload_content',
'_request_timeout',
'_request_auth',
'_content_type',
Expand Down Expand Up @@ -287,7 +279,6 @@ class {{classname}}(object):
auth_settings=_auth_settings,
async_req=_params.get('async_req'),
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
{{#servers.0}}
_host=_host,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import tornado.gen
{{/tornado}}

from {{packageName}}.configuration import Configuration
from {{packageName}}.api_response import ApiResponse
import {{modelPackage}}
from {{packageName}} import rest
from {{packageName}}.exceptions import ApiValueError, ApiException
Expand Down Expand Up @@ -154,7 +155,8 @@ class ApiClient(object):
query_params=None, header_params=None, body=None, post_params=None,
files=None, response_types_map=None, auth_settings=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None, _host=None,
_request_timeout=None,
_host=None,
_request_auth=None):

config = self.configuration
Expand Down Expand Up @@ -220,7 +222,6 @@ class ApiClient(object):
query_params=query_params,
headers=header_params,
post_params=post_params, body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
except ApiException as e:
if e.body:
Expand All @@ -231,14 +232,6 @@ class ApiClient(object):

return_data = response_data

if not _preload_content:
{{^tornado}}
return return_data
{{/tornado}}
{{#tornado}}
raise tornado.gen.Return(return_data)
{{/tornado}}

response_type = response_types_map.get(str(response_data.status), None)

if response_type == "bytearray":
Expand All @@ -261,17 +254,21 @@ class ApiClient(object):

{{^tornado}}
if _return_http_data_only:
return (return_data)
return return_data
else:
return (return_data, response_data.status,
response_data.getheaders())
return ApiResponse(status_code = response_data.status,
data = return_data,
headers = response_data.getheaders(),
raw_data = response_data.data)
{{/tornado}}
{{#tornado}}
if _return_http_data_only:
raise tornado.gen.Return(return_data)
else:
raise tornado.gen.Return((return_data, response_data.status,
response_data.getheaders()))
raise tornado.gen.Return(ApiResponse(status_code = response_data.status,
data = return_data,
headers = response_data.getheaders(),
raw_data = response_data.data))
{{/tornado}}

def sanitize_for_serialization(self, obj):
Expand Down Expand Up @@ -380,8 +377,8 @@ class ApiClient(object):
body=None, post_params=None, files=None,
response_types_map=None, auth_settings=None,
async_req=None, _return_http_data_only=None,
collection_formats=None,_preload_content=True,
_request_timeout=None, _host=None, _request_auth=None):
collection_formats=None,
_request_timeout=None, _host=None, _request_auth=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.

To make an async_req request, set the async_req parameter.
Expand All @@ -400,13 +397,10 @@ class ApiClient(object):
:param files dict: key -> filename, value -> filepath,
for `multipart/form-data`.
:param async_req bool: execute request asynchronously
:param _return_http_data_only: response data without head status code
and headers
:param _return_http_data_only: response data instead of ApiResponse
object with status code, headers, etc
:param collection_formats: dict of collection formats for path, query,
header, and post parameters.
:param _preload_content: if False, the urllib3.HTTPResponse object will
be returned without reading/decoding response
data. Default is True.
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
Expand All @@ -428,7 +422,7 @@ class ApiClient(object):
body, post_params, files,
response_types_map, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout, _host,
_request_timeout, _host,
_request_auth)

return self.pool.apply_async(self.__call_api, (resource_path,
Expand All @@ -440,61 +434,52 @@ class ApiClient(object):
auth_settings,
_return_http_data_only,
collection_formats,
_preload_content,
_request_timeout,
_host, _request_auth))

def request(self, method, url, query_params=None, headers=None,
post_params=None, body=None, _preload_content=True,
_request_timeout=None):
post_params=None, body=None, _request_timeout=None):
"""Makes the HTTP request using RESTClient."""
if method == "GET":
return self.rest_client.get_request(url,
query_params=query_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
headers=headers)
elif method == "HEAD":
return self.rest_client.head_request(url,
query_params=query_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
headers=headers)
elif method == "OPTIONS":
return self.rest_client.options_request(url,
query_params=query_params,
headers=headers,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
elif method == "POST":
return self.rest_client.post_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "PUT":
return self.rest_client.put_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "PATCH":
return self.rest_client.patch_request(url,
query_params=query_params,
headers=headers,
post_params=post_params,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
elif method == "DELETE":
return self.rest_client.delete_request(url,
query_params=query_params,
headers=headers,
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations
from typing import Any, List, Optional
from pydantic import StrictInt, StrictStr

class ApiResponse:
"""
API response object
"""

status_code: Optional[StrictInt]
headers: Optional[Dict[StrictStr, StrictStr]]
data: Optional[Any]
raw_data: Optional[Any]

def __init__(self,
status_code=None,
headers=None,
data=None,
wing328 marked this conversation as resolved.
Show resolved Hide resolved
raw_data=None):
self.status_code = status_code
self.headers = headers
self.data = data
self.raw_data = data
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ openapi_client/api/header_api.py
openapi_client/api/path_api.py
openapi_client/api/query_api.py
openapi_client/api_client.py
openapi_client/api_response.py
openapi_client/configuration.py
openapi_client/exceptions.py
openapi_client/models/__init__.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from openapi_client.api.query_api import QueryApi

# import ApiClient
from openapi_client.api_response import ApiResponse
from openapi_client.api_client import ApiClient
from openapi_client.configuration import Configuration
from openapi_client.exceptions import OpenApiException
Expand Down