Skip to content

Commit

Permalink
Merge pull request #49 from planetarium/improve/headless-url-config
Browse files Browse the repository at this point in the history
grapqhl url configurable
  • Loading branch information
ipdae authored Feb 15, 2024
2 parents 9e59091 + 2974267 commit 08284e4
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 81 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/unittest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ jobs:
KMS_KEY_ID: ${{ secrets.KMS_KEY_ID }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
GRAPHQL_PASSWORD: ${{ secrets.GRAPHQL_PASSWORD }}
HEADLESS_URL: ${{ secrets.HEADLESS_URL }}
DATA_PROVIDER_URL: ${{ secrets.DATA_PROVIDER_URL }}
run: |
poetry run pytest --redis-exec=$(which redis-server) --cov world_boss --cov-report=xml
- name: Upload coverage to Codecov
Expand Down
12 changes: 6 additions & 6 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from starlette.testclient import TestClient

from world_boss.app.cache import cache_exists, set_to_cache
from world_boss.app.data_provider import DATA_PROVIDER_URLS
from world_boss.app.config import config
from world_boss.app.enums import NetworkType
from world_boss.app.kms import HEADLESS_URLS, MINER_URLS, signer
from world_boss.app.kms import signer
from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount


Expand Down Expand Up @@ -79,7 +79,7 @@ def test_count_total_users(
):
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossTotalUsers": 100}},
)
with unittest.mock.patch(
Expand Down Expand Up @@ -115,13 +115,13 @@ def test_generate_ranking_rewards_csv(
]
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossRankingRewards": requested_rewards}},
)

