Exemplo n.º 1
0
def update_registration(new_data: dict) -> None:
    try:
        _log.info(f"[MATCHMAKING] Putting matchmaking config in storage: {new_data}")
        matchmaking_config = storage.get_json_from_object("MATCHMAKING_CONFIG.json")
        matchmaking_config.update(new_data)
        storage.put_object_as_json("MATCHMAKING_CONFIG.json", matchmaking_config)
        register()
    except Exception:
        raise exceptions.MatchmakingError("Failure updating matchmaking data")
Exemplo n.º 2
0
def make_matchmaking_request(
    http_verb: str, path: str, json_content: dict = None, retry: bool = True, authenticated: bool = True
) -> requests.Response:
    """Make an authenticated request to matchmaking and return the response
    Args:
        http_verb: GET, POST, etc
        path: path of the request
        json_content: OPTIONAL if the request needs a post body, include it as a dictionary
        retry: boolean whether or not to recursively retry on recoverable errors (i.e. no auth, missing registration)
            Note: This should not be provided manually, and is only for recursive calls within the function it
        authenticated: boolean whether or not this matchmaking endpoint is authenticated
    Returns:
        Requests response object
    Raises:
        exceptions.MatchmakingError when unexpected matchmaking error occurs
        exceptions.InsufficientFunds when matchmaking responds with payment required
        exceptions.NotFound when matchmaking responds with a 404
    """
    if json_content is None:
        json_content = {}
    http_verb = http_verb.upper()
    _log.info(f"[MATCHMAKING] Performing {http_verb} request to {path} with data: {json_content}")
    headers, data = None, None
    if authenticated:
        headers, data = authorization.generate_authenticated_request(http_verb, "matchmaking", path, json_content)
    else:
        data = json.dumps(json_content, separators=(",", ":")).encode("utf-8") if json_content else b""
        headers = {"Content-Type": "application/json"} if json_content else {}

    response = requests.request(method=http_verb, url=f"{MATCHMAKING_ADDRESS}{path}", headers=headers, data=data, timeout=REQUEST_TIMEOUT)

    if response.status_code < 200 or response.status_code >= 300:
        if retry and response.status_code == 401 and authenticated:
            _log.warning("[MATCHMAKING] received 401 from matchmaking. Registering new key with matchmaking and trying again")
            authorization.register_new_key_with_matchmaking()
            return make_matchmaking_request(http_verb=http_verb, path=path, json_content=json_content, retry=False, authenticated=authenticated)
        elif retry and response.status_code == 403 and authenticated:
            _log.warning("[MATCHMAKING] received 403 from matchmaking. Registration is probably expired. Re-registering and trying again")
            register()
            return make_matchmaking_request(http_verb=http_verb, path=path, json_content=json_content, retry=False, authenticated=authenticated)
        elif response.status_code == 402:
            raise exceptions.InsufficientFunds("received insufficient funds (402) from matchmaking")
        elif response.status_code == 404:
            raise exceptions.NotFound("Not found (404) from matchmaking")
        raise exceptions.MatchmakingError(
            f"Received unexpected response code {response.status_code} from matchmaking with response:\n{response.text}"
        )

    return response