hexway-hive-api 0.2.2__tar.gz → 0.2.3__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.
Files changed (30) hide show
  1. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/PKG-INFO +1 -1
  2. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/clients/async_rest_client.py +34 -9
  3. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api.egg-info/PKG-INFO +1 -1
  4. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/pyproject.toml +1 -1
  5. hexway_hive_api-0.2.3/tests/test_async_rest_client.py +245 -0
  6. hexway_hive_api-0.2.2/tests/test_async_rest_client.py +0 -111
  7. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/LICENSE +0 -0
  8. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/README.md +0 -0
  9. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/__init__.py +0 -0
  10. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/clients/__init__.py +0 -0
  11. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/clients/rest_client.py +0 -0
  12. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/__init__.py +0 -0
  13. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/enums.py +0 -0
  14. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/error_handler.py +0 -0
  15. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/exceptions.py +0 -0
  16. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/http_client/__init__.py +0 -0
  17. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/http_client/async_http_client.py +0 -0
  18. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/http_client/exceptions.py +0 -0
  19. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/http_client/http_client.py +0 -0
  20. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/models/__init__.py +0 -0
  21. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/models/issue.py +0 -0
  22. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api/rest/models/project.py +0 -0
  23. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api.egg-info/SOURCES.txt +0 -0
  24. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api.egg-info/dependency_links.txt +0 -0
  25. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api.egg-info/requires.txt +0 -0
  26. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/hexway_hive_api.egg-info/top_level.txt +0 -0
  27. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/setup.cfg +0 -0
  28. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/tests/test_async_http_client.py +0 -0
  29. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/tests/test_issue.py +0 -0
  30. {hexway_hive_api-0.2.2 → hexway_hive_api-0.2.3}/tests/test_project.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexway-hive-api
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Small wrapper for HexWay Hive API
5
5
  Author-email: "@Cur1iosity" <Cur1iosity@protonmail.com>
6
6
  Project-URL: Homepage, https://github.com/Cur1iosity/hexway-hive-api
@@ -2,6 +2,8 @@
2
2
 
3
3
  import json
4
4
  import ssl
5
+
6
+ import aiohttp
5
7
  from contextlib import asynccontextmanager
6
8
  from typing import Optional, MutableMapping, Union, Self, AsyncGenerator
7
9
  from uuid import UUID
@@ -90,6 +92,13 @@ class AsyncRestClient:
90
92
  context = self._make_ssl_context(cert)
91
93
  self.http_client.update_params(ssl=context)
92
94
 
95
+ if self.http_client.session.closed:
96
+ proxies = self.http_client.proxies
97
+ ssl_context = self.http_client.ssl
98
+ await self.http_client.close()
99
+ self.http_client = AsyncHTTPClient(ssl=ssl_context)
100
+ self.http_client.proxies = proxies
101
+
93
102
  self.server = server or self.server
94
103
  self.api_url = api_url or self.api_url or self.make_api_url_from(self.server)
95
104
 
@@ -99,10 +108,18 @@ class AsyncRestClient:
99
108
  if '@' not in username:
100
109
  username = f'{username}@ro.ot'
101
110
 
102
- response = await self.http_client.session.post(f"{self.api_url}/session", json={
103
- 'userLogin': username,
104
- 'userPassword': password,
105
- })
111
+ try:
112
+ response = await self.http_client.session.post(
113
+ f"{self.api_url}/session",
114
+ json={
115
+ 'userLogin': username,
116
+ 'userPassword': password,
117
+ },
118
+ ssl=self.http_client.ssl,
119
+ )
120
+ except aiohttp.ClientError as e:
121
+ await self.http_client.close()
122
+ raise exceptions.RestConnectionError(f'Failed to connect: {e}') from e
106
123
 
107
124
  cookie = response.cookies.get('BSESSIONID')
108
125
  if not cookie:
@@ -113,11 +130,19 @@ class AsyncRestClient:
113
130
 
114
131
  async def disconnect(self) -> bool:
115
132
  """Close connection and clean up client session."""
