Skip to content

Commit

Permalink
allow 120 chars line length
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Aug 30, 2024
1 parent fe443a2 commit 8dbf7e7
Show file tree
Hide file tree
Showing 31 changed files with 70 additions and 224 deletions.
4 changes: 1 addition & 3 deletions lago_python_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def __init__(self, api_key: str = "", api_url: str = "") -> None:

@property
def base_api_url(self) -> str:
return urljoin(
self.api_url if self.api_url else Client.BASE_URL, Client.API_PATH
)
return urljoin(self.api_url if self.api_url else Client.BASE_URL, Client.API_PATH)

@callable_cached_property
def add_ons(self) -> AddOnClient:
Expand Down
4 changes: 1 addition & 3 deletions lago_python_client/coupons/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class AppliedCouponClient(
RESPONSE_MODEL: ClassVar[Type[AppliedCouponResponse]] = AppliedCouponResponse
ROOT_NAME: ClassVar[str] = "applied_coupon"

def destroy(
self, external_customer_id: str, applied_coupon_id: str
) -> AppliedCouponResponse:
def destroy(self, external_customer_id: str, applied_coupon_id: str) -> AppliedCouponResponse:
api_response: Response = send_delete_request(
url=make_url(
origin=self.base_url,
Expand Down
4 changes: 1 addition & 3 deletions lago_python_client/credit_notes/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,5 @@ def estimate(self, input_object: CreditNoteEstimate) -> CreditNoteEstimatedRespo

return prepare_object_response(
response_model=CreditNoteEstimatedResponse,
data=get_response_data(
response=api_response, key=self.ESTIMATE_API_RESOURCE
),
data=get_response_data(response=api_response, key=self.ESTIMATE_API_RESOURCE),
)
16 changes: 3 additions & 13 deletions lago_python_client/customers/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class CustomerClient(
RESPONSE_MODEL: ClassVar[Type[CustomerResponse]] = CustomerResponse
ROOT_NAME: ClassVar[str] = "customer"

def current_usage(
self, resource_id: str, external_subscription_id: str
) -> CustomerUsageResponse:
def current_usage(self, resource_id: str, external_subscription_id: str) -> CustomerUsageResponse:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down Expand Up @@ -93,11 +91,7 @@ def portal_url(self, resource_id: str) -> str:
)

response_data = get_response_data(response=api_response, key=self.ROOT_NAME)
return (
response_data.get("portal_url", "")
if isinstance(response_data, Mapping)
else ""
)
return response_data.get("portal_url", "") if isinstance(response_data, Mapping) else ""

def checkout_url(self, resource_id: str) -> str:
api_response: Response = send_post_request(
Expand All @@ -109,8 +103,4 @@ def checkout_url(self, resource_id: str) -> str:
)

response_data = get_response_data(response=api_response, key=self.ROOT_NAME)
return (
response_data.get("checkout_url", "")
if isinstance(response_data, Mapping)
else ""
)
return response_data.get("checkout_url", "") if isinstance(response_data, Mapping) else ""
4 changes: 1 addition & 3 deletions lago_python_client/events/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
from typing import Mapping


class EventClient(
CreateCommandMixin[EventResponse], FindCommandMixin[EventResponse], BaseClient
):
class EventClient(CreateCommandMixin[EventResponse], FindCommandMixin[EventResponse], BaseClient):
API_RESOURCE: ClassVar[str] = "events"
RESPONSE_MODEL: ClassVar[Type[EventResponse]] = EventResponse
ROOT_NAME: ClassVar[str] = "event"
Expand Down
4 changes: 1 addition & 3 deletions lago_python_client/gross_revenues/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class GrossRevenueClient(
RESPONSE_MODEL: ClassVar[Type[GrossRevenueResponse]] = GrossRevenueResponse
ROOT_NAME: ClassVar[str] = "gross_revenue"

def find_all(
self, options: Mapping[str, Union[int, str]] = {}
) -> Mapping[str, Any]:
def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down
8 changes: 2 additions & 6 deletions lago_python_client/invoice_collections/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ class InvoiceCollectionClient(
BaseClient,
):
API_RESOURCE: ClassVar[str] = "invoice_collections"
RESPONSE_MODEL: ClassVar[Type[InvoiceCollectionResponse]] = (
InvoiceCollectionResponse
)
RESPONSE_MODEL: ClassVar[Type[InvoiceCollectionResponse]] = InvoiceCollectionResponse
ROOT_NAME: ClassVar[str] = "invoice_collection"

def find_all(
self, options: Mapping[str, Union[int, str]] = {}
) -> Mapping[str, Any]:
def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down
4 changes: 1 addition & 3 deletions lago_python_client/invoiced_usages/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class InvoicedUsageClient(
RESPONSE_MODEL: ClassVar[Type[InvoicedUsageResponse]] = InvoicedUsageResponse
ROOT_NAME: ClassVar[str] = "invoiced_usage"

def find_all(
self, options: Mapping[str, Union[int, str]] = {}
) -> Mapping[str, Any]:
def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down
10 changes: 2 additions & 8 deletions lago_python_client/invoices/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,5 @@ def payment_url(self, resource_id: str) -> str:
headers=make_headers(api_key=self.api_key),
)

response_data = get_response_data(
response=api_response, key="invoice_payment_details"
)
return (
response_data.get("payment_url", "")
if isinstance(response_data, Mapping)
else ""
)
response_data = get_response_data(response=api_response, key="invoice_payment_details")
return response_data.get("payment_url", "") if isinstance(response_data, Mapping) else ""
4 changes: 1 addition & 3 deletions lago_python_client/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ def update(
api_response: Response = send_put_request(
url=make_url(
origin=self.base_url,
path_parts=(self.API_RESOURCE, identifier)
if identifier
else (self.API_RESOURCE,),
path_parts=(self.API_RESOURCE, identifier) if identifier else (self.API_RESOURCE,),
),
content=to_json(
{
Expand Down
4 changes: 1 addition & 3 deletions lago_python_client/mrrs/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class MrrClient(
RESPONSE_MODEL: ClassVar[Type[MrrResponse]] = MrrResponse
ROOT_NAME: ClassVar[str] = "mrr"

def find_all(
self, options: Mapping[str, Union[int, str]] = {}
) -> Mapping[str, Any]:
def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down
36 changes: 9 additions & 27 deletions lago_python_client/services/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def verify_response(response: Response) -> Optional[Response]:
return response


def get_response_data(
*, response: Response, key: str = ""
) -> Optional[_MappingOrSequence]:
def get_response_data(*, response: Response, key: str = "") -> Optional[_MappingOrSequence]:
"""Return verified and unpacked response data."""
response_or_None: Optional[Response] = verify_response(response)
if not response_or_None:
Expand All @@ -76,9 +74,7 @@ def get_response_data(

# Ensure deserialized_data has correct type: sequence or mapping or raise LagoApiError
try:
mapping_or_sequence_data = typeguard.check_type(
deserialized_data, _MappingOrSequence
)
mapping_or_sequence_data = typeguard.check_type(deserialized_data, _MappingOrSequence)
except typeguard.TypeCheckError as exc:
raise LagoApiError(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500
Expand All @@ -89,27 +85,21 @@ def get_response_data(
)

if isinstance(mapping_or_sequence_data, Mapping):
return (
mapping_or_sequence_data.get(key, {}) if key else mapping_or_sequence_data
)
return mapping_or_sequence_data.get(key, {}) if key else mapping_or_sequence_data
elif isinstance(mapping_or_sequence_data, Sequence):
return mapping_or_sequence_data
else:
assert_never(mapping_or_sequence_data)


def prepare_object_response(
response_model: Type[_M], data: Optional[_MappingOrSequence]
) -> _M:
def prepare_object_response(response_model: Type[_M], data: Optional[_MappingOrSequence]) -> _M:
"""Return single object response - Pydantic model instance with provided data."""
if not data:
raise LagoApiError(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500
url=None,
response=None,
detail="Data is required to create instance of {model}".format(
model=response_model
),
detail="Data is required to create instance of {model}".format(model=response_model),
headers=None,
)

Expand All @@ -123,9 +113,7 @@ def prepare_index_response(
# Ensure deserialized_data has correct type: mapping with mapping or sequence inside or raise LagoApiError

try:
response_data: Mapping[str, _MappingOrSequence] = typeguard.check_type(
data, Mapping[str, _MappingOrSequence]
)
response_data: Mapping[str, _MappingOrSequence] = typeguard.check_type(data, Mapping[str, _MappingOrSequence])
except typeguard.TypeCheckError as exc:
raise LagoApiError(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500
Expand All @@ -137,8 +125,7 @@ def prepare_index_response(

return {
api_resource: [
prepare_object_response(response_model=response_model, data=el)
for el in response_data[api_resource]
prepare_object_response(response_model=response_model, data=el) for el in response_data[api_resource]
],
"meta": response_data["meta"],
}
Expand All @@ -153,9 +140,7 @@ def prepare_object_list_response(
# The only usage - ``WalletTransactionClient.create``
# Ensure deserialized_data has correct type: sequence with mapping or sequence inside or raise LagoApiError
try:
response_data: Sequence[_MappingOrSequence] = typeguard.check_type(
data, Sequence[_MappingOrSequence]
)
response_data: Sequence[_MappingOrSequence] = typeguard.check_type(data, Sequence[_MappingOrSequence])
except typeguard.TypeCheckError as exc:
raise LagoApiError(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, # 500
Expand All @@ -166,8 +151,5 @@ def prepare_object_list_response(
)

return {
api_resource: [
prepare_object_response(response_model=response_model, data=el)
for el in response_data
],
api_resource: [prepare_object_response(response_model=response_model, data=el) for el in response_data],
}
8 changes: 2 additions & 6 deletions lago_python_client/wallets/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class WalletClient(

class WalletTransactionClient(BaseClient):
API_RESOURCE: ClassVar[str] = "wallet_transactions"
RESPONSE_MODEL: ClassVar[Type[WalletTransactionResponse]] = (
WalletTransactionResponse
)
RESPONSE_MODEL: ClassVar[Type[WalletTransactionResponse]] = WalletTransactionResponse
ROOT_NAME: ClassVar[str] = "wallet_transactions"

def create(self, input_object: BaseModel) -> Mapping[str, Any]:
Expand All @@ -69,9 +67,7 @@ def create(self, input_object: BaseModel) -> Mapping[str, Any]:
data=get_response_data(response=api_response, key=self.ROOT_NAME),
)

def find_all(
self, wallet_id: str, options: Mapping[str, Union[int, str]] = {}
) -> Mapping[str, Any]:
def find_all(self, wallet_id: str, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
Expand Down
4 changes: 2 additions & 2 deletions lago_python_client/webhooks/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def public_key(self) -> bytes:
),
headers=make_headers(api_key=self.api_key),
)
response_data: Optional[Union[Mapping[str, Any], Sequence[Any]]] = (
get_response_data(response=api_response, key=self.ROOT_NAME)
response_data: Optional[Union[Mapping[str, Any], Sequence[Any]]] = get_response_data(
response=api_response, key=self.ROOT_NAME
)

try:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
requires = ["setuptools>=54", "wheel"]
build-backend = "setuptools.build_meta"

[tool.ruff]
line-length = 120

[tool.ruff.lint]
extend-select = ['E305']
preview = true
8 changes: 2 additions & 6 deletions tests/test_applied_coupon_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ def test_valid_find_all_applied_coupon_request(httpx_mock: HTTPXMock):
)
response = client.applied_coupons.find_all()

assert (
response["applied_coupons"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b"
)
assert response["applied_coupons"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b"
assert response["meta"]["current_page"] == 1


Expand All @@ -84,9 +82,7 @@ def test_valid_find_all_applied_coupon_request_with_options(httpx_mock: HTTPXMoc
)
response = client.applied_coupons.find_all({"per_page": 2, "page": 1})

assert (
response["applied_coupons"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac2222"
)
assert response["applied_coupons"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac2222"
assert response["meta"]["current_page"] == 1


Expand Down
16 changes: 3 additions & 13 deletions tests/test_billable_metric_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def billable_metric_object():


def filters():
return BillableMetricFilters(
__root__=[
BillableMetricFilter(key="country", values=["france", "italy", "spain"])
]
)
return BillableMetricFilters(__root__=[BillableMetricFilter(key="country", values=["france", "italy", "spain"])])


def mock_response():
Expand Down Expand Up @@ -178,10 +174,7 @@ def test_valid_find_all_billable_metric_request(httpx_mock: HTTPXMock):
)
response = client.billable_metrics.find_all()

assert (
response["billable_metrics"][0].lago_id
== "b7ab2926-1de8-4428-9bcd-779314ac1000"
)
assert response["billable_metrics"][0].lago_id == "b7ab2926-1de8-4428-9bcd-779314ac1000"
assert response["meta"]["current_page"] == 1


Expand All @@ -195,10 +188,7 @@ def test_valid_find_all_billable_metric_request_with_options(httpx_mock: HTTPXMo
)
response = client.billable_metrics.find_all({"per_page": 2, "page": 1})

assert (
response["billable_metrics"][1].lago_id
== "b7ab2926-1de8-4428-9bcd-779314a11111"
)
assert response["billable_metrics"][1].lago_id == "b7ab2926-1de8-4428-9bcd-779314a11111"
assert response["meta"]["current_page"] == 1


Expand Down
4 changes: 1 addition & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@


def test_base_url_when_api_url_is_given():
client = Client(
api_key="886fe239-927d-4072-ab72-6dd345e8dd0d", api_url="https://example.com/"
)
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d", api_url="https://example.com/")

assert client.base_api_url == "https://example.com/api/v1/"

Expand Down
4 changes: 1 addition & 3 deletions tests/test_credit_note_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def credit_note_object():

item2 = Item(fee="fee_id_2", amount_cents=5)

return CreditNote(
lago_id="credit_note_id", reason="other", items=Items(__root__=[item1, item2])
)
return CreditNote(lago_id="credit_note_id", reason="other", items=Items(__root__=[item1, item2]))


def credit_note_update_object():
Expand Down
8 changes: 2 additions & 6 deletions tests/test_customer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def create_customer():
subsidiary_id="2",
sync_with_provider=True,
)
integration_customers_list = IntegrationCustomersList(
__root__=[integration_customer]
)
integration_customers_list = IntegrationCustomersList(__root__=[integration_customer])

return Customer(
external_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba",
Expand Down Expand Up @@ -93,9 +91,7 @@ def test_valid_create_customers_request(httpx_mock: HTTPXMock):
assert response.shipping_address.address_line1 == "Test Ave"
assert response.shipping_address.address_line2 is None
assert response.shipping_address.state == "XZ"
assert (
response.integration_customers.__root__[0].external_customer_id == "test-12345"
)
assert response.integration_customers.__root__[0].external_customer_id == "test-12345"
assert response.integration_customers.__root__[0].type == "netsuite"
assert response.metadata.__root__[0].lago_id == "12345"
assert response.metadata.__root__[0].key == "key"
Expand Down
Loading

0 comments on commit 8dbf7e7

Please sign in to comment.