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-experimental] consolidates endpoints into paths module #13007

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
43515e3
Adds endpoint creation in path modules
spacether Jul 25, 2022
4e0d341
REgens samples
spacether Jul 26, 2022
ba763f2
Adds BaseApi so there can be 2 class interfaces for http method and o…
spacether Jul 27, 2022
44e95a8
Adds paths init
spacether Jul 27, 2022
1cdcae8
Adds enum containing paths
spacether Jul 27, 2022
8882c75
Uses path enum for endpoint paths
spacether Jul 27, 2022
17a3bcf
Adds camel case to undersce converstion in to ineum var name
spacether Jul 27, 2022
8033c34
Fixes path enum generation
spacether Jul 27, 2022
4f3cf5f
Moves path api combination module into apis
spacether Jul 27, 2022
ce3b8fa
Moves tag apis into a tags module
spacether Jul 28, 2022
c32c3ca
Adds path_to_api
spacether Jul 28, 2022
0629d91
Changes module path to paths
spacether Jul 28, 2022
5330d4c
Fixes tag api imports
spacether Jul 28, 2022
85e7329
Fixes self type in endpoint methods
spacether Jul 28, 2022
5720d1c
Adds test changes
spacether Jul 28, 2022
9290fbe
Adds tag enum
spacether Jul 28, 2022
bc34787
Adds tag_to_api
spacether Jul 29, 2022
c39bfd8
Adds missing tag
spacether Jul 29, 2022
2cde2c4
Fixes self types in endpoint methods
spacether Jul 29, 2022
ad919ad
Refactors java endpoint generation to be simpler
spacether Jul 29, 2022
c2858f0
Further refactors generateEndpoints
spacether Jul 29, 2022
df7a20f
Generates one test file per endpoint
spacether Jul 29, 2022
1f422a9
Updates v3 samples
spacether Jul 29, 2022
ec3c215
Fixes endpoint tests, all tests passing now
spacether Jul 29, 2022
8dfdf12
Samples regenerated
spacether Jul 29, 2022
4240e07
Fixes petstore tests
spacether Jul 30, 2022
aa5457b
Generates separate endpoint test methods on each endpoint
spacether Aug 1, 2022
a5d60ea
Fixes api docs and enum string values in those docs
spacether Aug 1, 2022
c1ce05c
Regenerates samples
spacether Aug 1, 2022
f77a14e
Removes pass to fix tests
spacether Aug 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
{{#with apiInfo}}
{{#each apis}}
{{#if @first}}
# coding: utf-8

# flake8: noqa

# Import all APIs into this package.
# If you have many APIs here with many many models used in each API this may
# raise a `RecursionError`.
# In order to avoid this, import only the API that you directly need like:
#
# from {{packageName}}.{{apiPackage}}.{{classFilename}} import {{classname}}
#
# or import this package, but before doing it, use:
#
# import sys
# sys.setrecursionlimit(n)

# Import APIs into API package:
{{/if}}
from {{packageName}}.{{apiPackage}}.{{classFilename}} import {{classname}}
{{/each}}
{{/with}}
# do not import all endpoints into this module because that uses a lot of memory and stack frames
# if you need the ability to import all endpoints then import them from
# tags, paths, or path_to_api, or tag_to_api
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# do not import all endpoints into this module because that uses a lot of memory and stack frames
# if you need the ability to import all endpoints from this module, import them with
# from {{packageName}}.apis.tag_to_api import tag_to_api

import enum


class TagValues(str, enum.Enum):
{{#each enumToTag}}
{{@key}} = "{{this}}"
{{/each}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# do not import all endpoints into this module because that uses a lot of memory and stack frames
# if you need the ability to import all endpoints from this module, import them with
# from {{packageName}}.apis.path_to_api import path_to_api
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# do not import all endpoints into this module because that uses a lot of memory and stack frames
# if you need the ability to import all endpoints from this module, import them with
# from {{packageName}}.apis.path_to_api import path_to_api

import enum


class PathValues(str, enum.Enum):
{{#each pathValToVar}}
{{this}} = "{{@key}}"
{{/each}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# do not import all endpoints into this module because that uses a lot of memory and stack frames
# if you need the ability to import all endpoints from this module, import them with
# from {{packageName}}.paths.{{pathModule}} import {{apiClassName}}

from {{packageName}}.paths import PathValues

path = PathValues.{{{pathVar}}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

{{>partial_header}}

from {{packageName}}.api_client import ApiClient
{{#with operations}}
{{#each operation}}
from {{packageName}}.api.{{classFilename}}_endpoints.{{operationId}} import {{operationIdCamelCase}}
from {{packageName}}.paths.{{nickname}}.{{httpMethod}} import {{operationIdCamelCase}}
{{/each}}
{{/with}}

Expand All @@ -15,7 +14,6 @@ class {{classname}}(
{{#each operation}}
{{operationIdCamelCase}},
{{/each}}
ApiClient,
):
"""NOTE: This class is auto generated by OpenAPI Generator
Ref: https://openapi-generator.tech
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```python
import {{{packageName}}}
from {{packageName}}.{{apiPackage}} import {{classFilename}}
from {{packageName}}.{{apiPackage}}.tags import {{classFilename}}
{{#each imports}}
{{{.}}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,42 @@ from unittest.mock import patch
import urllib3

import {{packageName}}
from {{packageName}}.{{apiPackage}}.{{classFilename}} import {{classname}} # noqa: E501
from {{packageName}}.paths.{{operation.nickname}} import {{operation.httpMethod}} # noqa: E501
from {{packageName}} import configuration, schemas, api_client

from . import ApiTestMixin
from .. import ApiTestMixin


class {{#with operations}}Test{{classname}}(ApiTestMixin, unittest.TestCase):
"""{{classname}} unit test stubs"""
{{#with operation}}
class Test{{operationIdSnakeCase}}(ApiTestMixin, unittest.TestCase):
"""
{{operationIdSnakeCase}} unit test stubs
{{#if summary}}
{{{summary}}} # noqa: E501
{{/if}}
"""
_configuration = configuration.Configuration()

def setUp(self):
used_api_client = api_client.ApiClient(configuration=self._configuration)
self.api = {{classname}}(api_client=used_api_client) # noqa: E501
self.api = {{httpMethod}}.ApiFor{{httpMethod}}(api_client=used_api_client) # noqa: E501

def tearDown(self):
pass

{{#each operation}}
def test_{{operationId}}(self):
"""Test case for {{{operationId}}}

{{#if summary}}
{{{summary}}} # noqa: E501
{{/if}}
"""
from {{packageName}}.{{apiPackage}}.{{classFilename}}_endpoints import {{operationId}} as endpoint_module
{{#each responses}}
{{#if @first}}
response_status = {{code}}
response_status = {{code}}
{{#if content}}
{{#each content}}
accept_content_type = '{{{@key}}}'

{{#if this.testCases}}

{{#each testCases}}
{{#with this }}
# test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}
def test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}(self):
# {{description}}
accept_content_type = '{{{../@key}}}'

with patch.object(urllib3.PoolManager, 'request') as mock_request:
payload = (
{{#with data}}
Expand All @@ -55,27 +52,27 @@ class {{#with operations}}Test{{classname}}(ApiTestMixin, unittest.TestCase):
)
mock_request.return_value = self.response(
self.json_bytes(payload),
status=response_status
status=self.response_status
)
{{#if valid}}
{{> api_test_partial }}

assert isinstance(api_response.response, urllib3.HTTPResponse)
assert isinstance(api_response.body, endpoint_module.{{schema.baseName}})
deserialized_response_body = endpoint_module.{{schema.baseName}}._from_openapi_data(
assert isinstance(api_response.body, {{httpMethod}}.{{schema.baseName}})
deserialized_response_body = {{httpMethod}}.{{schema.baseName}}._from_openapi_data(
payload,
_configuration=self._configuration
)
assert api_response.body == deserialized_response_body
{{else}}
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
self.api.{{operationId}}(
self.api.{{httpMethod}}(
accept_content_types=(accept_content_type,)
)
self.assert_pool_manager_request_called_with(
mock_request,
self._configuration.host + '{{{path}}}',
method='{{httpMethod}}',
method='{{httpMethod}}'.upper(),
content_type=None,
accept_content_type=accept_content_type,
)
Expand All @@ -87,20 +84,20 @@ class {{#with operations}}Test{{classname}}(ApiTestMixin, unittest.TestCase):
{{/if}}
{{/each}}
{{else}}
response_body = ''
response_body = ''
{{/if}}
{{/if}}
{{/each}}
{{#if bodyParam}}
{{#with bodyParam}}
{{#if required}}
{{#each content}}
content_type = '{{{@key}}}'
{{#if this.testCases}}

{{#each testCases}}
{{#with this }}
# test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}
def test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}(self):
content_type = '{{{../@key}}}'
# {{description}}
with patch.object(urllib3.PoolManager, 'request') as mock_request:
payload = (
Expand All @@ -109,25 +106,25 @@ class {{#with operations}}Test{{classname}}(ApiTestMixin, unittest.TestCase):
{{/with}}
)
{{#if valid}}
body = endpoint_module.{{schema.baseName}}._from_openapi_data(
body = {{httpMethod}}.{{schema.baseName}}._from_openapi_data(
payload,
_configuration=self._configuration
)
mock_request.return_value = self.response(
self.json_bytes(response_body),
status=response_status
self.json_bytes(self.response_body),
status=self.response_status
)
{{> api_test_partial }}

assert isinstance(api_response.response, urllib3.HTTPResponse)
assert isinstance(api_response.body, schemas.Unset)
{{else}}
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
body = endpoint_module.{{schema.baseName}}._from_openapi_data(
body = {{httpMethod}}.{{schema.baseName}}._from_openapi_data(
payload,
_configuration=self._configuration
)
self.api.{{operationId}}(body=body)
self.api.{{httpMethod}}(body=body)
{{/if}}
{{/with}}

Expand All @@ -138,10 +135,8 @@ class {{#with operations}}Test{{classname}}(ApiTestMixin, unittest.TestCase):
{{/if}}
{{/with}}
{{else}}
pass
{{/if}}

{{/each}}
{{/with}}

if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
api_response = self.api.{{operationId}}(
api_response = self.api.{{httpMethod}}(
{{#if bodyParam}}
body=body,
content_type=content_type
Expand All @@ -10,7 +10,7 @@ api_response = self.api.{{operationId}}(
self.assert_pool_manager_request_called_with(
mock_request,
self._configuration.host + '{{{path}}}',
method='{{httpMethod}}',
method='{{httpMethod}}'.upper(),
{{#if bodyParam}}
body=self.json_bytes(payload),
content_type=content_type,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{{#if pathItem.get}}
from {{packageName}}.paths.{{pathModule}}.get import ApiForget
{{/if}}
{{#if pathItem.put}}
from {{packageName}}.paths.{{pathModule}}.put import ApiForput
{{/if}}
{{#if pathItem.post}}
from {{packageName}}.paths.{{pathModule}}.post import ApiForpost
{{/if}}
{{#if pathItem.delete}}
from {{packageName}}.paths.{{pathModule}}.delete import ApiFordelete
{{/if}}
{{#if pathItem.options}}
from {{packageName}}.paths.{{pathModule}}.options import ApiForoptions
{{/if}}
{{#if pathItem.head}}
from {{packageName}}.paths.{{pathModule}}.head import ApiForhead
{{/if}}
{{#if pathItem.patch}}
from {{packageName}}.paths.{{pathModule}}.patch import ApiForpatch
{{/if}}
{{#if pathItem.trace}}
from {{packageName}}.paths.{{pathModule}}.trace import ApiFortrace
{{/if}}


class {{apiClassName}}(
{{#if pathItem.get}}
ApiForget,
{{/if}}
{{#if pathItem.put}}
ApiForput,
{{/if}}
{{#if pathItem.post}}
ApiForpost,
{{/if}}
{{#if pathItem.delete}}
ApiFordelete,
{{/if}}
{{#if pathItem.options}}
ApiForoptions,
{{/if}}
{{#if pathItem.head}}
ApiForhead,
{{/if}}
{{#if pathItem.patch}}
ApiForpatch,
{{/if}}
{{#if pathItem.trace}}
ApiFortrace,
{{/if}}):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typing

from {{packageName}}.paths import PathValues
{{#each pathModuleToApiClassname}}
from {{packageName}}.apis.paths.{{@key}} import {{this}}
{{/each}}

PathToApi = typing.TypedDict(
'PathToApi',
{
{{#each pathEnumToApiClassname}}
PathValues.{{@key}}: {{this}},
{{/each}}
}
)

path_to_api = PathToApi(
{
{{#each pathEnumToApiClassname}}
PathValues.{{@key}}: {{this}},
{{/each}}
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typing

from {{packageName}}.apis.tags import TagValues
{{#each tagModuleNameToApiClassname}}
from {{packageName}}.apis.tags.{{@key}} import {{this}}
{{/each}}

TagToApi = typing.TypedDict(
'TagToApi',
{
{{#each tagEnumToApiClassname}}
TagValues.{{@key}}: {{this}},
{{/each}}
}
)

tag_to_api = TagToApi(
{
{{#each tagEnumToApiClassname}}
TagValues.{{@key}}: {{this}},
{{/each}}
}
)
Loading