Skip to content

Commit

Permalink
Update asa.py
Browse files Browse the repository at this point in the history
Server can't be queried based on port and JSON output from EOS API varies. Needs more testing.
  • Loading branch information
dkoz committed Nov 14, 2023
1 parent b339cb5 commit 9f33a13
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions discordgsm/protocols/asa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from discordgsm.protocols.protocol import Protocol
import requests
import aiohttp
import base64
import asyncio

class Asa(Protocol):
name = 'asa'
Expand All @@ -15,8 +16,8 @@ def __init__(self, kv):
async def query(self):
host, port = str(self.kv['host']), int(str(self.kv['port']))
try:
access_token = self.get_access_token()
server_info = self.query_server_info(access_token, host, port)
access_token = await self.get_access_token()
server_info = await self.query_server_info(access_token, host, port)

sessions = server_info.get('sessions', [])
if not sessions:
Expand Down Expand Up @@ -55,19 +56,22 @@ async def query(self):

return result

def get_access_token(self):
async def get_access_token(self):
url = f"{self.epic_api}/auth/v1/oauth/token"
auth = base64.b64encode(f"{self.client_id}:{self.client_secret}".encode()).decode()
headers = {
"Authorization": f"Basic {auth}",
"Content-Type": "application/x-www-form-urlencoded"
}
body = f"grant_type=client_credentials&deployment_id={self.deployment_id}"
response = requests.post(url, headers=headers, data=body)
response.raise_for_status()
return response.json()['access_token']

async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=body) as response:
response.raise_for_status()
data = await response.json()
return data['access_token']

def query_server_info(self, access_token, host, port):
async def query_server_info(self, access_token, host, port):
url = f"{self.epic_api}/matchmaking/v1/{self.deployment_id}/filter"
headers = {
"Authorization": f"Bearer {access_token}",
Expand All @@ -77,9 +81,11 @@ def query_server_info(self, access_token, host, port):
body = {
"criteria": [
{"key": "attributes.ADDRESS_s", "op": "EQUAL", "value": host},
{"key": "attributes.ADDRESSBOUND_s", "op": "EQUAL", "value": f"0.0.0.0:{port}"}
{"key": "attributes.ADDRESSBOUND_s", "op": "EQUAL", "value": f"{host}:{port}"}
]
}
response = requests.post(url, headers=headers, json=body)
response.raise_for_status()
return response.json()

async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=body) as response:
response.raise_for_status()
return await response.json()

0 comments on commit 9f33a13

Please sign in to comment.