Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ohsome proxy api for user stats #68

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def add_api_endpoints(app):
UsersStatisticsAPI,
UsersStatisticsInterestsAPI,
UsersStatisticsAllAPI,
OhsomeProxyAPI,
)

# System API endpoint
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions backend/api/users/statistics.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading