Skip to content

Commit 6db06b6

Browse files
committed
fix various bugs and issues with cashew <-> btrix api calls
convert to json before converting to output type more debugging fix model type pass return url to checkout session request fix fn ordering correctly pass in headers include content-type header fix double encoding as json properly dump model to dict in requests
1 parent 1a8775a commit 6db06b6

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

backend/btrixcloud/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ class SubscriptionPortalUrlResponse(BaseModel):
19821982
class AddonMinutesPricing(BaseModel):
19831983
"""Addon minutes pricing"""
19841984

1985-
price: float
1985+
value: float
19861986
currency: str
19871987

19881988

@@ -1993,6 +1993,7 @@ class CheckoutAddonMinutesRequest(BaseModel):
19931993
orgId: str
19941994
subId: str
19951995
minutes: int | None = None
1996+
return_url: str
19961997

19971998

19981999
class CheckoutAddonMinutesResponse(BaseModel):

backend/btrixcloud/subs.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ async def add_sub_event(
245245
data["oid"] = oid
246246
await self.subs.insert_one(data)
247247

248-
def _get_sub_by_type_from_data(self, data: dict[str, object]) -> Union[
248+
def _get_sub_by_type_from_data(
249+
self, data: dict[str, object]
250+
) -> Union[
249251
SubscriptionCreateOut,
250252
SubscriptionImportOut,
251253
SubscriptionUpdateOut,
@@ -370,7 +372,7 @@ async def get_billing_portal_url(
370372
headers={
371373
"Authorization": "bearer " + external_subs_app_api_key
372374
},
373-
json=req.dict(),
375+
json=req.model_dump(),
374376
raise_for_status=True,
375377
) as resp:
376378
json = await resp.json()
@@ -394,41 +396,52 @@ async def get_execution_minutes_price(self, org: Organization):
394396
"GET",
395397
f"{external_subs_app_api_url}/prices/additionalMinutes",
396398
headers={
397-
"Authorization": "bearer " + external_subs_app_api_key
399+
"Authorization": "bearer " + external_subs_app_api_key,
398400
},
399401
raise_for_status=True,
400402
) as resp:
401-
text = await resp.text()
402-
return AddonMinutesPricing.model_validate_json(text)
403+
json = await resp.json()
404+
return AddonMinutesPricing(**json)
403405
# pylint: disable=broad-exception-caught
404406
except Exception as exc:
405407
print("Error fetching checkout url", exc)
406408

407-
async def get_checkout_url(self, org: Organization, minutes: int | None):
409+
async def get_checkout_url(
410+
self,
411+
org: Organization,
412+
headers: dict[str, str],
413+
minutes: int | None,
414+
):
408415
"""Create checkout url for additional minutes"""
409416
if not org.subscription:
410417
raise HTTPException(
411418
status_code=404, detail="Organization has no subscription"
412419
)
413420
subscription_id = org.subscription.subId
421+
return_url = f"{get_origin(headers)}/orgs/{org.slug}/settings/billing"
414422

415423
if external_subs_app_api_url:
416424
try:
417425
req = CheckoutAddonMinutesRequest(
418-
orgId=str(org.id), subId=subscription_id, minutes=minutes
426+
orgId=str(org.id),
427+
subId=subscription_id,
428+
minutes=minutes,
429+
return_url=return_url,
419430
)
420431
async with aiohttp.ClientSession() as session:
421432
async with session.request(
422433
"POST",
423434
f"{external_subs_app_api_url}/checkout/additionalMinutes",
424435
headers={
425-
"Authorization": "bearer " + external_subs_app_api_key
436+
"Authorization": "bearer " + external_subs_app_api_key,
437+
"Content-Type": "application/json",
426438
},
427-
json=req.model_dump_json(),
439+
json=req.model_dump(),
428440
raise_for_status=True,
429441
) as resp:
430-
text = await resp.text()
431-
return CheckoutAddonMinutesResponse.model_validate_json(text)
442+
json = await resp.json()
443+
print(f"get_checkout_url got response: {json}")
444+
return CheckoutAddonMinutesResponse(**json)
432445
# pylint: disable=broad-exception-caught
433446
except Exception as exc:
434447
print("Error fetching checkout url", exc)
@@ -572,9 +585,10 @@ async def get_execution_minutes_price(
572585
response_model=CheckoutAddonMinutesResponse,
573586
)
574587
async def get_execution_minutes_checkout_url(
588+
request: Request,
575589
minutes: int | None = None,
576590
org: Organization = Depends(org_ops.org_owner_dep),
577591
):
578-
return await ops.get_checkout_url(org, minutes)
592+
return await ops.get_checkout_url(org, dict(request.headers), minutes)
579593

580594
return ops

0 commit comments

Comments
 (0)