Skip to content

Commit

Permalink
chore: update pydantic to v2 (APPS-5285) (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-stankiewicz authored Nov 30, 2023
1 parent 9e22fc9 commit edbba43
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 141 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v11.0.0
- Added support for Pydantic v2
- [breaking change] `cookie_hash`, `last_click_time` and `last_impression_time` fields in `Conversion` schema are now nullable
- [breaking change] fixed naming convention compliance for fields in schemas: `rateCardId` (`UserInfo`) is now `rate_card_id` and `customProperties` (`Offer`) is now `custom_properties`

# v10.1.0
- Removed [inflection](https://pypi.org/project/inflection/) from the project dependencies
- Added `camelize` and `underscore` helper functions
Expand Down
270 changes: 184 additions & 86 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rtbhouse-sdk"
version = "10.2.0"
version = "11.0.0"
description = "RTB House SDK"
authors = ["RTB House Apps Team <[email protected]>"]
license = "BSD License"
Expand All @@ -26,8 +26,8 @@ priority = "primary"
[tool.poetry.dependencies]
python = ">=3.8.1, <4.0"

httpx = "^0.25.0"
pydantic = "^1.9.0"
httpx = "^0.25.2"
pydantic = ">=1.9, <3.0"

[tool.poetry.group.dev.dependencies]
black = "^23.0.0"
Expand Down
2 changes: 1 addition & 1 deletion rtbhouse_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""RTB House Python SDK."""
__version__ = "10.2.0"
__version__ = "11.0.0"
4 changes: 4 additions & 0 deletions rtbhouse_sdk/utils.py → rtbhouse_sdk/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Utils used in SDK."""
import re

from pydantic.version import VERSION as PYDANTIC_VERSION

PYDANTIC_V1 = PYDANTIC_VERSION[0] == "1"


def camelize(word: str, uppercase_first_letter: bool = True) -> str:
"""
Expand Down
102 changes: 58 additions & 44 deletions rtbhouse_sdk/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

from pydantic import BaseModel

from .utils import camelize
from ._utils import PYDANTIC_V1, camelize

to_camel_case = partial(camelize, uppercase_first_letter=False)
if not PYDANTIC_V1:
from pydantic import ConfigDict


to_camel_case = partial(
camelize,
uppercase_first_letter=False,
)


class CountConvention(str, Enum):
Expand Down Expand Up @@ -93,8 +100,15 @@ class SubcampaignsFilter(str, Enum):


class CamelizedBaseModel(BaseModel):
class Config:
alias_generator = to_camel_case
if PYDANTIC_V1:

class Config:
alias_generator = to_camel_case

else:
model_config = ConfigDict( # type: ignore
alias_generator=to_camel_case,
)


class UserInfo(CamelizedBaseModel):
Expand All @@ -121,16 +135,16 @@ class Campaign(CamelizedBaseModel):
creative_ids: List[int]
status: str
updated_at: Optional[datetime]
rateCardId: str
rate_card_id: str
is_editable: bool
advertiser_limits: Optional[Dict[str, Optional[str]]]
advertiser_limits: Optional[Dict[str, Optional[int]]] = None


class InvoiceData(BaseModel):
vat_number: str
company_name: str
street1: str
street2: Optional[str]
street2: Optional[str] = None
postal_code: str
city: str
country: str
Expand Down Expand Up @@ -161,7 +175,7 @@ class Offer(CamelizedBaseModel):
name: str
price: float
category_name: str
customProperties: Dict[str, str]
custom_properties: Dict[str, str]
updated_at: str
status: str

Expand Down Expand Up @@ -199,46 +213,46 @@ class Conversion(CamelizedBaseModel):
conversion_class: Optional[str]
conversion_value: float
commission_value: float
cookie_hash: str
cookie_hash: Optional[str]
conversion_time: datetime
last_click_time: datetime
last_impression_time: datetime
last_click_time: Optional[datetime]
last_impression_time: Optional[datetime]


class Stats(CamelizedBaseModel):
# from GroupBy
hour: Optional[int]
day: Optional[date]
week: Optional[str]
month: Optional[str]
year: Optional[str]
advertiser: Optional[str]
subcampaign: Optional[str]
subcampaign_hash: Optional[str]
user_segment: Optional[str]
device_type: Optional[str]
creative: Optional[str]
category: Optional[str]
category_name: Optional[str]
country: Optional[str]
placement: Optional[str]
hour: Optional[int] = None
day: Optional[date] = None
week: Optional[str] = None
month: Optional[str] = None
year: Optional[str] = None
advertiser: Optional[str] = None
subcampaign: Optional[str] = None
subcampaign_hash: Optional[str] = None
user_segment: Optional[str] = None
device_type: Optional[str] = None
creative: Optional[str] = None
category: Optional[str] = None
category_name: Optional[str] = None
country: Optional[str] = None
placement: Optional[str] = None

# from Metric
campaign_cost: Optional[float]
imps_count: Optional[int]
ecpm: Optional[float]
clicks_count: Optional[int]
ecpc: Optional[float]
ctr: Optional[float]
conversions_count: Optional[int]
ecpa: Optional[float]
cr: Optional[float]
conversions_value: Optional[float]
roas: Optional[float]
ecps: Optional[float]
video_complete_views: Optional[int]
ecpv: Optional[float]
vcr: Optional[float]
viewability: Optional[float]
user_frequency: Optional[float]
user_reach: Optional[float]
campaign_cost: Optional[float] = None
imps_count: Optional[int] = None
ecpm: Optional[float] = None
clicks_count: Optional[int] = None
ecpc: Optional[float] = None
ctr: Optional[float] = None
conversions_count: Optional[int] = None
ecpa: Optional[float] = None
cr: Optional[float] = None
conversions_value: Optional[float] = None
roas: Optional[float] = None
ecps: Optional[float] = None
video_complete_views: Optional[int] = None
ecpv: Optional[float] = None
vcr: Optional[float] = None
viewability: Optional[float] = None
user_frequency: Optional[float] = None
user_reach: Optional[float] = None
5 changes: 0 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@ exclude =
.venv
per-file-ignores =
**/__init__.py:F401,F403

[mypy]
ignore_missing_imports = True
check_untyped_defs = True
junit_xml = .mypy.xml
2 changes: 1 addition & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Tests for schema and it's utilities."""
from itertools import chain

from rtbhouse_sdk._utils import underscore
from rtbhouse_sdk.schema import Stats, StatsGroupBy, StatsMetric
from rtbhouse_sdk.utils import underscore


def test_stats_schema_is_up_to_date() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tests for utilities"""
import pytest

from rtbhouse_sdk.utils import camelize, underscore
from rtbhouse_sdk._utils import camelize, underscore


@pytest.mark.parametrize(
Expand Down

0 comments on commit edbba43

Please sign in to comment.