Skip to content

Commit

Permalink
feat: API Sync (Nov 2024) (#65)
Browse files Browse the repository at this point in the history
* fix: Discount expires_at is now DateTime

* fix: Replace get_parameters with encoder/to_json
  • Loading branch information
davidgrayston-paddle authored Nov 8, 2024
1 parent 2def986 commit 8671665
Show file tree
Hide file tree
Showing 50 changed files with 190 additions and 212 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx&utm_medium=paddle-python-sdk) for information about changes to the Paddle Billing platform, the Paddle API, and other developer tools.

## [Unreleased]

### Changed

- `paddle_billing.Resources.Discounts.Operations.CreateDiscount` `expires_at` is now `paddle_billing.Entities.DateTime`
- `paddle_billing.Resources.Discounts.Operations.UpdateDiscount` `expires_at` is now `paddle_billing.Entities.DateTime`

### Removed
- `get_parameters()` method on request operation classes is now removed or replaced by `to_json()` (see UPGRADING.md for further details)

## 0.3.2 - 2024-11-07

### Fixed
Expand Down
60 changes: 60 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,66 @@

All breaking changes prior to v1 will be documented in this file to assist with upgrading.

## Unreleased

### 1. Unused `get_parameters()` method was removed from request operation classes

`get_parameters()` methods returned the data used for operation request payloads, but is now removed or replaced by `to_json()`. This method was intended to be internal, so should not require any changes.

`get_parameters()` method was removed from the following classes:
- `paddle_billing.Entities.Reports.ReportFilter`
- `paddle_billing.Entities.Shared.CustomData`
- `paddle_billing.Entities.Subscriptions.SubscriptionItems`
- `paddle_billing.Entities.Subscriptions.SubscriptionItemsWithPrice`
- `paddle_billing.Notifications.Entities.Reports.ReportFilter`
- `paddle_billing.Notifications.Entities.Shared.CustomData`
- `paddle_billing.Resources.Addresses.Operations.CreateAddress`
- `paddle_billing.Resources.Addresses.Operations.UpdateAddress`
- `paddle_billing.Resources.Adjustments.Operations.CreateAdjustment`
- `paddle_billing.Resources.Businesses.Operations.CreateBusiness`
- `paddle_billing.Resources.Businesses.Operations.UpdateBusiness`
- `paddle_billing.Resources.Customers.Operations.CreateCustomer`
- `paddle_billing.Resources.Customers.Operations.UpdateCustomer`
- `paddle_billing.Resources.Discounts.Operations.CreateDiscount`
- `paddle_billing.Resources.Discounts.Operations.UpdateDiscount`
- `paddle_billing.Resources.NotificationSettings.Operations.CreateNotificationSetting`
- `paddle_billing.Resources.NotificationSettings.Operations.UpdateNotificationSetting`
- `paddle_billing.Resources.Prices.Operations.CreatePrice`
- `paddle_billing.Resources.Prices.Operations.UpdatePrice`
- `paddle_billing.Resources.PricingPreviews.Operations.PreviewPrice`
- `paddle_billing.Resources.Products.Operations.CreateProduct`
- `paddle_billing.Resources.Products.Operations.UpdateProduct`
- `paddle_billing.Resources.Reports.Operations.CreateReport` subclasses:
- `paddle_billing.Resources.Reports.Operations.CreateAdjustmentsReport`
- `paddle_billing.Resources.Reports.Operations.CreateDiscountsReport`
- `paddle_billing.Resources.Reports.Operations.CreateProductsAndPricesReport`
- `paddle_billing.Resources.Reports.Operations.CreateTransactionsReport`
- `paddle_billing.Resources.Reports.Operations.Filters.Filter` subclasses:
- `paddle_billing.Resources.Reports.Operations.Filters.AdjustmentActionFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.AdjustmentStatusFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.CollectionModeFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.CurrencyCodeFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.DiscountStatusFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.DiscountTypeFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.Filter`
- `paddle_billing.Resources.Reports.Operations.Filters.PriceStatusFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.PriceTypeFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.PriceUpdatedAtFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.ProductStatusFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.ProductTypeFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.ProductUpdatedAtFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.TransactionOriginFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.TransactionStatusFilter`
- `paddle_billing.Resources.Reports.Operations.Filters.UpdatedAtFilter`
- `paddle_billing.Resources.Subscriptions.Operations.CancelSubscription`
- `paddle_billing.Resources.Subscriptions.Operations.CreateOneTimeCharge`
- `paddle_billing.Resources.Subscriptions.Operations.PauseSubscription`
- `paddle_billing.Resources.Subscriptions.Operations.PreviewOneTimeCharge`
- `paddle_billing.Resources.Subscriptions.Operations.ResumeSubscription`
- `paddle_billing.Resources.Transactions.Operations.PreviewTransactionByAddress`
- `paddle_billing.Resources.Transactions.Operations.PreviewTransactionByCustomer`
- `paddle_billing.Resources.Transactions.Operations.PreviewTransactionByIP`

## v0.3.0

