polyapi-python 0.3.14.dev4__tar.gz → 0.3.16__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 (52) hide show
  1. {polyapi_python-0.3.14.dev4/polyapi_python.egg-info → polyapi_python-0.3.16}/PKG-INFO +1 -1
  2. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/http_client.py +16 -6
  3. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/schema.py +23 -4
  4. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16/polyapi_python.egg-info}/PKG-INFO +1 -1
  5. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/pyproject.toml +1 -1
  6. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_async_proof.py +86 -0
  7. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_schema.py +8 -1
  8. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/LICENSE +0 -0
  9. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/README.md +0 -0
  10. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/__init__.py +0 -0
  11. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/__main__.py +0 -0
  12. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/api.py +0 -0
  13. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/auth.py +0 -0
  14. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/cli.py +0 -0
  15. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/cli_constants.py +0 -0
  16. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/client.py +0 -0
  17. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/config.py +0 -0
  18. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/constants.py +0 -0
  19. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/deployables.py +0 -0
  20. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/error_handler.py +0 -0
  21. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/exceptions.py +0 -0
  22. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/execute.py +0 -0
  23. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/function_cli.py +0 -0
  24. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/generate.py +0 -0
  25. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/parser.py +0 -0
  26. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/poly_schemas.py +0 -0
  27. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/poly_tables.py +0 -0
  28. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/prepare.py +0 -0
  29. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/py.typed +0 -0
  30. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/rendered_spec.py +0 -0
  31. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/server.py +0 -0
  32. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/sync.py +0 -0
  33. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/typedefs.py +0 -0
  34. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/utils.py +0 -0
  35. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/variables.py +0 -0
  36. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi/webhook.py +0 -0
  37. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi_python.egg-info/SOURCES.txt +0 -0
  38. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi_python.egg-info/dependency_links.txt +0 -0
  39. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi_python.egg-info/requires.txt +0 -0
  40. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/polyapi_python.egg-info/top_level.txt +0 -0
  41. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/setup.cfg +0 -0
  42. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_api.py +0 -0
  43. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_auth.py +0 -0
  44. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_deployables.py +0 -0
  45. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_generate.py +0 -0
  46. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_parser.py +0 -0
  47. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_poly_custom.py +0 -0
  48. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_rendered_spec.py +0 -0
  49. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_server.py +0 -0
  50. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_tabi.py +0 -0
  51. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_utils.py +0 -0
  52. {polyapi_python-0.3.14.dev4 → polyapi_python-0.3.16}/tests/test_variables.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.14.dev4
3
+ Version: 0.3.16
4
4
  Summary: The Python Client for PolyAPI, the IPaaS by Developers for Developers
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -3,6 +3,7 @@ import httpx
3
3
 
4
4
  _sync_client: httpx.Client | None = None
5
5
  _async_client: httpx.AsyncClient | None = None
6
+ _async_client_loop: asyncio.AbstractEventLoop | None = None
6
7
 
7
8
 
8
9
  def _get_sync_client() -> httpx.Client:
@@ -13,9 +14,11 @@ def _get_sync_client() -> httpx.Client:
13
14
 
14
15
 
15
16
  def _get_async_client() -> httpx.AsyncClient:
16
- global _async_client
17
- if _async_client is None:
17
+ global _async_client, _async_client_loop
18
+ current_loop = asyncio.get_running_loop()
19
+ if _async_client is None or _async_client_loop is not current_loop:
18
20
  _async_client = httpx.AsyncClient(timeout=None)
21
+ _async_client_loop = current_loop
19
22
  return _async_client
20
23
 
21
24
 
@@ -66,8 +69,15 @@ def close():
66
69
  _sync_client = None
67
70
 
68
71
  async def close_async():
69
- global _sync_client, _async_client
72
+ global _sync_client, _async_client, _async_client_loop
70
73
  close()
71
- if _async_client is not None:
72
- await _async_client.aclose()
73
- _async_client = None
74
+ client = _async_client
75
+ client_loop = _async_client_loop
76
+ _async_client = None
77
+ _async_client_loop = None
78
+ if client is None:
79
+ return
80
+
81
+ current_loop = asyncio.get_running_loop()
82
+ if client_loop is current_loop:
83
+ await client.aclose()
@@ -101,8 +101,11 @@ def generate_schema_types(input_data: Dict, root=None):
101
101
  return output
102
102
 
103
103
 
104
- # Regex to match everything between "# example: {\n" and "^}$"
105
- MALFORMED_EXAMPLES_PATTERN = re.compile(r"# example: \{\n.*?^\}$", flags=re.DOTALL | re.MULTILINE)
104
+ # Matches commented example headers emitted by jsonschema-gentypes before a raw
105
+ # multiline JSON object/array body that is not commented out.
106
+ MALFORMED_EXAMPLE_HEADER_PATTERN = re.compile(
107
+ r"^\s*#\s*(?:\|\s*)?example:\s*([\[{])\s*$"
108
+ )
106
109
 