116
-
117
- await self.http_client.session.delete(f"{self.api_url}/session")
118
- self.state = ClientState.DISCONNECTED
119
- await self.http_client.clear_session()
120
- await self.http_client.close()
133
+ try:
134
+ await self.http_client.session.delete(f"{self.api_url}/session")
135
+ except aiohttp.ClientError:
136
+ # connection may already be closed by the server
137
+ pass
138
+ finally:
139
+ self.state = ClientState.DISCONNECTED
140
+ await self.http_client.clear_session()
141
+ proxies = self.http_client.proxies
142
+ ssl_context = self.http_client.ssl
143
+ await self.http_client.close()
144
+ self.http_client.proxies = proxies
145
+ self.http_client.ssl = ssl_context
121
146
  return True
122
147
 
123
148
  @asynccontextmanager
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexway-hive-api
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Small wrapper for HexWay Hive API
5
5
  Author-email: "@Cur1iosity" <Cur1iosity@protonmail.com>
6
6
  Project-URL: Homepage, https://github.com/Cur1iosity/hexway-hive-api
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "hexway-hive-api"
7
- version = "0.2.2"
7
+ version = "0.2.3"
8
8
  description = "Small wrapper for HexWay Hive API"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -0,0 +1,245 @@
1
+ from hexway_hive_api.clients.async_rest_client import AsyncRestClient
2
+ from hexway_hive_api.rest import exceptions
3
+ import aiohttp
4
+ import pytest
5
+
6
+
7
+ def test_make_api_url_from() -> None:
8
+ """Ensure API URL is built correctly from server string."""
9
+ url = AsyncRestClient.make_api_url_from('https://hive.local')
10
+ assert url == 'https://hive.local:443/api'
11
+
12
+
13
+ def test_proxies_property() -> None:
14
+ """Proxy configuration should be stored inside HTTP client."""
15
+ async def run() -> None:
16
+ client = AsyncRestClient()
17
+ client.proxies = {"http": "http://proxy"}
18
+ assert client.proxies.get("http") == "http://proxy"
19
+ await client.http_client.clear_session()
20
+
21
+ import asyncio
22
+ asyncio.run(run())
23
+
24
+
25
+ def test_connect_handles_aiohttp_error() -> None:
26
+ """Client should close session and raise ``RestConnectionError`` on aiohttp failure."""
27
+
28
+ class DummySession:
29
+ def __init__(self) -> None:
30
+ self.headers = {}
31
+ self.closed = False
32
+
33
+ async def post(self, *args, **kwargs):
34
+ raise aiohttp.ClientConnectionError("boom")
35
+
36
+ async def close(self):
37
+ self.closed = True
38
+
39
+ async def run() -> None:
40
+ client = AsyncRestClient()
41
+ old_session = client.http_client.session
42
+ dummy = DummySession()
43
+ client.http_client.session = dummy
44
+ await old_session.close()
45
+
46
+ with pytest.raises(exceptions.RestConnectionError):
47
+ await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
48
+
49
+ assert dummy.closed is True
50
+
51
+ import asyncio
52
+ asyncio.run(run())
53
+
54
+
55
+ def test_disconnect_closes_session() -> None:
56
+ """Client session should be closed after disconnect."""
57
+
58
+ class DummyResponse:
59
+ def __init__(self) -> None:
60
+ self.cookies = {"BSESSIONID": "cookie"}
61
+
62
+ async def json(self):
63
+ return {}
64
+
65
+ class DummySession:
66
+ def __init__(self) -> None:
67
+ self.headers = {}
68
+ self.closed = False
69
+
70
+ async def post(self, *args, **kwargs):
71
+ if self.closed:
72
+ raise RuntimeError("Session is closed")
73
+ return DummyResponse()
74
+
75
+ async def delete(self, *args, **kwargs):
76
+ if self.closed:
77
+ raise RuntimeError("Session is closed")
78
+ return DummyResponse()
79
+
80
+ async def close(self):
81
+ self.closed = True
82
+
83
+ async def run() -> None:
84
+ client = AsyncRestClient()
85
+ old_session = client.http_client.session
86
+ dummy = DummySession()
87
+ client.http_client.session = dummy
88
+ await old_session.close()
89
+
90
+ await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
91
+ await client.disconnect()
92
+
93
+ assert dummy.closed is True
94
+ assert client.http_client.session is dummy
95
+ assert client.http_client.session.closed is True
96
+
97
+ import asyncio
98
+ asyncio.run(run())
99
+
100
+
101
+ def test_disconnect_handles_server_error() -> None:
102
+ """Disconnect should recreate session even if server is already disconnected."""
103
+
104
+ class DummyResponse:
105
+ def __init__(self) -> None:
106
+ self.cookies = {"BSESSIONID": "cookie"}
107
+
108
+ async def json(self):
109
+ return {}
110
+
111
+ class DummySession:
112
+ def __init__(self) -> None:
113
+ self.headers = {}
114
+ self.closed = False
115
+
116
+ async def post(self, *args, **kwargs):
117
+ if self.closed:
118
+ raise RuntimeError("Session is closed")
119
+ return DummyResponse()
120
+
121
+ async def delete(self, *args, **kwargs):
122
+ raise aiohttp.ServerDisconnectedError()
123
+
124
+ async def close(self):
125
+ self.closed = True
126
+
127
+ async def run() -> None:
128
+ client = AsyncRestClient()
129
+ old_session = client.http_client.session
130
+ dummy = DummySession()
131
+ client.http_client.session = dummy
132
+ await old_session.close()
133
+
134
+ await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
135
+ await client.disconnect()
136
+
137
+ assert dummy.closed is True
138
+ assert client.http_client.session is dummy
139
+ assert client.http_client.session.closed is True
140
+
141
+ import asyncio
142
+ asyncio.run(run())
143
+
144
+
145
+ def test_connect_after_disconnect() -> None:
146
+ """Client should be able to reconnect after disconnect."""
147
+
148
+ class DummyResponse:
149
+ def __init__(self) -> None:
150
+ self.cookies = {"BSESSIONID": "cookie"}
151
+
152
+ async def json(self):
153
+ return {}
154
+
155
+ class DummySession:
156
+ def __init__(self) -> None:
157
+ self.headers = {}
158
+ self.closed = False
159
+
160
+ async def post(self, *args, **kwargs):
161
+ if self.closed:
162
+ raise RuntimeError("Session is closed")
163
+ return DummyResponse()
164
+
165
+ async def delete(self, *args, **kwargs):
166
+ if self.closed:
167
+ raise RuntimeError("Session is closed")
168
+ return DummyResponse()
169
+
170
+ async def close(self):
171
+ self.closed = True
172
+
173
+ async def run() -> None:
174
+ client = AsyncRestClient()
175
+ old_session = client.http_client.session
176
+ dummy1 = DummySession()
177
+ client.http_client.session = dummy1
178
+ await old_session.close()
179
+
180
+ await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
181
+ await client.disconnect()
182
+
183
+ assert dummy1.closed is True
184
+ assert client.http_client.session is dummy1
185
+ assert client.http_client.session.closed is True
186
+
187
+ dummy2 = DummySession()
188
+ client.http_client.session = dummy2
189
+
190
+ await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
191
+
192
+ assert dummy2.closed is False
193
+ assert client.http_client.session.headers.get("Cookie") == "BSESSIONID=cookie"
194
+ await client.http_client.session.close()
195
+
196
+ import asyncio
197
+ asyncio.run(run())
198
+
199
+
200
+ def test_connect_uses_ssl_context(monkeypatch) -> None:
201
+ """SSL context from ``cert`` parameter should be passed to requests."""
202
+ import ssl
203
+
204
+ async def run() -> None:
205
+ # avoid loading real certificate
206
+ monkeypatch.setattr(
207
+ AsyncRestClient,
208
+ "_make_ssl_context",
209
+ staticmethod(lambda cert: ssl.create_default_context()),
210
+ )
211
+
212
+ class DummyResponse:
213
+ def __init__(self) -> None:
214
+ self.cookies = {"BSESSIONID": "cookie"}
215
+
216
+ class DummySession:
217
+ def __init__(self) -> None:
218
+ self.headers = {}
219
+ self.captured = None
220
+ self.closed = False
221
+
222
+ async def post(self, *args, **kwargs):
223
+ self.captured = kwargs.get("ssl")
224
+ return DummyResponse()
225
+
226
+ async def close(self):
227
+ self.closed = True
228
+
229
+ client = AsyncRestClient(cert="path/to/cert.pem")
230
+ old_session = client.http_client.session
231
+ client.http_client.session = DummySession()
232
+ await old_session.close()
233
+
234
+ await client.connect(
235
+ server="https://hive.local",
236
+ api_url="https://hive.local/api",
237
+ username="u",
238
+ password="p",
239
+ )
240
+
241
+ assert isinstance(client.http_client.session.captured, ssl.SSLContext)
242
+ await client.http_client.session.close()
243
+
244
+ import asyncio
245
+ asyncio.run(run())
@@ -1,111 +0,0 @@
1
- from hexway_hive_api.clients.async_rest_client import AsyncRestClient
2
-
3
-
4
- def test_make_api_url_from() -> None:
5
- """Ensure API URL is built correctly from server string."""
6
- url = AsyncRestClient.make_api_url_from('https://hive.local')
7
- assert url == 'https://hive.local:443/api'
8
-
9
-
10
- def test_proxies_property() -> None:
11
- """Proxy configuration should be stored inside HTTP client."""
12
- async def run() -> None:
13
- client = AsyncRestClient()
14
- client.proxies = {"http": "http://proxy"}
15
- assert client.proxies.get("http") == "http://proxy"
16
- await client.http_client.clear_session()
17
-
18
- import asyncio
19
- asyncio.run(run())
20
-
21
-
22
- def test_disconnect_closes_session() -> None:
23
- """Client session should be closed after disconnect."""
24
-
25
- class DummyResponse:
26
- def __init__(self) -> None:
27
- self.cookies = {"BSESSIONID": "cookie"}
28
-
29
- async def json(self):
30
- return {}
31
-
32
- class DummySession:
33
- def __init__(self) -> None:
34
- self.headers = {}
35
- self.closed = False
36
-
37
- async def post(self, *args, **kwargs):
38
- return DummyResponse()
39
-
40
- async def delete(self, *args, **kwargs):
41
- return DummyResponse()
42
-
43
- async def close(self):
44
- self.closed = True
45
-
46
- async def run() -> None:
47
- client = AsyncRestClient()
48
- old_session = client.http_client.session
49
- client.http_client.session = DummySession()
50
- await old_session.close()
51
-
52
- await client.connect(server="http://test", api_url="http://test/api", username="u", password="p")
53
- await client.disconnect()
54
-
55
- assert client.http_client.session.closed is True
56
-
57
- import asyncio
58
- asyncio.run(run())
59
-
60
-
61
- def test_connect_uses_ssl_context(monkeypatch) -> None:
62
- """SSL context from ``cert`` parameter should be passed to requests."""
63
- import ssl
64
- async def run() -> None:
65
- captured = None
66
-
67
- # avoid loading real certificate
68
- monkeypatch.setattr(
69
- AsyncRestClient,
70
- "_make_ssl_context",
71
- staticmethod(lambda cert: ssl.create_default_context()),
72
- )
73
-
74
- client = AsyncRestClient(cert="path/to/cert.pem")
75
-
76
- async def dummy_request(self, method, url, **kwargs):
77
- nonlocal captured
78
- captured = kwargs.get("ssl")
79
- class DummyResponse:
80
- def __init__(self) -> None:
81
- self.cookies = {"BSESSIONID": "cookie"}
82
-
83
- async def json(self):
84
- return {}
85
-
86
- return DummyResponse()
87
-
88
- from types import MethodType
89
-
90
- monkeypatch.setattr(
91
- client.http_client.session,
92
- "request",
93
- MethodType(dummy_request, client.http_client.session),
94
- )
95
-
96
- async def dummy_post(self, url, **kwargs):
97
- return await self.request("POST", url, ssl=client.http_client.ssl, **kwargs)
98
-
99
- monkeypatch.setattr(
100
- client.http_client.session,
101
- "post",
102
- MethodType(dummy_post, client.http_client.session),
103
- )
104
-
105
- await client.connect(server="https://hive.local", api_url="https://hive.local/api", username="u", password="p")
106
-
107
- assert isinstance(captured, ssl.SSLContext)
108
- await client.http_client.session.close()
109
-
110
- import asyncio
111
- asyncio.run(run())
File without changes