soia-client 1.0.16__py3-none-any.whl → 1.0.18__py3-none-any.whl

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.
soia/_impl/service.py CHANGED
@@ -1,19 +1,47 @@
1
1
  import inspect
2
2
  import json
3
+ from collections.abc import Mapping
3
4
  from dataclasses import dataclass
4
- from typing import Any, Callable, Generic, Literal, Protocol, TypeAlias, Union, cast
5
+ from typing import Any, Callable, Generic, Literal, TypeAlias, Union, cast
5
6
 
6
7
  from soia._impl.method import Method, Request, Response
7
8
 
8
-
9
- class RequestHeaders(Protocol):
10
- def __getitem__(self, key: str, /) -> str | None: ...
9
+ RequestHeaders: TypeAlias = Mapping[str, str]
11
10
 
12
11
 
13
12
  ResponseHeaders: TypeAlias = dict[str, str]
14
13
 
15
14
 
16
15
  class Service:
16
+ """Wraps around the implementation of a soia service on the server side.
17
+
18
+ Usage: call '.add_method()' to register method implementations, then call
19
+ '.handle_request()' from the function called by your web framework when an
20
+ HTTP request is sent to your service's URL.
21
+
22
+ Example with Flask:
23
+
24
+ from flask import Response, request
25
+
26
+ soia_service = soia.Service()
27
+ soia_service.add_method(...)
28
+ soia_service.add_method(...)
29
+
30
+ @app.route("/myapi", methods=["GET", "POST"])
31
+ def myapi():
32
+ if request.method == "POST":
33
+ req_body = request.get_data(as_text=True)
34
+ else:
35
+ req_body = urllib.parse.unquote(request.query_string.decode("utf-8"))
36
+ req_headers = dict(request.headers)
37
+ raw_response = soia_service.handle_request(req_body, req_headers, {})
38
+ return Response(
39
+ raw_response.data,
40
+ status=raw_response.status_code,
41
+ content_type=raw_response.content_type,
42
+ )
43
+ """
44
+
17
45
  _number_to_method_impl: dict[int, "_MethodImpl"]
18
46
 
19
47
  def __init__(self):
@@ -1,5 +1,6 @@
1
1
  import http.client
2
- from typing import Final, Mapping
2
+ import re
3
+ from typing import Any, Final, Mapping
3
4
  from urllib.parse import urlparse
4
5
 
5
6
  from soia._impl.method import Method, Request, Response
@@ -10,8 +11,8 @@ class ServiceClient:
10
11
  _host: Final[str] # May include the port
11
12
  _path: Final[str]
12
13
 
13
- def __init__(self, service_url: str):
14
- parsed_url = urlparse(service_url)
14
+ def __init__(self, url: str):
15
+ parsed_url = urlparse(url)
15
16
  if parsed_url.query:
16
17
  raise ValueError("Service URL must not contain a query string")
17
18
  scheme = parsed_url.scheme
@@ -26,7 +27,12 @@ class ServiceClient:
26
27
  method: Method[Request, Response],
27
28
  request: Request,
28
29
  headers: Mapping[str, str] = {},
30
+ *,
31
+ res_headers: list[tuple[str, str]] | None = None,
32
+ timeout_secs: float | None = None,
29
33
  ) -> Response:
34
+ """Invokes the given method on the remote server through an RPC."""
35
+
30
36
  request_json = method.request_serializer.to_json_code(request)
