Skip to content

Commit

Permalink
Merge pull request #190 from florczakraf/188-make-upstream-api-endpoi…
Browse files Browse the repository at this point in the history
…nt-configurable

Make upstream API endpoint configurable
  • Loading branch information
florczakraf committed Jul 28, 2024
2 parents d2e6f42 + cf6d3ed commit 16a3c4b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
39 changes: 38 additions & 1 deletion boogiestats/boogie_api/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

import pytest
import requests_mock as requests_mock_lib
from django.conf import settings

from boogiestats import __version__ as boogiestats_version
from boogiestats.boogie_api.models import Song, Player, Score, LeaderboardSource
from boogiestats.boogie_api.views import GROOVESTATS_ENDPOINT, GROOVESTATS_RESPONSES, create_headers, LB_SOURCE_MAPPING
from boogiestats.boogie_api.views import GROOVESTATS_RESPONSES, create_headers, LB_SOURCE_MAPPING

GROOVESTATS_ENDPOINT = settings.BS_UPSTREAM_API_ENDPOINT


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -1394,3 +1397,37 @@ def test_pulling_gs_name_and_tag_with_missing_attributes(
else:
assert player.name == "1234"
assert player.machine_tag == "DUPA"


def test_custom_upstream_api_endpoint_is_taken_from_settings(
client, some_player, some_player_gs_api_key, requests_mock, monkeypatch
):
monkeypatch.setattr(settings, "BS_UPSTREAM_API_ENDPOINT", "https://example.com")
hash = "76957dd1f96f764d"
expected_result = {
"player1": {
"chartHash": hash,
"isRanked": False,
"gsLeaderboard": [],
"scoreDelta": 5809,
"result": "score-added",
}
}
requests_mock.post("https://example.com/score-submit.php", text=json.dumps(expected_result))
kwargs = {
"HTTP_x_api_key_player_1": some_player_gs_api_key,
}
client.post(
f"/score-submit.php?chartHashP1={hash}&maxLeaderboardResults=3",
data={
"player1": {
"score": 5805,
"comment": "50e, 42g, 8d, 11wo, 4m, C300",
"rate": 100,
}
},
content_type="application/json",
**kwargs,
)

assert requests_mock.called_once
6 changes: 3 additions & 3 deletions boogiestats/boogie_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import requests
import sentry_sdk
from django.conf import settings
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from prometheus_client import Counter
Expand All @@ -16,7 +17,6 @@
from boogiestats.boogie_api.utils import set_sentry_user

logger = logging.getLogger("django.server.boogiestats")
GROOVESTATS_ENDPOINT = "https://api.groovestats.com" # TODO take from settings?
GROOVESTATS_RESPONSES = {
"PLAYERS_VALIDATION_ERROR": {
"message": "Something went wrong.",
Expand Down Expand Up @@ -194,7 +194,7 @@ def _try_gs_get(request):
headers = create_headers(request)
try:
raw_response = requests.get(
GROOVESTATS_ENDPOINT + request.path,
settings.BS_UPSTREAM_API_ENDPOINT + request.path,
params=request.GET,
headers=headers,
timeout=GROOVESTATS_TIMEOUT,
Expand Down Expand Up @@ -279,7 +279,7 @@ def score_submit(request):
try:
GS_POST_REQUESTS_TOTAL.inc()
raw_response = requests.post(
GROOVESTATS_ENDPOINT + "/score-submit.php",
settings.BS_UPSTREAM_API_ENDPOINT + "/score-submit.php",
params=request.GET,
headers=headers,
json=body_parsed,
Expand Down
3 changes: 3 additions & 0 deletions boogiestats/boogiestats/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,6 @@
# Use BS_EXTRA_Q_AND_A to set any extra instance-specific questions and answers to appear in the Q&A section.
# It's a dictionary in Question -> Answer format. The question will also appear in the Table of Contents in the User Manual.
BS_EXTRA_Q_AND_A: Dict[str, str] = {}

# Upstream API endpoint, useful for chaining multiple BS instances
BS_UPSTREAM_API_ENDPOINT = "https://api.groovestats.com"

0 comments on commit 16a3c4b

Please sign in to comment.