|
| 1 | +# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This software is licensed under the Apache License, Version 2.0 (the |
| 4 | +# "License") as published by the Apache Software Foundation. |
| 5 | +# |
| 6 | +# You may not use this file except in compliance with the License. You may |
| 7 | +# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import TYPE_CHECKING |
| 18 | + |
| 19 | +from httpx import AsyncClient |
| 20 | + |
| 21 | +from supertokens_python import Supertokens |
| 22 | +from supertokens_python.constants import ( |
| 23 | + TELEMETRY_SUPERTOKENS_API_URL, |
| 24 | + TELEMETRY_SUPERTOKENS_API_VERSION, |
| 25 | +) |
| 26 | +from supertokens_python.constants import VERSION as SDKVersion |
| 27 | +from supertokens_python.exceptions import raise_bad_input_exception |
| 28 | +from supertokens_python.normalised_url_path import NormalisedURLPath |
| 29 | +from supertokens_python.querier import Querier |
| 30 | + |
| 31 | +from ..interfaces import AnalyticsResponse |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions |
| 35 | + |
| 36 | + |
| 37 | +async def handle_analytics_post( |
| 38 | + _: APIInterface, api_options: APIOptions |
| 39 | +) -> AnalyticsResponse: |
| 40 | + if not Supertokens.get_instance().telemetry: |
| 41 | + return AnalyticsResponse() |
| 42 | + body = await api_options.request.json() |
| 43 | + if body is None: |
| 44 | + raise_bad_input_exception("Please send body") |
| 45 | + email = body.get("email") |
| 46 | + dashboard_version = body.get("dashboardVersion") |
| 47 | + |
| 48 | + if email is None: |
| 49 | + raise_bad_input_exception("Missing required property 'email'") |
| 50 | + if dashboard_version is None: |
| 51 | + raise_bad_input_exception("Missing required property 'dashboardVersion'") |
| 52 | + |
| 53 | + telemetry_id = None |
| 54 | + |
| 55 | + try: |
| 56 | + response = await Querier.get_instance().send_get_request( |
| 57 | + NormalisedURLPath("/telemetry") |
| 58 | + ) |
| 59 | + if response is not None: |
| 60 | + if ( |
| 61 | + "exists" in response |
| 62 | + and response["exists"] |
| 63 | + and "telemetryId" in response |
| 64 | + ): |
| 65 | + telemetry_id = response["telemetryId"] |
| 66 | + |
| 67 | + number_of_users = await Supertokens.get_instance().get_user_count( |
| 68 | + include_recipe_ids=None |
| 69 | + ) |
| 70 | + |
| 71 | + except Exception as __: |
| 72 | + # If either telemetry id API or user count fetch fails, no event should be sent |
| 73 | + return AnalyticsResponse() |
| 74 | + |
| 75 | + apiDomain, websiteDomain, appName = ( |
| 76 | + api_options.app_info.api_domain, |
| 77 | + api_options.app_info.website_domain, |
| 78 | + api_options.app_info.app_name, |
| 79 | + ) |
| 80 | + |
| 81 | + data = { |
| 82 | + "websiteDomain": websiteDomain.get_as_string_dangerous(), |
| 83 | + "apiDomain": apiDomain.get_as_string_dangerous(), |
| 84 | + "appName": appName, |
| 85 | + "sdk": "python", |
| 86 | + "sdkVersion": SDKVersion, |
| 87 | + "numberOfUsers": number_of_users, |
| 88 | + "email": email, |
| 89 | + "dashboardVersion": dashboard_version, |
| 90 | + } |
| 91 | + |
| 92 | + if telemetry_id is not None: |
| 93 | + data["telemetryId"] = telemetry_id |
| 94 | + |
| 95 | + try: |
| 96 | + async with AsyncClient() as client: |
| 97 | + await client.post( # type: ignore |
| 98 | + url=TELEMETRY_SUPERTOKENS_API_URL, |
| 99 | + json=data, |
| 100 | + headers={"api-version": TELEMETRY_SUPERTOKENS_API_VERSION}, |
| 101 | + ) |
| 102 | + except Exception as __: |
| 103 | + # If telemetry event fails, no error should be thrown |
| 104 | + pass |
| 105 | + |
| 106 | + return AnalyticsResponse() |
0 commit comments