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
4 changes: 4 additions & 0 deletions zero_friction/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Unified Zero Friction SDK package."""

from .patches import apply_patches

apply_patches()
4 changes: 4 additions & 0 deletions zero_friction/patches/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Runtime patches applied to generated SDK clients."""

from .core import apply_patches

__all__ = ["apply_patches"]
36 changes: 36 additions & 0 deletions zero_friction/patches/apply_patched_country_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Apply the CountryCode compatibility patch."""


def apply_patched_country_code() -> None:
"""Apply the patched country code enum.

Replace the generated SDK enum with a version that accepts unknown values.
"""
import importlib

from masterdata_client import models as models_pkg

from .core import rebind_symbol_everywhere
from .patched_country_code import PatchedCountryCode

country_mod = importlib.import_module("masterdata_client.models.country_code")

# Replace original enum
country_mod.CountryCode = PatchedCountryCode # type: ignore[attr-defined]

# Replace imports in generated modules
rebind_symbol_everywhere(
models_pkg,
"CountryCode",
PatchedCountryCode,
)

# Fix already-created Pydantic models
address_mod = importlib.import_module("masterdata_client.models.address_dto")

AddressDTO = address_mod.AddressDTO

# Pydantic v2 builds validators when the model class is created.
# Rebuild the model after replacing the enum annotation.
AddressDTO.model_fields["country"].annotation = PatchedCountryCode
AddressDTO.model_rebuild(force=True)
3 changes: 2 additions & 1 deletion zero_friction/patches/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def apply_patches() -> None:
from .apply_patched_billing_calculations_type_parameters_dto import (
apply_patched_billing_calculations_type_parameters_dto,
)
from .apply_patched_country_code import apply_patched_country_code

for fn in [
apply_patched_billing_calculations_type_parameters_dto,
# add future patch functions here
apply_patched_country_code,
]:
_apply_once(fn)

Expand Down
76 changes: 76 additions & 0 deletions zero_friction/patches/patched_country_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Patched country code enum allowing unknown API values."""

import json
from enum import Enum
from typing import Self


class PatchedCountryCode(str, Enum):
"""Patched country code enum.

Accepts country codes returned by the API that are not included in the generated SDK.
"""

AUT = "aut"
BEL = "bel"
BGR = "bgr"
CYP = "cyp"
CZE = "cze"
DNK = "dnk"
EST = "est"
FIN = "fin"
FRA = "fra"
DEU = "deu"
GRC = "grc"
HUN = "hun"
IRL = "irl"
ITA = "ita"
LVA = "lva"
LTU = "ltu"
LUX = "lux"
MLT = "mlt"
NLD = "nld"
POL = "pol"
PRT = "prt"
ROU = "rou"
SVK = "svk"
ESP = "esp"
SWE = "swe"
GBR = "gbr"
CHE = "che"
ISL = "isl"
NOR = "nor"
RUS = "rus"
BLR = "blr"
UKR = "ukr"
MDA = "mda"
SRB = "srb"
ALB = "alb"
MNE = "mne"
MKD = "mkd"
TUR = "tur"
LIE = "lie"
SMR = "smr"
AND = "and"
MCO = "mco"
BIH = "bih"
BRA = "bra"
USA = "usa"
SGP = "sgp"
ZAF = "zaf"
CUW = "cuw"

@classmethod
def _missing_(cls, value: object) -> Self:
"""Allow unknown country codes."""
value = str(value)

member = str.__new__(cls, value)
member._name_ = value.upper()
member._value_ = value
return member

@classmethod
def from_json(cls, json_str: str) -> Self:
"""Create a country code instance from JSON."""
return cls(json.loads(json_str))
Loading