polars-api 0.1.4__tar.gz → 0.1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: polars-api
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: Polars extension for dealing with REST APIs
5
5
  Author-email: Diego Garcia Lozano <diegoglozano96@gmail.com>
6
6
  Project-URL: Homepage, https://diegoglozano.github.io/polars-api/
@@ -0,0 +1,97 @@
1
+ import asyncio
2
+ from typing import Optional
3
+
4
+ import httpx
5
+ import polars as pl
6
+
7
+
8
+ def check_status_code(status_code):
9
+ return status_code >= 200 and status_code < 300
10
+
11
+
12
+ @pl.api.register_expr_namespace("api")
13
+ class Api:
14
+ def __init__(self, url: pl.Expr) -> None:
15
+ self._url = url
16
+
17
+ @staticmethod
18
+ def _get(url: str, params: Optional[dict[str, str]] = None, timeout: Optional[float] = None) -> Optional[str]:
19
+ result = httpx.get(url, params=params, timeout=timeout)
20
+ if check_status_code(result.status_code):
21
+ return result.text
22
+ else:
23
+ return None
24
+
25
+ @staticmethod
26
+ def _post(url: str, params: dict[str, str], body: str, timeout: float) -> Optional[str]:
27
+ result = httpx.post(url, params=params, json=body, timeout=timeout)
28
+ if check_status_code(result.status_code):
29
+ return result.text
30
+ else:
31
+ return None
32
+
33
+ @staticmethod
34
+ async def _aget_one(url: str, params: str, timeout: float) -> str:
35
+ async with httpx.AsyncClient() as client:
36
+ r = await client.get(url, params=params, timeout=timeout)
37
+ return r.text
38
+
39
+ async def _aget_all(self, x, params, timeout):
40
+ return await asyncio.gather(*[self._aget_one(url, param, timeout) for url, param in zip(x, params)])
41
+
42
+ def _aget(self, x, params, timeout):
43
+ return pl.Series(asyncio.run(self._aget_all(x, params, timeout)))
44
+
45
+ @staticmethod
46
+ async def _apost_one(url: str, params: str, body: str, timeout: Optional[float]) -> str:
47
+ async with httpx.AsyncClient() as client:
48
+ r = await client.post(url, params=params, json=body, timeout=timeout)
49
+ return r.text
50
+
51
+ async def _apost_all(self, x, params, body, timeout):
52
+ return await asyncio.gather(*[
53
+ self._apost_one(url, _params, _body, timeout) for url, _params, _body in zip(x, params, body)
54
+ ])
55
+
56
+ def _apost(self, x, params, body, timeout):
57
+ return pl.Series(asyncio.run(self._apost_all(x, params, body, timeout)))
58
+
59
+ def get(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
60
+ if params is None:
61
+ params = pl.lit(None)
62
+ return pl.struct(self._url.alias("url"), params.alias("params")).map_elements(
63
+ lambda x: self._get(x["url"], params=x["params"], timeout=timeout),
64
+ return_dtype=pl.Utf8,
65
+ )
66
+
67
+ def post(
68
+ self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
69
+ ) -> pl.Expr:
70
+ if params is None:
71
+ params = pl.lit(None)
72
+ if body is None:
73
+ body = pl.lit(None)
74
+ return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_elements(
75
+ lambda x: self._post(x["url"], params=x["params"], body=x["body"], timeout=timeout),
76
+ return_dtype=pl.Utf8,
77
+ )
78
+
79
+ def aget(self, params: Optional[pl.Expr] = None, timeout: Optional[float] = None) -> pl.Expr:
80
+ if params is None:
81
+ params = pl.lit(None)
82
+ return pl.struct(self._url.alias("url"), params.alias("params")).map_batches(
83
+ lambda x: self._aget(x.struct.field("url"), params=x.struct.field("params"), timeout=timeout)
84
+ )
85
+
86
+ def apost(
87
+ self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None, timeout: Optional[float] = None
88
+ ) -> pl.Expr:
89
+ if params is None:
90
+ params = pl.lit(None)
91
+ if body is None:
92
+ body = pl.lit(None)
93
+ return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_batches(
94
+ lambda x: self._apost(
95
+ x.struct.field("url"), params=x.struct.field("params"), body=x.struct.field("body"), timeout=timeout
96
+ )
97
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: polars-api
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: Polars extension for dealing with REST APIs
5
5
  Author-email: Diego Garcia Lozano <diegoglozano96@gmail.com>
6
6
  Project-URL: Homepage, https://diegoglozano.github.io/polars-api/
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "polars-api"
3
- version = "0.1.4"
3
+ version = "0.1.5"
4
4
  description = "Polars extension for dealing with REST APIs"
5
5
  authors = [{ name = "Diego Garcia Lozano", email = "diegoglozano96@gmail.com" }]
6
6
  readme = "README.md"
@@ -1,91 +0,0 @@
1
- import asyncio
2
- from typing import Optional
3
-
4
- import httpx
5
- import polars as pl
6
-
7
-
8
- def check_status_code(status_code):
9
- return status_code >= 200 and status_code < 300
10
-
11
-
12
- @pl.api.register_expr_namespace("api")
13
- class Api:
14
- def __init__(self, url: pl.Expr) -> None:
15
- self._url = url
16
-
17
- @staticmethod
18
- def _get(url: str, params: Optional[dict[str, str]] = None) -> Optional[str]:
19
- result = httpx.get(url, params=params)
20
- if check_status_code(result.status_code):
21
- return result.text
22
- else:
23
- return None
24
-
25
- @staticmethod
26
- def _post(url: str, params: dict[str, str], body: str) -> Optional[str]:
27
- result = httpx.post(url, params=params, json=body)
28
- if check_status_code(result.status_code):
29
- return result.text
30
- else:
31
- return None
32
-
33
- @staticmethod
34
- async def _aget_one(url: str, params: str) -> str:
35
- async with httpx.AsyncClient() as client:
36
- r = await client.get(url, params=params)
37
- return r.text
38
-
39
- async def _aget_all(self, x, params):
40
- return await asyncio.gather(*[self._aget_one(url, param) for url, param in zip(x, params)])
41
-
42
- def _aget(self, x, params):
43
- return pl.Series(asyncio.run(self._aget_all(x, params)))
44
-
45
- @staticmethod
46
- async def _apost_one(url: str, params: str, body: str) -> str:
47
- async with httpx.AsyncClient() as client:
48
- r = await client.post(url, params=params, json=body)
49
- return r.text
50
-
51
- async def _apost_all(self, x, params, body):
52
- return await asyncio.gather(*[
53
- self._apost_one(url, _params, _body) for url, _params, _body in zip(x, params, body)
54
- ])
55
-
56
- def _apost(self, x, params, body):
57
- return pl.Series(asyncio.run(self._apost_all(x, params, body)))
58
-
59
- def get(self, params: Optional[pl.Expr] = None) -> pl.Expr:
60
- if params is None:
61
- params = pl.lit(None)
62
- return pl.struct(self._url.alias("url"), params.alias("params")).map_elements(
63
- lambda x: self._get(x["url"], params=x["params"]),
64
- return_dtype=pl.Utf8,
65
- )
66
-
67
- def post(self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None) -> pl.Expr:
68
- if params is None:
69
- params = pl.lit(None)
70
- if body is None:
71
- body = pl.lit(None)
72
- return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_elements(
73
- lambda x: self._post(x["url"], params=x["params"], body=x["body"]),
74
- return_dtype=pl.Utf8,
75
- )
76
-
77
- def aget(self, params: Optional[pl.Expr] = None) -> pl.Expr:
78
- if params is None:
79
- params = pl.lit(None)
80
- return pl.struct(self._url.alias("url"), params.alias("params")).map_batches(
81
- lambda x: self._aget(x.struct.field("url"), params=x.struct.field("params"))
82
- )
83
-
84
- def apost(self, params: Optional[pl.Expr] = None, body: Optional[pl.Expr] = None) -> pl.Expr:
85
- if params is None:
86
- params = pl.lit(None)
87
- if body is None:
88
- body = pl.lit(None)
89
- return pl.struct(self._url.alias("url"), params.alias("params"), body.alias("body")).map_batches(
90
- lambda x: self._apost(x.struct.field("url"), params=x.struct.field("params"), body=x.struct.field("body"))
91
- )
File without changes
File without changes
File without changes
File without changes
File without changes