xn-client 0.0.3.dev2__tar.gz → 0.0.3.dev4__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.
@@ -32,5 +32,6 @@ repos:
32
32
  ### Run the formatter.
33
33
  - id: ruff-format
34
34
  types_or: [python, pyi]
35
+ args: [--preview]
35
36
  verbose: true
36
37
  stages: [pre-commit]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-client
3
- Version: 0.0.3.dev2
3
+ Version: 0.0.3.dev4
4
4
  Author-email: Mike Artemiev <mixartemev@gmail.com>
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/XyncNet/x-client
@@ -35,6 +35,9 @@ local_scheme = "no-local-version"
35
35
  [tool.ruff]
36
36
  line-length = 120
37
37
 
38
+ [tool.ruff.format]
39
+ preview = true
40
+
38
41
  [tool.pytest.ini_options]
39
42
  asyncio_mode = "auto"
40
43
  asyncio_default_fixture_loop_scope = "module" # , "session", "class", "package", "function"
@@ -1,3 +1,5 @@
1
+ import asyncio
2
+
1
3
  from aiohttp import ClientSession, ClientResponse
2
4
  from aiohttp.http_exceptions import HttpProcessingError
3
5
 
@@ -17,24 +19,34 @@ class Client:
17
19
  async def close(self):
18
20
  await self.session.close()
19
21
 
20
- async def _get(self, url: str, params: dict = None, **kwargs):
21
- resp: ClientResponse = await self.session.get(url, params=params, **kwargs)
22
+ # noinspection PyMethodMayBeStatic
23
+ def _prehook(self, _payload: dict = None):
24
+ return {}
25
+
26
+ async def _get(self, url: str, params: dict = None):
27
+ asyncio.get_running_loop()
28
+ resp = await self.session.get(url, params=params, headers=self._prehook(params))
22
29
  return await self._proc(resp)
23
30
 
24
- async def _post(self, url: str, data: dict = None, params: dict = None, **kwargs):
31
+ async def _post(self, url: str, data: dict = None):
25
32
  dt = {"json" if isinstance(data, dict) else "data": data}
26
- resp = await self.session.post(url, **dt, **kwargs)
33
+ resp = await self.session.post(url, **dt, headers=self._prehook(data))
27
34
  return await self._proc(resp)
28
35
 
29
- async def _delete(self, url: str, params: dict = None, **kwargs):
30
- resp: ClientResponse = await self.session.delete(url, params=params, **kwargs)
36
+ async def _delete(self, url: str, params: dict = None):
37
+ resp: ClientResponse = await self.session.delete(url, params=params, headers=self._prehook(params))
31
38
  return await self._proc(resp)
32
39
 
33
- async def _proc(self, resp: ClientResponse, data=None) -> dict | str:
40
+ async def _proc(self, resp: ClientResponse, data_key: str = None, body=None) -> dict | str:
34
41
  if not str(resp.status).startswith("2"):
35
42
  if resp.status == 404:
36
43
  raise HttpNotFound()
37
44
  raise HttpProcessingError(code=resp.status, message=await resp.text())
38
45
  if resp.content_type.endswith("/json"):
39
- return await resp.json()
46
+ data = await resp.json()
47
+ if data_key:
48
+ if res := data.get(data_key):
49
+ return res
50
+ raise HttpProcessingError()
51
+ return data
40
52
  return await resp.text()
@@ -31,9 +31,12 @@ class Client:
31
31
  return self.__resp()
32
32
 
33
33
  def __resp(self) -> dict:
34
- resp = self.cn.getresponse()
35
- body = brotli.decompress(resp.read())
36
- return loads(body)
34
+ resp = self.cn.getresponse().read()
35
+ try:
36
+ body = loads(resp)
37
+ except Exception:
38
+ body = brotli.decompress(resp)
39
+ return body
37
40
 
38
41
  def close(self):
39
42
  self.cn.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xn-client
3
- Version: 0.0.3.dev2
3
+ Version: 0.0.3.dev4
4
4
  Author-email: Mike Artemiev <mixartemev@gmail.com>
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/XyncNet/x-client
File without changes
File without changes
File without changes