Skip to content

Client

Client

Source code in src/rated/client.py
 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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Client:
    def __init__(self, api_key: str, network: str):
        """
        Initialize a client instance with an API key and a network

        Args:
            api_key: Rated API key
            network: Supported network
        """
        self.api_key = api_key
        self.network: str = network
        self.headers = httpx.Headers(
            {
                "User-Agent": f"rated-python/{__version__}",
                "Authorization": f"Bearer {self.api_key}",
                "X-Rated-Network": self.network,
            }
        )

        self.client = httpx.Client(
            base_url=api_base_url,
            follow_redirects=True,
            event_hooks={
                "request": [
                    check_for_user_agent,
                    check_for_auth_header,
                    check_for_empty_query_params,
                ],
                "response": [raise_on_4xx_5xx],
            },
        )

    def get(self, *args, **kwargs) -> Any:
        """
        Make a GET request to the Rated API

        Args:
            *args: Positional arguments
            **kwargs: Keyword arguments

        Returns:
            JSON data from the response
        """
        params = kwargs.get("params", {}).copy()
        kwargs["headers"] = self.headers
        kwargs["params"] = {k: v for k, v in params.items() if v is not None}
        response = self.client.get(*args, **kwargs)
        return response.json()

    def post(self, *args, **kwargs) -> httpx.Response:
        """
        Make a POST request to the Rated API

        Args:
            *args: Positional arguments
            **kwargs: Keyword arguments

        Returns:
            Response
        """
        kwargs["headers"] = self.headers
        return self.client.post(*args, **kwargs)

    def yield_paginated_results(
        self,
        url: str,
        *,
        params: dict | None = None,
        cls: Type | None = None,
        follow_next: bool = False,
    ) -> Iterator:
        """
        Yield all results of a paginated response from the Rated API

        Args:
            url: The URL of the desired resource
            params: Query parameters for the request
            cls: Dataclass to be used to instantiate the new Python object
            follow_next: Follow next page if any and fetch its results

        Returns:
            An iterator over the results of the page
        """
        params_: Dict[str, Any] | None = params.copy() if params else None
        if params_:
            params_ = {k: v for k, v in params_.items() if v is not None}
        while True:
            response = self.client.get(url, params=params_, headers=self.headers)
            content = response.json()
            for item in content["data"]:
                if cls:
                    yield json_to_instance(item, cls)
                else:
                    yield item

            if not content["next"] or not follow_next:
                break

            url = content["next"]
            params_ = None

__init__(api_key, network)

Initialize a client instance with an API key and a network

Parameters:

Name Type Description Default
api_key str

Rated API key

required
network str

Supported network

required
Source code in src/rated/client.py
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
74
75
76
def __init__(self, api_key: str, network: str):
    """
    Initialize a client instance with an API key and a network

    Args:
        api_key: Rated API key
        network: Supported network
    """
    self.api_key = api_key
    self.network: str = network
    self.headers = httpx.Headers(
        {
            "User-Agent": f"rated-python/{__version__}",
            "Authorization": f"Bearer {self.api_key}",
            "X-Rated-Network": self.network,
        }
    )

    self.client = httpx.Client(
        base_url=api_base_url,
        follow_redirects=True,
        event_hooks={
            "request": [
                check_for_user_agent,
                check_for_auth_header,
                check_for_empty_query_params,
            ],
            "response": [raise_on_4xx_5xx],
        },
    )

get(*args, **kwargs)

Make a GET request to the Rated API

Parameters:

Name Type Description Default
*args

Positional arguments

()
**kwargs

Keyword arguments

{}

Returns:

Type Description
Any

JSON data from the response

Source code in src/rated/client.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def get(self, *args, **kwargs) -> Any:
    """
    Make a GET request to the Rated API

    Args:
        *args: Positional arguments
        **kwargs: Keyword arguments

    Returns:
        JSON data from the response
    """
    params = kwargs.get("params", {}).copy()
    kwargs["headers"] = self.headers
    kwargs["params"] = {k: v for k, v in params.items() if v is not None}
    response = self.client.get(*args, **kwargs)
    return response.json()

post(*args, **kwargs)

Make a POST request to the Rated API

Parameters:

Name Type Description Default
*args

Positional arguments

()
**kwargs

Keyword arguments

{}

Returns:

Type Description
Response

Response

Source code in src/rated/client.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def post(self, *args, **kwargs) -> httpx.Response:
    """
    Make a POST request to the Rated API

    Args:
        *args: Positional arguments
        **kwargs: Keyword arguments

    Returns:
        Response
    """
    kwargs["headers"] = self.headers
    return self.client.post(*args, **kwargs)

yield_paginated_results(url, *, params=None, cls=None, follow_next=False)

Yield all results of a paginated response from the Rated API

Parameters:

Name Type Description Default
url str

The URL of the desired resource

required
params dict | None

Query parameters for the request

None
cls Type | None

Dataclass to be used to instantiate the new Python object

None
follow_next bool

Follow next page if any and fetch its results

False

Returns:

Type Description
Iterator

An iterator over the results of the page

Source code in src/rated/client.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def yield_paginated_results(
    self,
    url: str,
    *,
    params: dict | None = None,
    cls: Type | None = None,
    follow_next: bool = False,
) -> Iterator:
    """
    Yield all results of a paginated response from the Rated API

    Args:
        url: The URL of the desired resource
        params: Query parameters for the request
        cls: Dataclass to be used to instantiate the new Python object
        follow_next: Follow next page if any and fetch its results

    Returns:
        An iterator over the results of the page
    """
    params_: Dict[str, Any] | None = params.copy() if params else None
    if params_:
        params_ = {k: v for k, v in params_.items() if v is not None}
    while True:
        response = self.client.get(url, params=params_, headers=self.headers)
        content = response.json()
        for item in content["data"]:
            if cls:
                yield json_to_instance(item, cls)
            else:
                yield item

        if not content["next"] or not follow_next:
            break

        url = content["next"]
        params_ = None

json_to_instance(json_, cls)

Converts a camelCased JSON to a Python object instance

Parameters:

Name Type Description Default
json_ Dict

The JSON data to convert

required
cls Type

Dataclass to be used to instantiate the new Python object

required

Returns:

Type Description
Any

An instance of the given class

Source code in src/rated/client.py
148
149
150
151
152
153
154
155
156
157
158
159
160
def json_to_instance(json_: Dict, cls: Type) -> Any:
    """
    Converts a camelCased JSON to a Python object instance

    Args:
        json_: The JSON data to convert
        cls: Dataclass to be used to instantiate the new Python object

    Returns:
        An instance of the given class
    """
    decamelized = {humps.decamelize(k): v for k, v in json_.items()}
    return cls(**decamelized)