Skip to content

Commit

Permalink
Make extra API schema fields accessible through Pydantic `model_extra…
Browse files Browse the repository at this point in the history
…` attribute
  • Loading branch information
pawelad committed Sep 11, 2024
1 parent 513a30e commit d443634
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog], and this project adheres to

## Unreleased

### Changed
- Make extra API schema fields accessible through Pydantic `model_extra` attribute.

### Fixed
- Never expect undocumented API fields to be present.

Expand Down
6 changes: 5 additions & 1 deletion src/pymonzo/accounts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime
from typing import List, Optional, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field

from pymonzo.accounts.enums import MonzoAccountCurrency, MonzoAccountType

Expand Down Expand Up @@ -37,6 +37,8 @@ class MonzoAccountOwner(BaseModel):
preferred_first_name: First name preferred by the user.
"""

model_config = ConfigDict(extra="allow")

# Undocumented in API docs
user_id: Optional[str] = None
preferred_name: Optional[str] = None
Expand Down Expand Up @@ -65,6 +67,8 @@ class MonzoAccount(BaseModel):
payment_details: Account payment details.
"""

model_config = ConfigDict(extra="allow")

id: str
description: str
created: datetime
Expand Down
6 changes: 5 additions & 1 deletion src/pymonzo/attachments/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class MonzoAttachment(BaseModel):
Expand All @@ -20,6 +20,8 @@ class MonzoAttachment(BaseModel):
created: The timestamp in UTC when the attachment was created.
"""

model_config = ConfigDict(extra="allow")

id: str
user_id: str
external_id: str
Expand All @@ -39,5 +41,7 @@ class MonzoAttachmentResponse(BaseModel):
upload_url: The URL to `POST` the file to when uploading.
"""

model_config = ConfigDict(extra="allow")

file_url: str
upload_url: str
4 changes: 3 additions & 1 deletion src/pymonzo/balance/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Optional

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

# Optional `rich` support
try:
Expand Down Expand Up @@ -41,6 +41,8 @@ class MonzoBalance(BaseModel):
local_spend: Local spend.
"""

model_config = ConfigDict(extra="allow")

balance: int
total_balance: int
currency: str
Expand Down
4 changes: 3 additions & 1 deletion src/pymonzo/feed/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Optional

from pydantic import BaseModel, HttpUrl
from pydantic import BaseModel, ConfigDict, HttpUrl


class MonzoBasicFeedItem(BaseModel):
Expand All @@ -24,6 +24,8 @@ class MonzoBasicFeedItem(BaseModel):
Defaults to standard app colours.
"""

model_config = ConfigDict(extra="allow")

title: str
image_url: HttpUrl
body: str
Expand Down
4 changes: 3 additions & 1 deletion src/pymonzo/pots/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime
from typing import Optional

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

# Optional `rich` support
try:
Expand Down Expand Up @@ -53,6 +53,8 @@ class MonzoPot(BaseModel):
has_virtual_cards: Whether the pot has linked virtual cards.
"""

model_config = ConfigDict(extra="allow")

id: str
name: str
style: str
Expand Down
6 changes: 5 additions & 1 deletion src/pymonzo/transactions/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime
from typing import Dict, Optional, Union

from pydantic import BaseModel, Field, field_validator
from pydantic import BaseModel, ConfigDict, Field, field_validator

from pymonzo.transactions.enums import (
MonzoTransactionCategory,
Expand Down Expand Up @@ -82,6 +82,8 @@ class MonzoTransactionMerchant(BaseModel):
created: Merchant creation date.
"""

model_config = ConfigDict(extra="allow")

id: str
group_id: str
name: str
Expand Down Expand Up @@ -197,6 +199,8 @@ class MonzoTransaction(BaseModel):
decline_reason: This is only present on declined transactions.
"""

model_config = ConfigDict(extra="allow")

amount: int
created: datetime
currency: str
Expand Down
8 changes: 7 additions & 1 deletion src/pymonzo/webhooks/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Monzo API 'webhooks' related schemas."""

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

from pymonzo.transactions import MonzoTransactionMerchant

Expand All @@ -17,6 +17,8 @@ class MonzoWebhook(BaseModel):
url: The URL we will send notifications to.
"""

model_config = ConfigDict(extra="allow")

id: str
account_id: str
url: str
Expand Down Expand Up @@ -50,6 +52,8 @@ class MonzoWebhookTransactionEvent(BaseModel):
merchant: Merchant information.
"""

model_config = ConfigDict(extra="allow")

account_id: str
amount: int
created: str
Expand All @@ -73,5 +77,7 @@ class MonzoWebhookEvent(BaseModel):
data: Webhook event data.
"""

model_config = ConfigDict(extra="allow")

type: str
data: MonzoWebhookTransactionEvent
4 changes: 3 additions & 1 deletion src/pymonzo/whoami/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Monzo API 'whoami' related schemas."""

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict

# Optional `rich` support
try:
Expand All @@ -23,6 +23,8 @@ class MonzoWhoAmI(BaseModel):
user_id: User ID.
"""

model_config = ConfigDict(extra="allow")

authenticated: bool
client_id: str
user_id: str
Expand Down

0 comments on commit d443634

Please sign in to comment.