Skip to content

Commit

Permalink
[Community] use supabase func to fetch extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Oct 1, 2024
1 parent 467bb96 commit 6529dbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
48 changes: 14 additions & 34 deletions octobot/community/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,55 +631,35 @@ async def fetch_private_data(self, reset=False):
await self._refresh_products()

async def _fetch_package_urls(self, mqtt_uuid: typing.Optional[str]) -> (list[str], str):
self.logger.debug(f"Fetching package")
resp = await self.supabase_client.http_get(
constants.COMMUNITY_EXTENSIONS_CHECK_ENDPOINT,
headers={
"Content-Type": "application/json",
"X-Auth-Token": constants.COMMUNITY_EXTENSIONS_CHECK_ENDPOINT_KEY
},
params={"mqtt_id": mqtt_uuid} if mqtt_uuid else {},
timeout=constants.COMMUNITY_FETCH_TIMEOUT
)
self.logger.debug("Fetched package")
resp.raise_for_status()
json_resp = json.loads(resp.json().get("message", {}))
if not json_resp:
self.logger.debug(f"Fetching extension package details")
extensions_details = await self.supabase_client.fetch_extensions(mqtt_uuid)
self.logger.debug("Fetched extension package details")
if not extensions_details:
return None, None, None
packages = [
package
for package in json_resp["paid_package_slugs"]
for package in extensions_details["paid_package_slugs"]
if package
]
urls = [
url
for url in json_resp["package_urls"]
for url in extensions_details["package_urls"]
if url
]
mqtt_id = json_resp["mqtt_id"]
mqtt_id = extensions_details["mqtt_id"]
return packages, urls, mqtt_id

async def fetch_checkout_url(self, payment_method, redirect_url):
async def fetch_checkout_url(self, payment_method: str, redirect_url: str):
try:
if not self.is_logged_in():
self.logger.info(f"Can't fetch checkout url: no authenticated user")
return None
self.logger.debug(f"Fetching {payment_method} checkout url")
resp = await self.supabase_client.http_post(
constants.COMMUNITY_EXTENSIONS_CHECK_ENDPOINT,
json={
"payment_method": payment_method,
"success_url": redirect_url,
},
headers={
"Content-Type": "application/json",
"X-Auth-Token": constants.COMMUNITY_EXTENSIONS_CHECK_ENDPOINT_KEY
},
timeout=constants.COMMUNITY_FETCH_TIMEOUT
)
resp.raise_for_status()
json_resp = json.loads(resp.json().get("message", {}))
if not json_resp:
url_details = await self.supabase_client.fetch_checkout_url(payment_method, redirect_url)
if not url_details:
# valid error code but no content: user already has this product
return None
url = json_resp["checkout_url"]
url = url_details["checkout_url"]
self.logger.info(
f"Here is your {constants.OCTOBOT_EXTENSION_PACKAGE_1_NAME} checkout url {url} "
f"paste it into a web browser to proceed to payment if your browser did to automatically "
Expand Down
25 changes: 25 additions & 0 deletions octobot/community/supabase_backend/community_supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,31 @@ async def get_otp_with_auth_key(self, user_email: str, auth_key: str) -> str:
except Exception:
raise authentication.AuthenticationError(f"Invalid auth key authentication details")

async def fetch_extensions(self, mqtt_uuid: typing.Optional[str]) -> dict:
resp = await self.functions.invoke(
"os-paid-package-api",
{
"body": {
"action": "get_extension_details",
"mqtt_id": mqtt_uuid
},
}
)
return json.loads(json.loads(resp)["message"])

async def fetch_checkout_url(self, payment_method: str, redirect_url: str) -> dict:
resp = await self.functions.invoke(
"os-paid-package-api",
{
"body": {
"action": "get_checkout_url",
"payment_method": payment_method,
"success_url": redirect_url,
},
}
)
return json.loads(json.loads(resp)["message"])

async def fetch_bot(self, bot_id) -> dict:
try:
# https://postgrest.org/en/stable/references/api/resource_embedding.html#hint-disambiguation
Expand Down

0 comments on commit 6529dbd

Please sign in to comment.