From 6bf38a29c805b80184895f1341b7b40606dcef6f Mon Sep 17 00:00:00 2001 From: kaditya97 Date: Fri, 22 Mar 2024 06:11:59 +0545 Subject: [PATCH] feat: ohsome proxy api for user stats --- backend/__init__.py | 4 ++++ backend/api/users/statistics.py | 42 +++++++++++++++++++++++++++++++++ backend/config.py | 3 +++ 3 files changed, 49 insertions(+) diff --git a/backend/__init__.py b/backend/__init__.py index 4e19622fde..213be4e0e8 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -360,6 +360,7 @@ def add_api_endpoints(app): UsersStatisticsAPI, UsersStatisticsInterestsAPI, UsersStatisticsAllAPI, + OhsomeProxyAPI, ) # System API endpoint @@ -900,6 +901,9 @@ def add_api_endpoints(app): UsersStatisticsAllAPI, format_url("users/statistics/"), ) + api.add_resource( + OhsomeProxyAPI, format_url("users/statistics/ohsome/"), methods=["GET"] + ) # User RecommendedProjects endpoint api.add_resource( UsersRecommendedProjectsAPI, diff --git a/backend/api/users/statistics.py b/backend/api/users/statistics.py index 2838cd4831..e3e6f9dd8c 100644 --- a/backend/api/users/statistics.py +++ b/backend/api/users/statistics.py @@ -1,12 +1,14 @@ from json import JSONEncoder from datetime import date, timedelta from flask_restful import Resource, request +import requests from backend.services.users.user_service import UserService from backend.services.stats_service import StatsService from backend.services.interests_service import InterestService from backend.services.users.authentication_service import token_auth from backend.api.utils import validate_date_input +from backend.config import EnvironmentConfig class UsersStatisticsAPI(Resource, JSONEncoder): @@ -138,3 +140,43 @@ def get(self): return stats.to_primitive(), 200 except (KeyError, ValueError) as e: return {"Error": str(e).split("-")[1], "SubCode": str(e).split("-")[0]}, 400 + + +class OhsomeProxyAPI(Resource): + @token_auth.login_required + def get(self): + """ + Get HomePage Stats + --- + tags: + - system + produces: + - application/json + parameters: + - in: header + name: Authorization + description: Base64 encoded session token + required: true + type: string + default: Token sessionTokenHere== + - in: query + name: url + type: string + description: get user stats for osm contributions + responses: + 200: + description: User stats + 500: + description: Internal Server Error + """ + url = request.args.get("url") + if not url: + return {"Error": "URL is None", "SubCode": "URL not provided"}, 400 + try: + headers = {"Authorization": f"Basic {EnvironmentConfig.OHSOME_STATS_TOKEN}"} + + # Make the GET request with headers + response = requests.get(url, headers=headers) + return response.json(), 200 + except Exception as e: + return {"Error": str(e), "SubCode": "Error fetching data"}, 400 diff --git a/backend/config.py b/backend/config.py index 555205014f..fe3b7ae40a 100644 --- a/backend/config.py +++ b/backend/config.py @@ -237,6 +237,9 @@ class EnvironmentConfig: # Sentry backend DSN SENTRY_BACKEND_DSN = os.getenv("TM_SENTRY_BACKEND_DSN", None) + # Ohsome Stats Token + OHSOME_STATS_TOKEN = os.getenv("OHSOME_STATS_TOKEN", None) + class TestEnvironmentConfig(EnvironmentConfig): POSTGRES_TEST_DB = os.getenv("POSTGRES_TEST_DB", None)