Skip to content

Documentation

Api

Source code in polars_api/api.py
 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
 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
@pl.api.register_expr_namespace("api")
class Api:
    def __init__(self, url: pl.Expr) -> None:
        """
        Initialize the Api class with a URL expression.

        Parameters
        ----------
        url : pl.Expr
            The URL expression.
        """
        self._url = url

    @staticmethod
    def _get(url: str, params: Optional[dict[str, str]] = None, timeout: Optional[float] = None) -> Optional[str]:
        """
        Perform a synchronous GET request.

        Parameters
        ----------
        url : str
            The URL to send the GET request to.
        params : dict[str, str], optional
            The query parameters to include in the request.
        timeout : float, optional
            The timeout for the request.

        Returns
        -------
        str or None
            The response text if the request is successful, None otherwise.
        """
        result = httpx.get(url, params=params, timeout=timeout)
        if _check_status_code(result.status_code):
            return result.text
        else:
            return None

    @staticmethod
    def _post(url: str, params: dict[str, str], body: str, timeout: float) -> Optional[str]:
        """
        Perform a synchronous POST request.

        Parameters
        ----------
        url : str
            The URL to send the POST request to.
        params : dict[str, str]
            The query parameters to include in the request.
        body : str
            The JSON body to include in the request.
        timeout : float
            The timeout for the request.

        Returns
        -------
        str or None
            The response text if the request is successful, None otherwise.
        """
        result = httpx.post(url, params=params, json=body, timeout=timeout)
        if _check_status_code(result.status_code):
            return result.text
        else:
            return None

    @staticmethod
    async def _aget_one(url: str, params: str, timeout: float) -> str:
        """
        Perform an asynchronous GET request.

        Parameters
        ----------
        url : str
            The URL to send the GET request to.
        params : str
            The query parameters to include in the request.
        timeout : float
            The timeout for the request.

        Returns
        -------
        str
            The response text.
        """
        async with httpx.AsyncClient() as client:
            r = await client.get(url, params=params, timeout=timeout)
            return r.text

    async def _aget_all(self, x, params, timeout):
        """
        Perform multiple asynchronous GET requests.

        Parameters
        ----------
        x : list
            List of URLs to send the GET requests to.
        params : list
            List of query parameters for each request.
        timeout : float
            The timeout for the requests.

        Returns
        -------
        list
            List of response texts.
        """
        return await asyncio.gather(*[self._aget_one(url, param, timeout) for url, param in zip(x, params)])

    def _aget(self, x, params, timeout):
        """
        Wrapper for performing multiple asynchronous GET requests.

        Parameters
        ----------
        x : list
            List of URLs to send the GET requests to.
        params : list
            List of query parameters for each request.
        timeout : float
            The timeout for the requests.

        Returns
        -------
        pl.Series
            Series of response texts.
        """
        return pl.Series(asyncio.run(self._aget_all(x, params, timeout)))

    @staticmethod
    async def _apost_one(url: str, params: str, body: str, timeout: Optional[float]) -> str:
        """
        Perform an asynchronous POST request.

        Parameters
        ----------
        url : str
            The URL to send the POST request to.
        params : str
            The query parameters to include in the request.
        body : str
            The JSON body to include in the request.
        timeout : float, optional
            The timeout for the request.

        Returns
        -------
        str
            The response text.
        """
        async with httpx.AsyncClient() as client:
            r = await client.post(url, params=params, json=body, timeout=timeout)
            return r.text

    async def _apost_all(self, x, params, body, timeout):
        """
        Perform multiple asynchronous POST requests.

        Parameters
        ----------
        x : list
            List of URLs to send the POST requests to.
        params : list
            List of query parameters for each request.
        body : list
            List of JSON bodies for each request.
        timeout : float
            The timeout for the requests.

        Returns
        -------
        list
            List of response texts.
        """
        return await asyncio.gather(*[
            self._apost_one(url, _params, _body, timeout) for url, _params, _body in zip(x, params, body)
        ])

    def _apost(self, x, params, body, timeout):
        """
        Wrapper for performing multiple asynchronous POST requests.

        Parameters
        ----------
        x : list
            List of URLs to send the POST requests to.
        params : list
            List of query parameters for each request.
        body : list
            List of JSON bodies for each request.
        timeout : float
            The timeout for the requests.

        Returns
        -------
        pl.Series
            Series of response texts.
        """
        return pl.Series(asyncio.run(self._apost_all(x, params, body, timeout)))

    def get(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
        """
        Perform a synchronous GET request for each URL in the expression.

        Parameters
        ----------
        params : pl.Expr, optional
            The query parameters expression.
        timeout : float, optional
            The timeout for the requests.

        Returns
        -------
        pl.Expr
            Expression containing the response texts.
        """
        if params is None:
            params = pl.lit(None)
        return pl.struct(self._url.alias("url"), params.alias("params")).map_elements(
            lambda x: self._get(x["url"], params=x["params"], timeout=timeout),
            return_dtype=pl.Utf8,
        )

    def post(
        self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
    ) -> pl.Expr:
        """
        Perform a synchronous POST request for each URL in the expression.

        Parameters
        ----------
        params : pl.Expr, optional
            The query parameters expression.
        body : pl.Expr, optional
            The JSON body expression.
        timeout : float, optional
            The timeout for the requests.

        Returns
        -------
        pl.Expr
            Expression containing the response texts.
        """
        if params is None:
            params = pl.lit(None)
        if body is None:
            body = pl.lit(None)
        return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_elements(
            lambda x: self._post(x["url"], params=x["params"], body=x["body"], timeout=timeout),
            return_dtype=pl.Utf8,
        )

    def aget(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
        """
        Perform an asynchronous GET request for each URL in the expression.

        Parameters
        ----------
        params : pl.Expr, optional
            The query parameters expression.
        timeout : float, optional
            The timeout for the requests.

        Returns
        -------
        pl.Expr
            Expression containing the response texts.
        """
        nest_asyncio.apply()
        if params is None:
            params = pl.lit(None)
        return pl.struct(self._url.alias("url"), params.alias("params")).map_batches(
            lambda x: self._aget(x.struct.field("url"), params=x.struct.field("params"), timeout=timeout)
        )

    def apost(
        self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
    ) -> pl.Expr:
        """
        Perform an asynchronous POST request for each URL in the expression.

        Parameters
        ----------
        params : pl.Expr, optional
            The query parameters expression.
        body : pl.Expr, optional
            The JSON body expression.
        timeout : float, optional
            The timeout for the requests.

        Returns
        -------
        pl.Expr
            Expression containing the response texts.
        """
        nest_asyncio.apply()
        if params is None:
            params = pl.lit(None)
        if body is None:
            body = pl.lit(None)
        return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_batches(
            lambda x: self._apost(
                x.struct.field("url"), params=x.struct.field("params"), body=x.struct.field("body"), timeout=timeout
            )
        )

__init__(url)

Initialize the Api class with a URL expression.

Parameters:

Name Type Description Default
url Expr

The URL expression.

required
Source code in polars_api/api.py
28
29
30
31
32
33
34
35
36
37
def __init__(self, url: pl.Expr) -> None:
    """
    Initialize the Api class with a URL expression.

    Parameters
    ----------
    url : pl.Expr
        The URL expression.
    """
    self._url = url

aget(params=None, timeout=None)

Perform an asynchronous GET request for each URL in the expression.

Parameters:

Name Type Description Default
params Expr

The query parameters expression.

None
timeout float

The timeout for the requests.

None

Returns:

Type Description
Expr

Expression containing the response texts.

Source code in polars_api/api.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def aget(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
    """
    Perform an asynchronous GET request for each URL in the expression.

    Parameters
    ----------
    params : pl.Expr, optional
        The query parameters expression.
    timeout : float, optional
        The timeout for the requests.

    Returns
    -------
    pl.Expr
        Expression containing the response texts.
    """
    nest_asyncio.apply()
    if params is None:
        params = pl.lit(None)
    return pl.struct(self._url.alias("url"), params.alias("params")).map_batches(
        lambda x: self._aget(x.struct.field("url"), params=x.struct.field("params"), timeout=timeout)
    )

apost(params=None, body=None, timeout=None)

Perform an asynchronous POST request for each URL in the expression.

Parameters:

Name Type Description Default
params Expr

The query parameters expression.

None
body Expr

The JSON body expression.

None
timeout float

The timeout for the requests.

None

Returns:

Type Description
Expr

Expression containing the response texts.

Source code in polars_api/api.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
def apost(
    self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
) -> pl.Expr:
    """
    Perform an asynchronous POST request for each URL in the expression.

    Parameters
    ----------
    params : pl.Expr, optional
        The query parameters expression.
    body : pl.Expr, optional
        The JSON body expression.
    timeout : float, optional
        The timeout for the requests.

    Returns
    -------
    pl.Expr
        Expression containing the response texts.
    """
    nest_asyncio.apply()
    if params is None:
        params = pl.lit(None)
    if body is None:
        body = pl.lit(None)
    return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_batches(
        lambda x: self._apost(
            x.struct.field("url"), params=x.struct.field("params"), body=x.struct.field("body"), timeout=timeout
        )
    )

get(params=None, timeout=None)

Perform a synchronous GET request for each URL in the expression.

Parameters:

Name Type Description Default
params Expr

The query parameters expression.

None
timeout float

The timeout for the requests.

None

Returns:

Type Description
Expr

Expression containing the response texts.

Source code in polars_api/api.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def get(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
    """
    Perform a synchronous GET request for each URL in the expression.

    Parameters
    ----------
    params : pl.Expr, optional
        The query parameters expression.
    timeout : float, optional
        The timeout for the requests.

    Returns
    -------
    pl.Expr
        Expression containing the response texts.
    """
    if params is None:
        params = pl.lit(None)
    return pl.struct(self._url.alias("url"), params.alias("params")).map_elements(
        lambda x: self._get(x["url"], params=x["params"], timeout=timeout),
        return_dtype=pl.Utf8,
    )

post(params=None, body=None, timeout=None)

Perform a synchronous POST request for each URL in the expression.

Parameters:

Name Type Description Default
params Expr

The query parameters expression.

None
body Expr

The JSON body expression.

None
timeout float

The timeout for the requests.

None

Returns:

Type Description
Expr

Expression containing the response texts.

Source code in polars_api/api.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
def post(
    self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
) -> pl.Expr:
    """
    Perform a synchronous POST request for each URL in the expression.

    Parameters
    ----------
    params : pl.Expr, optional
        The query parameters expression.
    body : pl.Expr, optional
        The JSON body expression.
    timeout : float, optional
        The timeout for the requests.

    Returns
    -------
    pl.Expr
        Expression containing the response texts.
    """
    if params is None:
        params = pl.lit(None)
    if body is None:
        body = pl.lit(None)
    return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_elements(
        lambda x: self._post(x["url"], params=x["params"], body=x["body"], timeout=timeout),
        return_dtype=pl.Utf8,
    )