Module valolyticspy.services.api.valolytics

Expand source code
import requests, sys

sys.path.append("")
from valolyticspy.models.match import MatchModel
from valolyticspy.dtos.valolytics.playerstats import PlayerStatsDto
from typing import List, Dict
from valolyticspy.dtos.riot.enums import MatchRegion
from valolyticspy.dtos.riot.match import MatchDto
from valolyticspy.exceptions import FailedAPIRequestException

class ValolyticsClient:
    """
    A client for interacting with the Valolytics API.

    Attributes:
        VALOLYTICS_API_KEY (str): The API key used for making requests to the Valolytics API.

    Methods:
        __init__(api_key: str): Initializes a new instance of the ValolyticsClient class.
        get_match_by_match_id(matchid: str, region: MatchRegion) -> MatchModel:
            Retrieves the match information by match ID.
        get_playerstats_by_match_id(matchid: str, region: MatchRegion) -> Dict[str, PlayerStatsDto]:
            Retrieves the player statistics by match ID.
        get_playerstats_by_match_ids(matchids: List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]:
            Retrieves the player statistics by match IDs.
        handle_request(url: str) -> dict:
            Handles the HTTP request to the Valolytics API.

    """
    
    VALOLYTICS_API_KEY: str
    
    def __init__(self, api_key: str) -> None:
        """
        Initializes a new instance of the ValolyticsClient class.

        Args:
            api_key (str): The API key used for making requests to the Valolytics API.

        """
        self.VALOLYTICS_API_KEY = api_key

    def get_match_by_match_id(self, matchid:str, region: MatchRegion) -> MatchModel:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match is located.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/matches/{region.value}/{matchid}")
        return MatchModel(MatchDto(**data), self, region)
    
    def get_playerstats_by_match_id(self, matchid:str, region: MatchRegion) -> Dict[str, PlayerStatsDto]:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match was played.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/{matchid}", method='POST')
        return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
    
    def get_playerstats_by_match_ids(self, matchids:List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match was played.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/list", body=matchids, method='POST')
        return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
    
    def handle_request(self, url: str, body: dict = None, method: str = 'GET') -> dict:
        """
        Sends a GET request to the specified URL with the given headers and returns the response as a JSON object.

        Args:
            url (str): The URL to send the GET request to.
            headers (dict, optional): The headers to include in the request. Defaults to {'user-agent': 'mozilla/5.0'}.

        Returns:
            dict: The response from the API as a JSON object.

        Raises:
            FailedAPIRequestException: If the response status code is not 200.
        """
        headers: dict = {'user-agent': 'mozilla/5.0', 'x-api-key': self.VALOLYTICS_API_KEY}
        if method == 'GET':
            response = requests.get(url, headers=headers)
        elif method == 'POST':
            if body is not None:
                response = requests.post(url, headers=headers, json=body)
            else:
                response = requests.post(url, headers=headers)
        else:
            raise FailedAPIRequestException("Invalid method.")
        if response.status_code != 200:
            raise FailedAPIRequestException()
        return response.json()

Classes

class ValolyticsClient (api_key: str)

A client for interacting with the Valolytics API.

Attributes

VALOLYTICS_API_KEY : str
The API key used for making requests to the Valolytics API.

Methods

init(api_key: str): Initializes a new instance of the ValolyticsClient class. get_match_by_match_id(matchid: str, region: MatchRegion) -> MatchModel: Retrieves the match information by match ID. get_playerstats_by_match_id(matchid: str, region: MatchRegion) -> Dict[str, PlayerStatsDto]: Retrieves the player statistics by match ID. get_playerstats_by_match_ids(matchids: List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]: Retrieves the player statistics by match IDs. handle_request(url: str) -> dict: Handles the HTTP request to the Valolytics API.

Initializes a new instance of the ValolyticsClient class.

Args

api_key : str
The API key used for making requests to the Valolytics API.
Expand source code
class ValolyticsClient:
    """
    A client for interacting with the Valolytics API.

    Attributes:
        VALOLYTICS_API_KEY (str): The API key used for making requests to the Valolytics API.

    Methods:
        __init__(api_key: str): Initializes a new instance of the ValolyticsClient class.
        get_match_by_match_id(matchid: str, region: MatchRegion) -> MatchModel:
            Retrieves the match information by match ID.
        get_playerstats_by_match_id(matchid: str, region: MatchRegion) -> Dict[str, PlayerStatsDto]:
            Retrieves the player statistics by match ID.
        get_playerstats_by_match_ids(matchids: List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]:
            Retrieves the player statistics by match IDs.
        handle_request(url: str) -> dict:
            Handles the HTTP request to the Valolytics API.

    """
    
    VALOLYTICS_API_KEY: str
    
    def __init__(self, api_key: str) -> None:
        """
        Initializes a new instance of the ValolyticsClient class.

        Args:
            api_key (str): The API key used for making requests to the Valolytics API.

        """
        self.VALOLYTICS_API_KEY = api_key

    def get_match_by_match_id(self, matchid:str, region: MatchRegion) -> MatchModel:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match is located.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/matches/{region.value}/{matchid}")
        return MatchModel(MatchDto(**data), self, region)
    
    def get_playerstats_by_match_id(self, matchid:str, region: MatchRegion) -> Dict[str, PlayerStatsDto]:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match was played.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/{matchid}", method='POST')
        return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
    
    def get_playerstats_by_match_ids(self, matchids:List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]:
        """
        Retrieves the match information by match ID.

        Args:
            matchid (str): The ID of the match.
            region (MatchRegion): The region where the match was played.

        Returns:
            MatchModel: The match information.

        """
        data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/list", body=matchids, method='POST')
        return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
    
    def handle_request(self, url: str, body: dict = None, method: str = 'GET') -> dict:
        """
        Sends a GET request to the specified URL with the given headers and returns the response as a JSON object.

        Args:
            url (str): The URL to send the GET request to.
            headers (dict, optional): The headers to include in the request. Defaults to {'user-agent': 'mozilla/5.0'}.

        Returns:
            dict: The response from the API as a JSON object.

        Raises:
            FailedAPIRequestException: If the response status code is not 200.
        """
        headers: dict = {'user-agent': 'mozilla/5.0', 'x-api-key': self.VALOLYTICS_API_KEY}
        if method == 'GET':
            response = requests.get(url, headers=headers)
        elif method == 'POST':
            if body is not None:
                response = requests.post(url, headers=headers, json=body)
            else:
                response = requests.post(url, headers=headers)
        else:
            raise FailedAPIRequestException("Invalid method.")
        if response.status_code != 200:
            raise FailedAPIRequestException()
        return response.json()

Class variables

var VALOLYTICS_API_KEY : str

Methods

def get_match_by_match_id(self, matchid: str, region: MatchRegion) ‑> MatchModel

Retrieves the match information by match ID.

Args

matchid : str
The ID of the match.
region : MatchRegion
The region where the match is located.

Returns

MatchModel
The match information.
Expand source code
def get_match_by_match_id(self, matchid:str, region: MatchRegion) -> MatchModel:
    """
    Retrieves the match information by match ID.

    Args:
        matchid (str): The ID of the match.
        region (MatchRegion): The region where the match is located.

    Returns:
        MatchModel: The match information.

    """
    data = self.handle_request(url=f"https://api.valolytics.gg/api/matches/{region.value}/{matchid}")
    return MatchModel(MatchDto(**data), self, region)
def get_playerstats_by_match_id(self, matchid: str, region: MatchRegion) ‑> Dict[str, PlayerStatsDto]

Retrieves the match information by match ID.

Args

matchid : str
The ID of the match.
region : MatchRegion
The region where the match was played.

Returns

MatchModel
The match information.
Expand source code
def get_playerstats_by_match_id(self, matchid:str, region: MatchRegion) -> Dict[str, PlayerStatsDto]:
    """
    Retrieves the match information by match ID.

    Args:
        matchid (str): The ID of the match.
        region (MatchRegion): The region where the match was played.

    Returns:
        MatchModel: The match information.

    """
    data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/{matchid}", method='POST')
    return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
def get_playerstats_by_match_ids(self, matchids: List[str], region: MatchRegion) ‑> Dict[str, PlayerStatsDto]

Retrieves the match information by match ID.

Args

matchid : str
The ID of the match.
region : MatchRegion
The region where the match was played.

Returns

MatchModel
The match information.
Expand source code
def get_playerstats_by_match_ids(self, matchids:List[str], region: MatchRegion) -> Dict[str, PlayerStatsDto]:
    """
    Retrieves the match information by match ID.

    Args:
        matchid (str): The ID of the match.
        region (MatchRegion): The region where the match was played.

    Returns:
        MatchModel: The match information.

    """
    data = self.handle_request(url=f"https://api.valolytics.gg/api/stats/playerstats/{region.value}/list", body=matchids, method='POST')
    return {puuid: PlayerStatsDto(**data[puuid]) for puuid in data}
def handle_request(self, url: str, body: dict = None, method: str = 'GET') ‑> dict

Sends a GET request to the specified URL with the given headers and returns the response as a JSON object.

Args

url : str
The URL to send the GET request to.
headers : dict, optional
The headers to include in the request. Defaults to {'user-agent': 'mozilla/5.0'}.

Returns

dict
The response from the API as a JSON object.

Raises

FailedAPIRequestException
If the response status code is not 200.
Expand source code
def handle_request(self, url: str, body: dict = None, method: str = 'GET') -> dict:
    """
    Sends a GET request to the specified URL with the given headers and returns the response as a JSON object.

    Args:
        url (str): The URL to send the GET request to.
        headers (dict, optional): The headers to include in the request. Defaults to {'user-agent': 'mozilla/5.0'}.

    Returns:
        dict: The response from the API as a JSON object.

    Raises:
        FailedAPIRequestException: If the response status code is not 200.
    """
    headers: dict = {'user-agent': 'mozilla/5.0', 'x-api-key': self.VALOLYTICS_API_KEY}
    if method == 'GET':
        response = requests.get(url, headers=headers)
    elif method == 'POST':
        if body is not None:
            response = requests.post(url, headers=headers, json=body)
        else:
            response = requests.post(url, headers=headers)
    else:
        raise FailedAPIRequestException("Invalid method.")
    if response.status_code != 200:
        raise FailedAPIRequestException()
    return response.json()