httpx_mock.add_response(
method="POST",
url=MINER_URLS[NetworkType.MAIN],
url=config.headless_url,
json={
"data": {
"stateQuery": {
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_stage_transactions(
task_id = req.json()
task: AsyncResult = AsyncResult(task_id)
task.get(timeout=30)
assert m.call_count == len(HEADLESS_URLS[network_type]) * len(fx_transactions)
assert m.call_count == len(fx_transactions)
m2.assert_called_once_with(
channel="channel_id", text=f"stage {len(fx_transactions)} transactions"
)
Expand Down
7 changes: 4 additions & 3 deletions tests/data_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from pytest_httpx import HTTPXMock

from world_boss.app.cache import cache_exists, set_to_cache
from world_boss.app.data_provider import DATA_PROVIDER_URLS, data_provider_client
from world_boss.app.config import config
from world_boss.app.data_provider import data_provider_client
from world_boss.app.enums import NetworkType
from world_boss.app.stubs import RankingRewardDictionary

Expand All @@ -15,7 +16,7 @@
def test_get_total_users_count(
network_type: NetworkType, raid_id: int, expected_count: int
):
result = data_provider_client.get_total_users_count(raid_id, network_type)
result = data_provider_client.get_total_users_count(raid_id)
assert result == expected_count


Expand Down Expand Up @@ -60,7 +61,7 @@ def test_get_ranking_rewards_error(
cache_key = f"world_boss_{raid_id}_{network_type}_{offset}_{limit}"
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[network_type],
url=config.data_provider_url,
json={
"errors": [{"message": "can't receive"}],
"data": {"worldBossRankingRewards": None},
Expand Down
10 changes: 4 additions & 6 deletions tests/graphql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from pytest_httpx import HTTPXMock

from world_boss.app.config import config
from world_boss.app.data_provider import DATA_PROVIDER_URLS
from world_boss.app.enums import NetworkType
from world_boss.app.kms import HEADLESS_URLS, MINER_URLS
from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount


Expand All @@ -27,7 +25,7 @@ def test_next_tx_nonce(fx_session, fx_test_client):
def test_count_total_users(fx_test_client, httpx_mock: HTTPXMock):
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossTotalUsers": 100}},
)
query = "query { countTotalUsers(seasonId: 1) }"
Expand Down Expand Up @@ -86,13 +84,13 @@ def test_generate_ranking_rewards_csv(
]
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossRankingRewards": requested_rewards}},
)

httpx_mock.add_response(
method="POST",
url=MINER_URLS[NetworkType.MAIN],
url=config.headless_url,
json={
"data": {
"stateQuery": {
Expand Down Expand Up @@ -295,7 +293,7 @@ def test_stage_transactions(
task_id = req.json()["data"]["stageTransactions"]
task: AsyncResult = AsyncResult(task_id)
task.get(timeout=30)
assert m.call_count == len(HEADLESS_URLS[network_type]) * len(fx_transactions)
assert m.call_count == len(fx_transactions)
m2.assert_called_once_with(
channel=config.slack_channel_id,
text=f"stage {len(fx_transactions)} transactions",
Expand Down
11 changes: 6 additions & 5 deletions tests/kms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import pytest
from gql.transport.exceptions import TransportQueryError

from world_boss.app.config import config
from world_boss.app.enums import NetworkType
from world_boss.app.kms import HEADLESS_URLS, MINER_URLS, signer
from world_boss.app.kms import signer
from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount
from world_boss.app.raid import get_currencies
from world_boss.app.stubs import AmountDictionary, Recipient
Expand Down Expand Up @@ -76,7 +77,7 @@ async def test_check_transaction_status_async(fx_session, fx_mainnet_transaction
],
)
def test_prepare_reward_assets(fx_app, network_type: NetworkType):
headless_url = MINER_URLS[network_type]
headless_url = config.headless_url
assets: List[AmountDictionary] = [
{"decimalPlaces": 18, "ticker": "CRYSTAL", "quantity": 109380000},
{"decimalPlaces": 0, "ticker": "RUNESTONE_FENRIR1", "quantity": 406545},
Expand All @@ -94,7 +95,7 @@ def test_stage_transaction(fx_session, fx_mainnet_transactions):
tx = fx_mainnet_transactions[0]
fx_session.add(tx)
fx_session.flush()
urls = HEADLESS_URLS[NetworkType.INTERNAL]
urls = [config.headless_url]
for url in urls:
with pytest.raises(TransportQueryError) as e:
signer.stage_transaction(url, tx)
Expand All @@ -105,14 +106,14 @@ def test_query_transaction_result(fx_session, fx_mainnet_transactions):
tx = fx_mainnet_transactions[0]
fx_session.add(tx)
fx_session.flush()
url = MINER_URLS[NetworkType.MAIN]
url = config.headless_url
signer.query_transaction_result(url, tx.tx_id, fx_session)
transaction = fx_session.query(Transaction).one()
assert transaction.tx_result == "INCLUDED"


def test_query_balance(fx_session):
url = MINER_URLS[NetworkType.MAIN]
url = config.headless_url
reward = WorldBossReward()
reward.avatar_address = "avatar_address"
reward.agent_address = "agent_address"
Expand Down
24 changes: 10 additions & 14 deletions tests/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import pytest
from pytest_httpx import HTTPXMock

from world_boss.app.data_provider import DATA_PROVIDER_URLS
from world_boss.app.config import config
from world_boss.app.enums import NetworkType
from world_boss.app.kms import MINER_URLS, signer
from world_boss.app.kms import signer
from world_boss.app.models import Transaction, WorldBossReward, WorldBossRewardAmount
from world_boss.app.stubs import (
RankingRewardDictionary,
Expand All @@ -30,7 +30,7 @@
def test_count_users(celery_session_worker, httpx_mock: HTTPXMock):
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossTotalUsers": 100}},
)
with unittest.mock.patch("world_boss.app.tasks.client.chat_postMessage") as m:
Expand Down Expand Up @@ -91,13 +91,13 @@ def test_get_ranking_rewards(
redisdb.set(rewards_cache_key, json.dumps(cached_rewards))
httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={"data": {"worldBossRankingRewards": requested_rewards}},
)
redisdb.set(addresses_cache_key, json.dumps(cached_addresses))
httpx_mock.add_response(
method="POST",
url=MINER_URLS[NetworkType.MAIN],
url=config.headless_url,
json={
"data": {
"stateQuery": {
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_get_ranking_rewards_error(

httpx_mock.add_response(
method="POST",
url=DATA_PROVIDER_URLS[NetworkType.MAIN],
url=config.data_provider_url,
json={
"errors": [{"message": "can't receive"}],
"data": {"worldBossRankingRewards": None},
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_sign_transfer_assets(
nonce,
[],
"memo",
MINER_URLS[NetworkType.MAIN],
config.headless_url,
max_nonce,
nonce_list,
).get(timeout=10)
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_stage_transaction(
transaction.nonce = 1
fx_session.add(transaction)
fx_session.commit()
url = MINER_URLS[network_type]
url = config.headless_url
with unittest.mock.patch(
"world_boss.app.tasks.signer.stage_transaction", return_value="tx_id"
) as m:
Expand Down Expand Up @@ -279,9 +279,7 @@ def test_query_tx_result(celery_session_worker, fx_session, fx_transactions):
fx_session.commit()

for tx_id in tx_ids:
_, result = query_tx_result.delay(MINER_URLS[NetworkType.MAIN], tx_id).get(
timeout=10
)
_, result = query_tx_result.delay(config.headless_url, tx_id).get(timeout=10)
tx = fx_session.query(Transaction).filter_by(tx_id=tx_id).one()
assert result == "INCLUDED"
assert tx.tx_result == "INCLUDED"
Expand All @@ -306,9 +304,7 @@ def test_upload_result(
)
def test_check_signer_balance(celery_session_worker, ticker: str, decimal_places: int):
currency = {"ticker": ticker, "decimalPlaces": decimal_places, "minters": []}
result = check_signer_balance.delay(MINER_URLS[NetworkType.MAIN], currency).get(
timeout=10
)
result = check_signer_balance.delay(config.headless_url, currency).get(timeout=10)
assert result == f"0 {ticker}"


Expand Down
11 changes: 6 additions & 5 deletions world_boss/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from sqlalchemy.orm import Session
from starlette.responses import JSONResponse, Response

from world_boss.app.config import config
from world_boss.app.enums import NetworkType
from world_boss.app.kms import HEADLESS_URLS, MINER_URLS, signer
from world_boss.app.kms import signer
from world_boss.app.models import Transaction
from world_boss.app.orm import SessionLocal
from world_boss.app.raid import (
Expand Down Expand Up @@ -131,7 +132,7 @@ async def prepare_transfer_assets(
assert len(recipient_map[k]) <= 100
# insert tables
memo = "world boss ranking rewards by world boss signer"
url = MINER_URLS[NetworkType.MAIN]
url = config.headless_url
task = chord(
sign_transfer_assets.s(
time_stamp,
Expand Down Expand Up @@ -185,7 +186,7 @@ async def stage_transactions(
network_type = NetworkType.INTERNAL
if text.lower() == "main":
network_type = NetworkType.MAIN
headless_urls = HEADLESS_URLS[network_type]
headless_urls = [config.headless_url]
task = chord(
stage_transaction.s(headless_url, nonce)
for headless_url in headless_urls
Expand All @@ -202,7 +203,7 @@ async def transaction_result(
db: Session = Depends(get_db),
):
tx_ids = db.query(Transaction.tx_id).filter_by(tx_result=None)
url = MINER_URLS[NetworkType.MAIN]
url = config.headless_url
task = chord(query_tx_result.s(url, str(tx_id)) for tx_id, in tx_ids)(
upload_tx_result.s(channel_id)
)
Expand All @@ -215,7 +216,7 @@ async def check_balance(
request: Request, channel_id: Annotated[str, Form()], db: Session = Depends(get_db)
):
currencies = get_currencies(db)
url = MINER_URLS[NetworkType.MAIN]
url = config.headless_url
task = chord(check_signer_balance.s(url, currency) for currency in currencies)(
upload_balance_result.s(channel_id)
)
Expand Down
4 changes: 4 additions & 0 deletions world_boss/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Settings(BaseSettings):
sentry_sample_rate: float = 0.1
slack_channel_id: str
graphql_password: str
headless_url: str
data_provider_url: str

class Config:
env_file = ".env"
Expand Down Expand Up @@ -58,6 +60,8 @@ class Config:
"sentry_sample_rate": {"env": "SENTRY_SAMPLE_RATE"},
"slack_channel_id": {"env": "SLACK_CHANNEL_ID"},
"graphql_password": {"env": "GRAPHQL_PASSWORD"},
"headless_url": {"env": "HEADLESS_URL"},
"data_provider_url": {"env": "DATA_PROVIDER_URL"},
}


Expand Down
17 changes: 7 additions & 10 deletions world_boss/app/data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

import httpx

from world_boss.app.config import config
from world_boss.app.enums import NetworkType

__all__ = ["DATA_PROVIDER_URLS", "DataProviderClient", "data_provider_client"]
__all__ = ["DataProviderClient", "data_provider_client"]

from world_boss.app.cache import cache_exists, get_from_cache, set_to_cache
from world_boss.app.stubs import RankingRewardDictionary

TOTAL_USER_QUERY = "query($raidId: Int!) { worldBossTotalUsers(raidId: $raidId) }"
DATA_PROVIDER_URLS: dict[NetworkType, str] = {
NetworkType.MAIN: "https://api.9c.gg/graphql",
NetworkType.INTERNAL: "https://api.9c.gg/graphql",
}
DATA_PROVIDER_URL: str = config.data_provider_url
RANKING_REWARDS_QUERY = """
query($raidId: Int!, $limit: Int!, $offset: Int!) {
worldBossRankingRewards(raidId: $raidId, limit: $limit, offset: $offset) {
Expand All @@ -39,16 +37,16 @@ class DataProviderClient:
def __init__(self):
self._client = httpx.Client(timeout=None)

def _query(self, network_type: NetworkType, query: str, variables: dict):
def _query(self, query: str, variables: dict):
result = self._client.post(
DATA_PROVIDER_URLS[network_type],
DATA_PROVIDER_URL,
json={"query": query, "variables": variables},
)
return result.json()

def get_total_users_count(self, raid_id: int, network_type: NetworkType) -> int:
def get_total_users_count(self, raid_id: int) -> int:
variables = {"raidId": raid_id}
result = self._query(network_type, TOTAL_USER_QUERY, variables)
result = self._query(TOTAL_USER_QUERY, variables)
return result["data"]["worldBossTotalUsers"]

def get_ranking_rewards(
Expand All @@ -60,7 +58,6 @@ def get_ranking_rewards(
rewards = json.loads(cached_value)
else:
result = self._query(
network_type,
RANKING_REWARDS_QUERY,
{"raidId": raid_id, "offset": offset, "limit": limit},
)
Expand Down
Loading

0 comments on commit 08284e4

Please sign in to comment.