### 1. `AvailablePaymentMethods` has been replaced by `PaymentMethodType`.
Expand Down
18 changes: 6 additions & 12 deletions paddle_billing/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@

class PayloadEncoder(JSONEncoder):
def default(self, z):
if isinstance(z, Undefined):
return None

if hasattr(z, "to_json") and callable(z.to_json):
return z.to_json()

if is_dataclass(z):
data = {}
for field in fields(z):
data[field.name] = getattr(z, field.name)

return FiltersUndefined.filter_undefined_values(data)

if isinstance(z, Undefined):
return None

if hasattr(z, "to_json") and callable(z.to_json):
return z.to_json()

return super().default(z)


Expand Down Expand Up @@ -183,17 +183,11 @@ def get_raw(self, url: str, parameters: HasParameters | dict = None) -> Response
def post_raw(
self, url: str, payload: dict | Operation | None = None, parameters: HasParameters | dict | None = None
) -> Response:
if isinstance(payload, dict):
payload = FiltersUndefined.filter_undefined_values(payload) # Strip Undefined items from the dict

url = Client.format_uri_parameters(url, parameters) if parameters else url

return self._make_request("POST", url, payload)

def patch_raw(self, url: str, payload: dict | Operation | None) -> Response:
if isinstance(payload, dict):
payload = FiltersUndefined.filter_undefined_values(payload) # Strip Undefined items from the dict

return self._make_request("PATCH", url, payload)

def delete_raw(self, url: str) -> Response:
Expand Down
5 changes: 1 addition & 4 deletions paddle_billing/Entities/Reports/ReportFilter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Entities.Reports import ReportFilterName, ReportFilterOperator

Expand All @@ -13,6 +13,3 @@ class ReportFilter:
@staticmethod
def from_dict(data: dict) -> ReportFilter:
return ReportFilter(**data)

def get_parameters(self) -> dict:
return asdict(self)
3 changes: 0 additions & 3 deletions paddle_billing/Entities/Shared/CustomData.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ class CustomData:
def __init__(self, data: dict | list):
self.data = data

def get_parameters(self):
return self.data

def to_json(self):
return self.data
5 changes: 1 addition & 4 deletions paddle_billing/Entities/Subscriptions/SubscriptionItems.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from dataclasses import asdict, dataclass
from dataclasses import dataclass


@dataclass
Expand All @@ -13,6 +13,3 @@ def from_dict(data: dict) -> SubscriptionItems:
price_id=data["price_id"],
quantity=data["quantity"],
)

def get_parameters(self) -> dict:
return asdict(self)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Entities.Subscriptions.SubscriptionNonCatalogPrice import SubscriptionNonCatalogPrice
from paddle_billing.Entities.Subscriptions.SubscriptionNonCatalogPriceWithProduct import (
Expand All @@ -18,6 +18,3 @@ def from_dict(data: dict) -> SubscriptionItemsWithPrice:
price=data["price"],
quantity=data["quantity"],
)

def get_parameters(self) -> dict:
return asdict(self)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Notifications.Entities.Reports import ReportFilterName, ReportFilterOperator

Expand All @@ -13,6 +13,3 @@ class ReportFilter:
@staticmethod
def from_dict(data: dict) -> ReportFilter:
return ReportFilter(**data)

def get_parameters(self) -> dict:
return asdict(self)
2 changes: 1 addition & 1 deletion paddle_billing/Notifications/Entities/Shared/CustomData.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
class CustomData:
data: dict | list # JSON serializable Python types

def get_parameters(self):
def to_json(self):
return self.data
6 changes: 2 additions & 4 deletions paddle_billing/Resources/Addresses/AddressesClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ def get(self, customer_id: str, address_id: str) -> Address:
return Address.from_dict(parser.get_data())

def create(self, customer_id: str, operation: CreateAddress) -> Address:
self.response = self.client.post_raw(f"/customers/{customer_id}/addresses", operation.get_parameters())
self.response = self.client.post_raw(f"/customers/{customer_id}/addresses", operation)
parser = ResponseParser(self.response)

return Address.from_dict(parser.get_data())

def update(self, customer_id: str, address_id: str, operation: UpdateAddress) -> Address:
self.response = self.client.patch_raw(
f"/customers/{customer_id}/addresses/{address_id}", operation.get_parameters()
)
self.response = self.client.patch_raw(f"/customers/{customer_id}/addresses/{address_id}", operation)
parser = ResponseParser(self.response)

return Address.from_dict(parser.get_data())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Operation import Operation
from paddle_billing.Undefined import Undefined
from paddle_billing.Entities.Shared import CountryCode, CustomData


@dataclass
class CreateAddress:
class CreateAddress(Operation):
country_code: CountryCode
description: str | None = Undefined()
first_line: str | None = Undefined()
Expand All @@ -14,6 +15,3 @@ class CreateAddress:
postal_code: str | None = Undefined()
region: str | None = Undefined()
custom_data: CustomData | None = Undefined()

def get_parameters(self) -> dict:
return asdict(self)
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Operation import Operation
from paddle_billing.Undefined import Undefined
from paddle_billing.Entities.Shared import CountryCode, CustomData, Status