107
110
  # Regex to fix invalid escape sequences in docstrings
108
111
  INVALID_ESCAPE_PATTERNS = [
@@ -117,8 +120,24 @@ def clean_malformed_examples(example: str) -> str:
117
120
  """ there is a bug in the `jsonschmea_gentypes` library where if an example from a jsonchema is an object,
118
121
  it will break the code because the object won't be properly commented out. Also fixes invalid escape sequences.
119
122
  """
120
- # Remove malformed examples
121
- cleaned_example = MALFORMED_EXAMPLES_PATTERN.sub("", example)
123
+ cleaned_lines = []
124
+ balance = 0
125
+ skipping_example = False
126
+
127
+ for line in example.splitlines(keepends=True):
128
+ if not skipping_example:
129
+ if MALFORMED_EXAMPLE_HEADER_PATTERN.match(line):
130
+ skipping_example = True
131
+ balance = line.count("{") + line.count("[") - line.count("}") - line.count("]")
132
+ continue
133
+ cleaned_lines.append(line)
134
+ continue
135
+
136
+ balance += line.count("{") + line.count("[") - line.count("}") - line.count("]")
137
+ if balance <= 0:
138
+ skipping_example = False
139
+
140
+ cleaned_example = "".join(cleaned_lines)
122
141
 
123
142
  # Fix invalid escape sequences in docstrings
124
143
  for pattern, replacement in INVALID_ESCAPE_PATTERNS:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.14.dev4
3
+ Version: 0.3.16
4
4
  Summary: The Python Client for PolyAPI, the IPaaS by Developers for Developers
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "polyapi-python"
7
- version = "0.3.14.dev4"
7
+ version = "0.3.16"
8
8
  description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
9
9
  authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
10
10
  dependencies = [
@@ -56,10 +56,12 @@ class TestHttpClientPairing:
56
56
  # Reset singletons so each test starts fresh
57
57
  http_client._sync_client = None
58
58
  http_client._async_client = None
59
+ http_client._async_client_loop = None
59
60
 
60
61
  def teardown_method(self):
61
62
  http_client._sync_client = None
62
63
  http_client._async_client = None
64
+ http_client._async_client_loop = None
63
65
 
64
66
  @patch.object(httpx.Client, "post", return_value=_fake_response())
65
67
  def test_sync_post_uses_sync_client(self, mock_post):
@@ -79,6 +81,90 @@ class TestHttpClientPairing:
79
81
  mock_post.assert_called_once()
80
82
  assert resp.status_code == 200
81
83
  assert http_client._async_client is not None
84
+ assert http_client._async_client_loop is not None
85
+
86
+ def test_async_post_reuses_client_within_same_loop(self):
87
+ first_client = MagicMock()
88
+ first_client.post = AsyncMock(return_value=_fake_response())
89
+
90
+ with patch("polyapi.http_client.httpx.AsyncClient", return_value=first_client) as mock_async_client:
91
+ async def _run():
92
+ first_response = await http_client.async_post("https://example.com/first", json={})
93
+ second_response = await http_client.async_post("https://example.com/second", json={})
94
+ return first_response, second_response, asyncio.get_running_loop()
95
+
96
+ first_response, second_response, current_loop = asyncio.run(_run())
97
+
98
+ assert first_response.status_code == 200
99
+ assert second_response.status_code == 200
100
+ assert mock_async_client.call_count == 1
101
+ assert first_client.post.await_count == 2
102
+ assert http_client._async_client is first_client
103
+ assert http_client._async_client_loop is current_loop
104
+
105
+ def test_async_post_recreates_client_after_loop_change(self):
106
+ first_client = MagicMock()
107
+ first_client.post = AsyncMock(return_value=_fake_response())
108
+ second_client = MagicMock()
109
+ second_client.post = AsyncMock(return_value=_fake_response())
110
+
111
+ with patch(
112
+ "polyapi.http_client.httpx.AsyncClient",
113
+ side_effect=[first_client, second_client],
114
+ ) as mock_async_client:
115
+ async def _run_once(url: str):
116
+ response = await http_client.async_post(url, json={})
117
+ return response, http_client._async_client, asyncio.get_running_loop()
118
+
119
+ first_response, first_cached_client, first_loop = asyncio.run(_run_once("https://example.com/first"))
120
+ second_response, second_cached_client, second_loop = asyncio.run(_run_once("https://example.com/second"))
121
+
122
+ assert first_response.status_code == 200
123
+ assert second_response.status_code == 200
124
+ assert mock_async_client.call_count == 2
125
+ assert first_client.post.await_count == 1
126
+ assert second_client.post.await_count == 1
127
+ assert first_cached_client is first_client
128
+ assert second_cached_client is second_client
129
+ assert first_loop is not second_loop
130
+ assert http_client._async_client is second_client
131
+ assert http_client._async_client_loop is second_loop
132
+
133
+ def test_close_async_clears_cached_client_for_current_loop(self):
134
+ async def _run():
135
+ cached_client = MagicMock()
136
+ cached_client.aclose = AsyncMock()
137
+ http_client._async_client = cached_client
138
+ http_client._async_client_loop = asyncio.get_running_loop()
139
+
140
+ await http_client.close_async()
141
+
142
+ return cached_client
143
+
144
+ cached_client = asyncio.run(_run())
145
+
146
+ cached_client.aclose.assert_awaited_once()
147
+ assert http_client._async_client is None
148
+ assert http_client._async_client_loop is None
149
+
150
+ def test_close_async_drops_stale_client_without_cross_loop_close(self):
151
+ stale_client = MagicMock()
152
+ stale_client.aclose = AsyncMock()
153
+
154
+ async def _seed_stale_client():
155
+ http_client._async_client = stale_client
156
+ http_client._async_client_loop = asyncio.get_running_loop()
157
+
158
+ asyncio.run(_seed_stale_client())
159
+
160
+ async def _close_on_new_loop():
161
+ await http_client.close_async()
162
+
163
+ asyncio.run(_close_on_new_loop())
164
+
165
+ stale_client.aclose.assert_not_awaited()
166
+ assert http_client._async_client is None
167
+ assert http_client._async_client_loop is None
82
168
 
83
169
  @patch.object(httpx.Client, "get", return_value=_fake_response())
84
170
  def test_sync_get(self, mock_get):
@@ -19,6 +19,7 @@ CHARACTER_SCHEMA = {
19
19
  }
20
20
 
21
21
  APALEO_MALFORMED_EXAMPLE = 'from typing import List, TypedDict, Union\nfrom typing_extensions import Required\n\n\n# Body.\n# \n# example: {\n "from": "2024-04-21",\n "to": "2024-04-24",\n "grossDailyRate": {\n "amount": 160.0,\n "currency": "EUR"\n },\n "timeSlices": [\n {\n "blockedUnits": 3\n },\n {\n "blockedUnits": 0\n },\n {\n "blockedUnits": 7\n }\n ]\n}\n# x-readme-ref-name: ReplaceBlockModel\nBody = TypedDict(\'Body\', {\n # Start date and time from which the inventory will be blockedSpecify either a pure date or a date and time (without fractional second part) in UTC or with UTC offset as defined in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO8601:2004</a>\n # \n # Required property\n \'from\': Required[str],\n # End date and time until which the inventory will be blocked. Cannot be more than 5 years after the start date.Specify either a pure date or a date and time (without fractional second part) in UTC or with UTC offset as defined in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO8601:2004</a>\n # \n # Required property\n \'to\': Required[str],\n # x-readme-ref-name: MonetaryValueModel\n # \n # Required property\n \'grossDailyRate\': Required["_BodygrossDailyRate"],\n # The list of time slices\n # \n # Required property\n \'timeSlices\': Required[List["_BodytimeSlicesitem"]],\n}, total=False)\n\n\nclass _BodygrossDailyRate(TypedDict, total=False):\n """ x-readme-ref-name: MonetaryValueModel """\n\n amount: Required[Union[int, float]]\n """\n format: double\n\n Required property\n """\n\n currency: Required[str]\n """ Required property """\n\n\n\nclass _BodytimeSlicesitem(TypedDict, total=False):\n """ x-readme-ref-name: CreateBlockTimeSliceModel """\n\n blockedUnits: Required[Union[int, float]]\n """\n Number of units blocked for the time slice\n\n format: int32\n\n Required property\n """\n\n'
22
+ APALEO_MALFORMED_PIPE_EXAMPLE = APALEO_MALFORMED_EXAMPLE.replace("# example: {", "# | example: {")
22
23
 
23
24
 
24
25
  class T(unittest.TestCase):
@@ -32,9 +33,15 @@ class T(unittest.TestCase):
32
33
  def test_clean_malformed_examples(self):
33
34
  output = clean_malformed_examples(APALEO_MALFORMED_EXAMPLE)
34
35
  self.assertNotIn("# example: {", output)
36
+ self.assertNotIn(' "from": "2024-04-21",', output)
37
+
38
+ def test_clean_malformed_examples_pipe_style(self):
39
+ output = clean_malformed_examples(APALEO_MALFORMED_PIPE_EXAMPLE)
40
+ self.assertNotIn("# | example: {", output)
41
+ self.assertNotIn(' "from": "2024-04-21",', output)
35
42
 
36
43
  def test_character_encoding(self):
37
44
  output = generate_schema_types(CHARACTER_SCHEMA, "Dict")
38
45
  expected = 'from typing import TypedDict\n\n\nclass Dict(TypedDict, total=False):\n CHARACTER_SCHEMA_NAME: str\n """ This is — “bad”, right? """\n\n'
39
46
  self.assertEqual(output, expected)
40
-
47
+