Pular para conteúdo

Api

Fixtures

Bases: Requester

Source code in cartola_project/api.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Fixtures(Requester):
    def _get_endpoint(self) -> str:
        """Generates fixture`s  the endpoint for the API
        Returns:
            Endpoint string
        """
        return f"{self.base_endpoint}fixtures"

    def _get_params(self, season_year: str, league_id: str) -> List[dict]:
        """Generate a list of parameters for the API

        Args:
            season_year: Season year (in european format this year could different)
            league_id: Id of the league

        Returns:
            List containing a dict of the parameters league and season
        """
        return [{"league": league_id, "season": season_year}]

Matches

Bases: Requester

Source code in cartola_project/api.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Matches(Requester):
    def _get_endpoint(self) -> str:
        """Generates the matches`s endpoint for the API
        Returns:
            Endpoint string
        """
        return f"{self.base_endpoint}fixtures/statistics"

    def _get_params(self, match_id: List[str]) -> List[dict]:
        """Generate a list of parameters for the API

        Args:
            match_id: List of match id

        Returns:
            List containing a dict of the parameters fixture that reporesents a match
        """
        return [{"fixture": id} for id in match_id]

Players

Bases: Requester

Source code in cartola_project/api.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class Players(Requester):
    def _get_endpoint(self) -> str:
        """Generates the player`s endpoint for the API
        Returns:
            Endpoint string
        """
        return f"{self.base_endpoint}fixtures/players"

    def _get_params(self, match_id: List[str]) -> List[dict]:
        """Generate a list of parameters for the API.
        This endpoint is used to get the players of a match.

        Args:
            match_id: List of match id

        Returns:
            List containing a dict of the parameters fixture that represents a match
        """
        return [{"fixture": id} for id in match_id]

Requester

Bases: ABC

Source code in cartola_project/api.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Requester(ABC):
    def __init__(self, api_host_key: str, api_secert_key: str):
        """Abstract base class for all API requesters.
        Work as interface for all API requesters, forcing to implement some methods.
        Also use an exteranl library for rate limiting and back-off exponential
        to timing out requests.

        Args:
            api_host_key: Api credentials host key
            api_secert_key: Api credentials secret key
        """
        self.headers = {
            "X-RapidAPI-Host": api_host_key,
            "X-RapidAPI-Key": api_secert_key,
        }
        self.base_endpoint = "https://api-football-v1.p.rapidapi.com/v3/"

    @abstractmethod
    def _get_endpoint(self) -> str:
        """Abstract method to generate the endpoint for the API"""
        raise NotImplementedError

    @abstractmethod
    def _get_params(self, **kwargs) -> dict | list[dict]:
        """Abstract method to generate the parameters for the API`
        Args:
            **kwargs: Some extra parameters for the API
        """
        raise NotImplementedError

    @backoff.on_exception(backoff.expo, requests.exceptions.HTTPError, max_tries=10, factor=10)
    def get_response(self, endpoint: str, header: str, param: dict) -> requests.Response:
        """Concrete method to get the response from the API. This request method use
        back-off exponential to timing out requests, with max retries and factor are set to 10.
        Also only retry if the response is not 2xx.

        Args:
            endpoint: Endpoint string
            header: Header for the request
            param: Parameter for the request

        Returns:
            Response from the API
        """
        print(f"Request {endpoint} with {param} as parameter")
        response = requests.request("GET", endpoint, headers=header, params=param)
        response.raise_for_status()
        return response

    def get_data(self, **kwargs) -> dict | list[dict]:
        """Method to get the data from the API. This methods warperd _get_response_ to set
        some parameters for the API.

        Args:
            **kwargs: Extra parameters for the API

        Returns:
            Json response from the API
        """
        endpoint = self._get_endpoint()
        params = self._get_params(**kwargs)
        print(f"Using endpoint {endpoint} with {len(params)} parameter(s)")
        responses_json = [
            self.get_response(endpoint, self.headers, param).json() for param in params
        ]
        return responses_json

__init__(api_host_key, api_secert_key)

Abstract base class for all API requesters. Work as interface for all API requesters, forcing to implement some methods. Also use an exteranl library for rate limiting and back-off exponential to timing out requests.

Parameters:

Name Type Description Default
api_host_key str

Api credentials host key

required
api_secert_key str

Api credentials secret key

required
Source code in cartola_project/api.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def __init__(self, api_host_key: str, api_secert_key: str):
    """Abstract base class for all API requesters.
    Work as interface for all API requesters, forcing to implement some methods.
    Also use an exteranl library for rate limiting and back-off exponential
    to timing out requests.

    Args:
        api_host_key: Api credentials host key
        api_secert_key: Api credentials secret key
    """
    self.headers = {
        "X-RapidAPI-Host": api_host_key,
        "X-RapidAPI-Key": api_secert_key,
    }
    self.base_endpoint = "https://api-football-v1.p.rapidapi.com/v3/"

get_data(**kwargs)

Method to get the data from the API. This methods warperd get_response to set some parameters for the API.

Parameters:

Name Type Description Default
**kwargs

Extra parameters for the API

{}

Returns:

Type Description
dict | list[dict]

Json response from the API

Source code in cartola_project/api.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_data(self, **kwargs) -> dict | list[dict]:
    """Method to get the data from the API. This methods warperd _get_response_ to set
    some parameters for the API.

    Args:
        **kwargs: Extra parameters for the API

    Returns:
        Json response from the API
    """
    endpoint = self._get_endpoint()
    params = self._get_params(**kwargs)
    print(f"Using endpoint {endpoint} with {len(params)} parameter(s)")
    responses_json = [
        self.get_response(endpoint, self.headers, param).json() for param in params
    ]
    return responses_json

get_response(endpoint, header, param)

Concrete method to get the response from the API. This request method use back-off exponential to timing out requests, with max retries and factor are set to 10. Also only retry if the response is not 2xx.

Parameters:

Name Type Description Default
endpoint str

Endpoint string

required
header str

Header for the request

required
param dict

Parameter for the request

required

Returns:

Type Description
requests.Response

Response from the API

Source code in cartola_project/api.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@backoff.on_exception(backoff.expo, requests.exceptions.HTTPError, max_tries=10, factor=10)
def get_response(self, endpoint: str, header: str, param: dict) -> requests.Response:
    """Concrete method to get the response from the API. This request method use
    back-off exponential to timing out requests, with max retries and factor are set to 10.
    Also only retry if the response is not 2xx.

    Args:
        endpoint: Endpoint string
        header: Header for the request
        param: Parameter for the request

    Returns:
        Response from the API
    """
    print(f"Request {endpoint} with {param} as parameter")
    response = requests.request("GET", endpoint, headers=header, params=param)
    response.raise_for_status()
    return response

Teams

Bases: Requester

Source code in cartola_project/api.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Teams(Requester):
    def _get_endpoint(self) -> str:
        """Generates the team`s endpoint for the API
        Returns:
            Endpoint string
        """
        return f"{self.base_endpoint}teams"

    def _get_params(self, team_id: List[str]) -> List[dict]:
        """Generate a list of parameters for the API

        Args:
            team_id: List of team id

        Returns:
            List containing a dict of the parameters id that reporesents a team
        """
        return [{"id": id} for id in team_id]