@dataclass
class UpdateAddress:
class UpdateAddress(Operation):
country_code: CountryCode | Undefined = Undefined()
description: str | None | Undefined = Undefined()
first_line: str | None | Undefined = Undefined()
Expand All @@ -15,6 +16,3 @@ class UpdateAddress:
region: str | None | Undefined = Undefined()
custom_data: CustomData | None | Undefined = Undefined()
status: Status | None | Undefined = Undefined()

def get_parameters(self) -> dict:
return asdict(self)
2 changes: 1 addition & 1 deletion paddle_billing/Resources/Adjustments/AdjustmentsClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def list(self, operation: ListAdjustments = None) -> AdjustmentCollection:
)

def create(self, operation: CreateAdjustment) -> Adjustment:
self.response = self.client.post_raw("/adjustments", operation.get_parameters())
self.response = self.client.post_raw("/adjustments", operation)
parser = ResponseParser(self.response)

return Adjustment.from_dict(parser.get_data())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
from dataclasses import dataclass

from paddle_billing.Operation import Operation

from paddle_billing.Entities.Shared import Action

from paddle_billing.Resources.Adjustments.Operations import CreateAdjustmentItem


@dataclass
class CreateAdjustment:
class CreateAdjustment(Operation):
action: Action
items: list[CreateAdjustmentItem]
reason: str
transaction_id: str

def get_parameters(self) -> dict:
items = [
{
"item_id": item.item_id,
"type": item.type,
"amount": item.amount,
}
for item in self.items
]

return {
"action": self.action.value,
"items": items,
"reason": self.reason,
"transaction_id": self.transaction_id,
}
6 changes: 2 additions & 4 deletions paddle_billing/Resources/Businesses/BusinessesClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ def get(self, customer_id: str, business_id: str) -> Business:
return Business.from_dict(parser.get_data())

def create(self, customer_id: str, operation: CreateBusiness) -> Business:
self.response = self.client.post_raw(f"/customers/{customer_id}/businesses", operation.get_parameters())
self.response = self.client.post_raw(f"/customers/{customer_id}/businesses", operation)
parser = ResponseParser(self.response)

return Business.from_dict(parser.get_data())

def update(self, customer_id: str, business_id: str, operation: UpdateBusiness) -> Business:
self.response = self.client.patch_raw(
f"/customers/{customer_id}/businesses/{business_id}", operation.get_parameters()
)
self.response = self.client.patch_raw(f"/customers/{customer_id}/businesses/{business_id}", operation)
parser = ResponseParser(self.response)

return Business.from_dict(parser.get_data())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Operation import Operation
from paddle_billing.Undefined import Undefined
from paddle_billing.Entities.Shared import Contacts, CustomData


@dataclass
class CreateBusiness:
class CreateBusiness(Operation):
name: str
company_number: str | None | Undefined = Undefined()
tax_identifier: str | None | Undefined = Undefined()
contacts: list[Contacts] | Undefined = Undefined()
custom_data: CustomData | None | Undefined = Undefined()

def get_parameters(self) -> dict:
return asdict(self)
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Operation import Operation
from paddle_billing.Undefined import Undefined
from paddle_billing.Entities.Shared import Contacts, CustomData, Status


@dataclass
class UpdateBusiness:
class UpdateBusiness(Operation):
name: str | Undefined = Undefined()
company_number: str | None | Undefined = Undefined()
tax_identifier: str | None | Undefined = Undefined()
contacts: list[Contacts] | Undefined = Undefined()
custom_data: CustomData | None | Undefined = Undefined()
status: Status | Undefined = Undefined()

def get_parameters(self) -> dict:
return asdict(self)
4 changes: 2 additions & 2 deletions paddle_billing/Resources/Customers/CustomersClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def get(self, customer_id: str) -> Customer:
return Customer.from_dict(parser.get_data())

def create(self, operation: CreateCustomer) -> Customer:
self.response = self.client.post_raw("/customers", operation.get_parameters())
self.response = self.client.post_raw("/customers", operation)
parser = ResponseParser(self.response)

return Customer.from_dict(parser.get_data())

def update(self, customer_id: str, operation: UpdateCustomer) -> Customer:
self.response = self.client.patch_raw(f"/customers/{customer_id}", operation.get_parameters())
self.response = self.client.patch_raw(f"/customers/{customer_id}", operation)
parser = ResponseParser(self.response)

return Customer.from_dict(parser.get_data())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from dataclasses import asdict, dataclass
from dataclasses import dataclass

from paddle_billing.Operation import Operation
from paddle_billing.Undefined import Undefined
from paddle_billing.Entities.Shared import CustomData


@dataclass
class CreateCustomer:
class CreateCustomer(Operation):
email: str
name: str | None | Undefined = Undefined()
custom_data: CustomData | None | Undefined = Undefined()
locale: str | Undefined = Undefined()

def get_parameters(self) -> dict:
return asdict(self)
Loading

0 comments on commit 8671665

Please sign in to comment.