Skip to content

Commit

Permalink
test working for pydantic responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Nov 1, 2023
1 parent f0817a9 commit 0f4671f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,20 @@ class BaseApi(api_client.Api):
{{> endpoint_operation_impl}}


class {{operationIdCamelCase}}(BaseApi):
class {{#prstv2}}Raw{{/prstv2}}{{operationIdCamelCase}}(BaseApi):
# this class is used by api classes that refer to endpoints with operationId fn names

{{> endpoint_interface_impl methodName=operationId}}
{{#prstv2}}


class {{operationIdCamelCase}}(BaseApi):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.raw = Raw{{operationIdCamelCase}}(*args, **kwargs)

{{> endpoint_pydantic_impl}}
{{/prstv2}}


class ApiFor{{httpMethod}}(BaseApi):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
{{/if}}
stream: bool = False,
{{/if}}
{{#if interface}}
{{#if validateArg}}
validate: bool = False,
{{/if}}
{{/if}}
){{#unless omitReturnType}} -> {{#if isMappedArgs}}api_client.MappedArgs{{else}}typing.Union[
{{#each responses}}
{{#if isDefault}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{#if isDeprecated}}@api_client.DeprecationWarningOnce(prefix="{{clientApiName}}")
{{/if}}async def a{{operationId}}(
{{> endpoint_args async=true isOverload=false skipDeserialization="null" contentType="null" interface=true omitReturnType=true validateArg=true}}
raw_response = await self.raw.a{{operationId}}(
{{> endpoint_args_call_mapped_args}}
)
if validate:
return {{returnType}}Pydantic(**raw_response.body)
return {{returnType}}Pydantic.model_construct(**raw_response.body)

{{#if isDeprecated}}@api_client.DeprecationWarningOnce(prefix="{{clientApiName}}")
{{/if}}def {{operationId}}(
{{> endpoint_args isOverload=false skipDeserialization="null" contentType="null" interface=true omitReturnType=true validateArg=true}}
raw_response = self.raw.{{operationId}}(
{{> endpoint_args_call_mapped_args}}
)
if validate:
return {{returnType}}Pydantic(**raw_response.body)
return {{returnType}}Pydantic.model_construct(**raw_response.body)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
{{>partial_header}}

from datetime import datetime, date
import typing
from enum import Enum
from typing_extensions import TypedDict, Literal
from pydantic import BaseModel, Field
{{#each models}}
{{#with model}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{> type_templates/type_object}}
{{> type_templates/type_composed}}
{{> type_templates/type_enum}}
{{> type_templates/type_list}}
{{> type_templates/type_map}}
{{> type_templates/type_primitive}}
{{> pydantic_templates/type_object}}
{{> pydantic_templates/type_composed}}
{{> pydantic_templates/type_enum}}
{{> pydantic_templates/type_list}}
{{> pydantic_templates/type_map}}
{{> pydantic_templates/type_primitive}}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
{{#if hasVars}}
{{#if useDictionaryBasedTypedDict}}
Required{{classname}} = TypedDict("Required{{classname}}", {
{{#each requiredVars}}
{{#if description}}
# {{{description}}}
{{/if}}
"{{baseName}}": {{dataType}},
{{#unless @last}}

{{/unless}}
{{/each}}
})
{{else}}
class Required{{classname}}(TypedDict):
class {{classname}}(BaseModel):
{{#each requiredVars}}
{{#if description}}
# {{{description}}}
Expand All @@ -22,39 +9,18 @@ class Required{{classname}}(TypedDict):

{{/unless}}
{{/each}}
{{#unless requiredVars}}
pass
{{/unless}}
{{/if}}

{{#if useDictionaryBasedTypedDict}}
Optional{{classname}} = TypedDict("Optional{{classname}}", {
{{#each optionalVars}}
{{#if description}}
# {{{description}}}
{{/if}}
"{{baseName}}": {{dataType}},
{{#unless @last}}

{{/unless}}
{{/each}}
}, total=False)
{{else}}
class Optional{{classname}}(TypedDict, total=False):
{{#each optionalVars}}
{{#if description}}
# {{{description}}}
{{/if}}
{{baseName}}: {{{dataType}}}
{{baseName}}: {{{dataType}}} = Field(None)
{{#unless @last}}

{{/unless}}
{{/each}}
{{#unless requiredVars}}
{{#unless optionalVars}}
pass
{{/unless}}
{{/if}}

class {{classname}}(Required{{classname}}, Optional{{classname}}):
pass
{{/unless}}
{{/if}}

0 comments on commit 0f4671f

Please sign in to comment.