31
37
  body = ":".join(
32
38
  [
@@ -41,10 +47,13 @@ class ServiceClient:
41
47
  "Content-Type": "text/plain; charset=utf-8",
42
48
  "Content-Length": str(len(body)),
43
49
  }
50
+ connection_options: dict[str, Any] = {}
51
+ if timeout_secs is not None:
52
+ connection_options["timeout"] = timeout_secs
44
53
  if self._scheme == "https":
45
- conn = http.client.HTTPSConnection(self._host)
54
+ conn = http.client.HTTPSConnection(self._host, **connection_options)
46
55
  else:
47
- conn = http.client.HTTPConnection(self._host)
56
+ conn = http.client.HTTPConnection(self._host, **connection_options)
48
57
  try:
49
58
  conn.request(
50
59
  "POST",
@@ -53,15 +62,18 @@ class ServiceClient:
53
62
  headers=headers,
54
63
  )
55
64
  response = conn.getresponse()
65
+ if res_headers is not None:
66
+ res_headers.clear()
67
+ res_headers.extend(response.getheaders())
56
68
  status_code = response.status
57
- content_type = response.getheader("Content-Type")
69
+ content_type = response.getheader("Content-Type") or ""
58
70
  response_data = response.read().decode("utf-8", errors="ignore")
59
71
  finally:
60
72
  conn.close()
61
73
  if status_code in range(200, 300):
62
74
  return method.response_serializer.from_json_code(response_data)
63
75
  else:
64
- message = f"HTTP response status {status_code}"
65
- if content_type == "text/plain":
76
+ message = f"HTTP status {status_code}"
77
+ if re.match(r"text/plain\b", content_type):
66
78
  message = f"{message}: {response_data}"
67
79
  raise RuntimeError(message)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: soia-client
3
- Version: 1.0.16
3
+ Version: 1.0.18
4
4
  Author-email: Tyler Fibonacci <gepheum@gmail.com>
5
5
  License: MIT License
6
6
 
@@ -14,13 +14,13 @@ soia/_impl/primitives.py,sha256=Xk26Fv4oQG2oXd3tS_2sAnJYQdXYX9nva09713AcJvs,8940
14
14
  soia/_impl/repr.py,sha256=7WX0bEAVENTjlyZIcbT8TcJylS7IRIyafGCmqaIMxFM,1413
15
15
  soia/_impl/serializer.py,sha256=28IwkjtUnLpbnPQfVNfJXkApCK4JhXHwLkC5MVhF8xo,3529
16
16
  soia/_impl/serializers.py,sha256=IL9jHHMo11pgrL1-crarOEElvTyV5YM6FTcgumjW6IU,2564
17
- soia/_impl/service.py,sha256=Z0HZlitdEyosbrTXK8OQMnqvahywUJPi7GRTnyRE8B4,6004
18
- soia/_impl/service_client.py,sha256=5gS7tUt0LJdKGxJsjNU6-aQaZk4dRg1alT83oJfqIZo,2218
17
+ soia/_impl/service.py,sha256=z8Zj2s8AYAz-Bf1Dm7PRdNvh5FbGpgj5hm2velMBjmc,7052
18
+ soia/_impl/service_client.py,sha256=qDntwRyXfLsmXl4ELfOkh-fgv553nrGy72K0JghLM80,2734
19
19
  soia/_impl/structs.py,sha256=YTc3Ykj2TxPquar2XsP2DhFfkfIoELXOveyd8yTqN90,26545
20
20
  soia/_impl/timestamp.py,sha256=lXBNH8mPmzflkNjSKZSBl2XS-ot9N8N92B_zGO2SMtU,4078
21
21
  soia/_impl/type_adapter.py,sha256=RyIyh4Fnt9rMy0HRzC-a2v2JAdZsV9FBzoGEUVygVRE,2101
22
- soia_client-1.0.16.dist-info/licenses/LICENSE,sha256=SaAftKkX6hfSOiPdENQPS70tifH3PDHgazq8eK2Pwfw,1064
23
- soia_client-1.0.16.dist-info/METADATA,sha256=mAhZErll3dmFwg-rMhQWEwd-EWFi9yf2rvCpBjS3a14,1667
24
- soia_client-1.0.16.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
25
- soia_client-1.0.16.dist-info/top_level.txt,sha256=lsYG9JrvauFe1oIV5zvnwsS9hsx3ztwfK_937op9mxc,5
26
- soia_client-1.0.16.dist-info/RECORD,,
22
+ soia_client-1.0.18.dist-info/licenses/LICENSE,sha256=SaAftKkX6hfSOiPdENQPS70tifH3PDHgazq8eK2Pwfw,1064
23
+ soia_client-1.0.18.dist-info/METADATA,sha256=2KPUoHzmxfNNNQ6Dnj4XYPbu8nHZpQzs2Dbt17GXGq0,1667
24
+ soia_client-1.0.18.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
25
+ soia_client-1.0.18.dist-info/top_level.txt,sha256=lsYG9JrvauFe1oIV5zvnwsS9hsx3ztwfK_937op9mxc,5
26
+ soia_client-1.0.18.dist-info/RECORD,,