Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions codegen/pkg/builder/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,37 @@ func (b *Builder) pathsToParamTypes(paths *v3.Paths) []Writable {

if len(opSpec.Parameters) > 0 {
fields := make([]Property, 0)
fieldTypes := make([]Writable, 0)
paramsTypeName := operationName + "Params"
for _, p := range opSpec.Parameters {
// path parameters are passed as a parameters to the generated method
if p.In == "path" || p.In == "header" {
continue
}

name := p.Name
alias := name
if p.GoLow().IsReference() {
name = strcase.ToCamel(strings.TrimPrefix(p.Schema.GetReference(), "#/components/schemas/"))
}

// NOTE: we should leave it as-is, but that doesn't work with 'include[]'
name = strings.ReplaceAll(name, "[]", "")

// NOTE: this also needs to be handled properly
name = strings.ReplaceAll(name, ".", "_")

typ := b.convertToValidPyType("", p.Schema)
alias := p.Name
name := parameterFieldName(alias)
typeName, types := b.genSchema(p.Schema, paramsTypeName+strcase.ToCamel(name))
fieldTypes = append(fieldTypes, types...)

fields = append(fields, Property{
Name: name,
SerializedName: alias,
Type: typ,
Type: typeName,
Optional: p.Required == nil || !*p.Required,
Comment: parameterPropertyDoc(p.Schema.Schema()),
})
}

if len(fields) != 0 {
paramsTypeName := operationName + "Params"
paramsTpl := ClassDeclaration{
Type: "struct",
Name: paramsTypeName,
Description: operationParamsDoc(paramsTypeName, opSpec),
Fields: fields,
}

paramTypes = append(paramTypes, fieldTypes...)
paramTypes = append(paramTypes, &paramsTpl)
}
}
Expand All @@ -114,6 +107,12 @@ func (b *Builder) pathsToParamTypes(paths *v3.Paths) []Writable {
return paramTypes
}

func parameterFieldName(name string) string {
name = strings.ReplaceAll(name, "[]", "")
name = strings.ReplaceAll(name, ".", "_")
return name
}

// pathsToResponseTypes generates response types for operations. This is responsible only for inlined
// schemas that are specific to the operation itself and are not references.
func (b *Builder) pathsToResponseTypes(paths *v3.Paths) []Writable {
Expand Down
6 changes: 1 addition & 5 deletions sumup/customers/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
from __future__ import annotations
from .._service import Resource, AsyncResource, HeaderTypes
from .._exceptions import APIError
from ..types import (
Customer,
PaymentInstrumentResponse,
PersonalDetails,
)
from ..types import Customer, PaymentInstrumentResponse, PersonalDetails
import httpx
import typing
import pydantic
Expand Down
7 changes: 1 addition & 6 deletions sumup/members/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
from .._service import Resource, AsyncResource, HeaderTypes
from .._exceptions import APIError
from .._secret import Secret
from ..types import (
Attributes,
Member,
MembershipStatus,
Metadata,
)
from ..types import Attributes, Member, MembershipStatus, Metadata
import httpx
import typing
import pydantic
Expand Down
2 changes: 2 additions & 0 deletions sumup/memberships/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .resource import (
MembershipsResource,
AsyncMembershipsResource,
ListMembershipsParamsResourceParentType,
ListMembershipsParams,
ListMemberships200Response,
)
Expand All @@ -20,6 +21,7 @@
__all__ = [
"MembershipsResource",
"AsyncMembershipsResource",
"ListMembershipsParamsResourceParentType",
"ListMembershipsParams",
"ListMemberships200Response",
"Attributes",
Expand Down
14 changes: 8 additions & 6 deletions sumup/memberships/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
from __future__ import annotations
from .._service import Resource, AsyncResource, HeaderTypes
from .._exceptions import APIError
from ..types import (
Membership,
MembershipStatus,
ResourceType,
)
from ..types import Membership, MembershipStatus, ResourceType
import httpx
import typing
import pydantic


class ListMembershipsParamsResourceParentType(pydantic.BaseModel):
"""
ListMembershipsParamsResourceParentType is a schema definition.
"""


class ListMembershipsParams(pydantic.BaseModel):
"""
ListMembershipsParams: query parameters for ListMemberships
Expand Down Expand Up @@ -49,7 +51,7 @@ class ListMembershipsParams(pydantic.BaseModel):
validation_alias=pydantic.AliasChoices("resource.parent.id", "resource_parent_id"),
)

resource_parent_type: typing.Optional[ResourceType] = pydantic.Field(
resource_parent_type: typing.Optional[ListMembershipsParamsResourceParentType] = pydantic.Field(
default=None,
serialization_alias="resource.parent.type",
validation_alias=pydantic.AliasChoices("resource.parent.type", "resource_parent_type"),
Expand Down
6 changes: 1 addition & 5 deletions sumup/merchants/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
from __future__ import annotations
from .._service import Resource, AsyncResource, HeaderTypes
from .._exceptions import APIError
from ..types import (
ListPersonsResponseBody,
Merchant,
Person,
)
from ..types import ListPersonsResponseBody, Merchant, Person
import httpx
import typing
import pydantic
Expand Down
8 changes: 8 additions & 0 deletions sumup/payouts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from .resource import (
PayoutsResource,
AsyncPayoutsResource,
ListPayoutsV1ParamsFormat,
ListPayoutsV1ParamsOrder,
ListPayoutsV1Params,
ListPayoutsParamsFormat,
ListPayoutsParamsOrder,
ListPayoutsParams,
)
from ..types import (
Expand All @@ -16,7 +20,11 @@
__all__ = [
"PayoutsResource",
"AsyncPayoutsResource",
"ListPayoutsV1ParamsFormat",
"ListPayoutsV1ParamsOrder",
"ListPayoutsV1Params",
"ListPayoutsParamsFormat",
"ListPayoutsParamsOrder",
"ListPayoutsParams",
"Error",
"ErrorExtended",
Expand Down
17 changes: 13 additions & 4 deletions sumup/payouts/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import pydantic
import typing_extensions

ListPayoutsV1ParamsFormat = typing.Union[typing.Literal["csv", "json"], str]

ListPayoutsV1ParamsOrder = typing.Union[typing.Literal["asc", "desc"], str]


class ListPayoutsV1Params(pydantic.BaseModel):
"""
Expand All @@ -19,11 +23,16 @@ class ListPayoutsV1Params(pydantic.BaseModel):

start_date: datetime.date

format: typing.Optional[str] = None
format: typing.Optional[ListPayoutsV1ParamsFormat] = None

limit: typing.Optional[int] = None

order: typing.Optional[str] = None
order: typing.Optional[ListPayoutsV1ParamsOrder] = None


ListPayoutsParamsFormat = typing.Union[typing.Literal["csv", "json"], str]

ListPayoutsParamsOrder = typing.Union[typing.Literal["asc", "desc"], str]


class ListPayoutsParams(pydantic.BaseModel):
Expand All @@ -35,11 +44,11 @@ class ListPayoutsParams(pydantic.BaseModel):

start_date: datetime.date

format: typing.Optional[str] = None
format: typing.Optional[ListPayoutsParamsFormat] = None

limit: typing.Optional[int] = None

order: typing.Optional[str] = None
order: typing.Optional[ListPayoutsParamsOrder] = None


class PayoutsResource(Resource):
Expand Down
4 changes: 1 addition & 3 deletions sumup/receipts/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from __future__ import annotations
from .._service import Resource, AsyncResource, HeaderTypes
from .._exceptions import APIError
from ..types import (
Receipt,
)
from ..types import Receipt
import httpx
import typing
import pydantic
Expand Down
12 changes: 12 additions & 0 deletions sumup/transactions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
RefundTransactionBody,
GetTransactionV21Params,
GetTransactionParams,
ListTransactionsV21ParamsOrder,
ListTransactionsV21ParamsStatuse,
ListTransactionsV21ParamsType,
ListTransactionsV21Params,
ListTransactionsParamsOrder,
ListTransactionsParamsStatuse,
ListTransactionsParamsType,
ListTransactionsParams,
ListTransactionsV21200Response,
ListTransactions200Response,
Expand Down Expand Up @@ -46,7 +52,13 @@
"RefundTransactionBody",
"GetTransactionV21Params",
"GetTransactionParams",
"ListTransactionsV21ParamsOrder",
"ListTransactionsV21ParamsStatuse",
"ListTransactionsV21ParamsType",
"ListTransactionsV21Params",
"ListTransactionsParamsOrder",
"ListTransactionsParamsStatuse",
"ListTransactionsParamsType",
"ListTransactionsParams",
"ListTransactionsV21200Response",
"ListTransactions200Response",
Expand Down
32 changes: 26 additions & 6 deletions sumup/transactions/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ class GetTransactionParams(pydantic.BaseModel):
transaction_code: typing.Optional[str] = None


ListTransactionsV21ParamsOrder = typing.Union[typing.Literal["ascending", "descending"], str]

ListTransactionsV21ParamsStatuse = typing.Union[
typing.Literal["CANCELLED", "CHARGE_BACK", "FAILED", "REFUNDED", "SUCCESSFUL"], str
]

ListTransactionsV21ParamsType = typing.Union[
typing.Literal["CHARGE_BACK", "PAYMENT", "REFUND"], str
]


class ListTransactionsV21Params(pydantic.BaseModel):
"""
ListTransactionsV21Params: query parameters for ListTransactionsV2.1
Expand All @@ -78,23 +89,32 @@ class ListTransactionsV21Params(pydantic.BaseModel):

oldest_time: typing.Optional[datetime.datetime] = None

order: typing.Optional[str] = None
order: typing.Optional[ListTransactionsV21ParamsOrder] = None

payment_types: typing.Optional[list[PaymentType]] = None

statuses: typing.Optional[list[str]] = pydantic.Field(
statuses: typing.Optional[list[ListTransactionsV21ParamsStatuse]] = pydantic.Field(
default=None,
serialization_alias="statuses[]",
validation_alias=pydantic.AliasChoices("statuses[]", "statuses"),
)

transaction_code: typing.Optional[str] = None

types: typing.Optional[list[str]] = None
types: typing.Optional[list[ListTransactionsV21ParamsType]] = None

users: typing.Optional[list[str]] = None


ListTransactionsParamsOrder = typing.Union[typing.Literal["ascending", "descending"], str]

ListTransactionsParamsStatuse = typing.Union[
typing.Literal["CANCELLED", "CHARGE_BACK", "FAILED", "REFUNDED", "SUCCESSFUL"], str
]

ListTransactionsParamsType = typing.Union[typing.Literal["CHARGE_BACK", "PAYMENT", "REFUND"], str]


class ListTransactionsParams(pydantic.BaseModel):
"""
ListTransactionsParams: query parameters for ListTransactions
Expand All @@ -112,19 +132,19 @@ class ListTransactionsParams(pydantic.BaseModel):

oldest_time: typing.Optional[datetime.datetime] = None

order: typing.Optional[str] = None
order: typing.Optional[ListTransactionsParamsOrder] = None

payment_types: typing.Optional[list[PaymentType]] = None

statuses: typing.Optional[list[str]] = pydantic.Field(
statuses: typing.Optional[list[ListTransactionsParamsStatuse]] = pydantic.Field(
default=None,
serialization_alias="statuses[]",
validation_alias=pydantic.AliasChoices("statuses[]", "statuses"),
)

transaction_code: typing.Optional[str] = None

types: typing.Optional[list[str]] = None
types: typing.Optional[list[ListTransactionsParamsType]] = None

users: typing.Optional[list[str]] = None

Expand Down
12 changes: 12 additions & 0 deletions tests/test_query_params.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import httpx
import pytest
import typing

from sumup.transactions import ListTransactionsV21Params

Expand Down Expand Up @@ -34,3 +35,14 @@ def handler(request: httpx.Request) -> httpx.Response:
request = captured_request["request"]
assert request.url.path == "/v2.1/merchants/merchant-123/transactions/history"
assert list(request.url.params.multi_items()) == expected_query_items


def test_transactions_list_query_param_enums_are_typed():
order_annotation = ListTransactionsV21Params.model_fields["order"].annotation

assert typing.get_origin(order_annotation) is typing.Union
assert typing.get_args(order_annotation) == (
typing.Literal["ascending", "descending"],
str,
type(None),
)
Loading