tilebox-grpc 0.37.0__py3-none-any.whl → 0.37.1__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.
@@ -1,8 +1,8 @@
1
1
  from collections.abc import Callable, Sequence
2
2
  from typing import TypeVar
3
3
 
4
- from _tilebox.grpc.channel import CHANNEL_OPTIONS, ChannelInfo, add_metadata, parse_channel_info
5
- from grpc import ssl_channel_credentials
4
+ from _tilebox.grpc.channel import CHANNEL_OPTIONS, ChannelInfo, ChannelProtocol, add_metadata, parse_channel_info
5
+ from grpc import Compression, ssl_channel_credentials
6
6
  from grpc.aio import (
7
7
  Channel,
8
8
  ClientCallDetails,
@@ -34,11 +34,28 @@ def open_channel(url: str, auth_token: str | None = None) -> Channel:
34
34
 
35
35
 
36
36
  def _open_channel(channel_info: ChannelInfo, interceptors: Sequence[ClientInterceptor]) -> Channel:
37
- if channel_info.use_ssl:
38
- return secure_channel(
39
- channel_info.url_without_protocol, ssl_channel_credentials(), CHANNEL_OPTIONS, interceptors=interceptors
40
- )
41
- return insecure_channel(channel_info.url_without_protocol, CHANNEL_OPTIONS, interceptors=interceptors)
37
+ match channel_info.protocol:
38
+ case ChannelProtocol.HTTPS:
39
+ return secure_channel(
40
+ f"{channel_info.address}:{channel_info.port}",
41
+ ssl_channel_credentials(),
42
+ CHANNEL_OPTIONS,
43
+ compression=Compression.Gzip,
44
+ interceptors=interceptors,
45
+ )
46
+ case ChannelProtocol.HTTP:
47
+ return insecure_channel(
48
+ f"{channel_info.address}:{channel_info.port}",
49
+ CHANNEL_OPTIONS,
50
+ compression=Compression.NoCompression,
51
+ interceptors=interceptors,
52
+ )
53
+ case ChannelProtocol.UNIX:
54
+ return insecure_channel(
55
+ channel_info.address, CHANNEL_OPTIONS, compression=Compression.NoCompression, interceptors=interceptors
56
+ )
57
+ case _:
58
+ raise ValueError(f"Unsupported channel protocol: {channel_info.protocol}")
42
59
 
43
60
 
44
61
  RequestType = TypeVar("RequestType")
_tilebox/grpc/channel.py CHANGED
@@ -2,6 +2,7 @@ import json
2
2
  import re
3
3
  from collections.abc import Callable
4
4
  from dataclasses import dataclass
5
+ from enum import Enum
5
6
  from typing import TypeVar
6
7
 
7
8
  from grpc import (
@@ -43,10 +44,21 @@ CHANNEL_OPTIONS = [
43
44
  ]
44
45
 
45
46
 
47
+ class ChannelProtocol(Enum):
48
+ HTTPS = 1
49
+ HTTP = 2
50
+ UNIX = 3
51
+
52
+
46
53
  @dataclass
47
54
  class ChannelInfo:
48
- url_without_protocol: str
49
- use_ssl: bool
55
+ address: str
56
+ """GRPC target address. For http(s) connections this is the host[:port] format without a scheme. For unix sockets
57
+ this is the unix socket path including the `unix://` prefix for absolute paths or `unix:` for relative paths."""
58
+ port: int
59
+ """Port number for http(s) connections. For unix sockets this is always 0."""
60
+ protocol: ChannelProtocol
61
+ """The protocol to use for the channel."""
50
62
 
51
63
 
52
64
  def open_channel(url: str, auth_token: str | None = None) -> Channel:
@@ -69,14 +81,22 @@ def open_channel(url: str, auth_token: str | None = None) -> Channel:
69
81
 
70
82
 
71
83
  def _open_channel(channel_info: ChannelInfo) -> Channel:
72
- if channel_info.use_ssl:
73
- return secure_channel(
74
- channel_info.url_without_protocol,
75
- ssl_channel_credentials(),
76
- CHANNEL_OPTIONS,
77
- compression=Compression.Gzip,
78
- )
79
- return insecure_channel(channel_info.url_without_protocol, CHANNEL_OPTIONS, compression=Compression.NoCompression)
84
+ match channel_info.protocol:
85
+ case ChannelProtocol.HTTPS:
86
+ return secure_channel(
87
+ f"{channel_info.address}:{channel_info.port}",
88
+ ssl_channel_credentials(),
89
+ CHANNEL_OPTIONS,
90
+ compression=Compression.Gzip,
91
+ )
92
+ case ChannelProtocol.HTTP:
93
+ return insecure_channel(
94
+ f"{channel_info.address}:{channel_info.port}", CHANNEL_OPTIONS, compression=Compression.NoCompression
95
+ )
96
+ case ChannelProtocol.UNIX:
97
+ return insecure_channel(channel_info.address, CHANNEL_OPTIONS, compression=Compression.NoCompression)
98
+ case _:
99
+ raise ValueError(f"Unsupported channel protocol: {channel_info.protocol}")
80
100
 
81
101
 
82
102
  _URL_SCHEME = re.compile(r"^(https?://)?([^: ]+)(:\d+)?/?$")
@@ -98,27 +118,28 @@ def parse_channel_info(url: str) -> ChannelInfo:
98
118
  A ChannelInfo object that can be used to create a gRPC channel.
99
119
  """
100
120
  # See https://github.com/grpc/grpc/blob/master/doc/naming.md
101
- if url.startswith("unix:"):
102
- return ChannelInfo(url, False)
121
+ if url.startswith("unix:"): ## unix:///absolute/path or unix://path
122
+ return ChannelInfo(url, 0, ChannelProtocol.UNIX)
103
123
 
104
124
  # `urllib.parse.urlparse` behaves a bit weird with URLs that don't have a scheme but a port number, so regex it is
105
125
  if (match := _URL_SCHEME.match(url)) is None:
106
126
  raise ValueError(f"Invalid URL: {url}")
107
127
  scheme, netloc, port = match.groups()
108
128
  netloc = netloc.rstrip("/")
109
- use_ssl = True
129
+ protocol = ChannelProtocol.HTTPS
110
130
 
111
131
  if scheme == "http://": # explicitly set http -> require a port
112
132
  if port is None:
113
133
  raise ValueError("Explicit port required for insecure HTTP channel")
114
- use_ssl = False
134
+ protocol = ChannelProtocol.HTTP
135
+
136
+ # no scheme, but a port that looks like a dev port -> insecure
137
+ if scheme is None and port is not None and port != ":443":
138
+ protocol = ChannelProtocol.HTTP
115
139
 
116
- if scheme is None and port is not None: # no scheme, but a port that looks like a dev port -> insecure
117
- use_ssl = port == ":443"
140
+ port_number = 443 if port is None else int(port.removeprefix(":"))
118
141
 
119
- if use_ssl:
120
- return ChannelInfo(netloc + (port or ":443"), True)
121
- return ChannelInfo(netloc + port, False)
142
+ return ChannelInfo(netloc, port_number, protocol)
122
143
 
123
144
 
124
145
  RequestType = TypeVar("RequestType")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tilebox-grpc
3
- Version: 0.37.0
3
+ Version: 0.37.1
4
4
  Summary: GRPC / Protocol Buffers functions for Tilebox
5
5
  Project-URL: Homepage, https://tilebox.com
6
6
  Project-URL: Documentation, https://docs.tilebox.com/
@@ -1,15 +1,15 @@
1
1
  _tilebox/grpc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- _tilebox/grpc/channel.py,sha256=2uBAHcmKdGCzqVHhTsISAVirI9bQxXNfbC4GGM6RyHM,5332
2
+ _tilebox/grpc/channel.py,sha256=e0Dyn1GqgJScHcwMfNKwfLa1VPalHp_B3li3IEoKmoc,6342
3
3
  _tilebox/grpc/error.py,sha256=QVfLuvitYLlhWENE98zdb4m9gx0PGMsR6m1_prwkwj4,3598
4
4
  _tilebox/grpc/pagination.py,sha256=-KhkhQ_0IE-BgzjNAmkPac7fH55ZGICstaqRhCLvHGE,1292
5
5
  _tilebox/grpc/producer_consumer.py,sha256=NG6gUCcvYOGbplV1Vq9bFro-4cNpTHvT7eL4zlNDkkA,2154
6
6
  _tilebox/grpc/replay.py,sha256=_NC76BfmvazNrJI5MovSnPTC7kYwWZn3Au_0yPlOLsc,6006
7
7
  _tilebox/grpc/aio/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- _tilebox/grpc/aio/channel.py,sha256=6wWtR4h4ASDYOgAPXP0o_o3h0YEnzonwwwFI6d-5AZY,2305
8
+ _tilebox/grpc/aio/channel.py,sha256=H6poWZT4jn62kHsSdCrkW85t8fGLlbzwGJr8Y6-aVN8,2976
9
9
  _tilebox/grpc/aio/error.py,sha256=Lkf20vuQn9XaLj0MlQ1uqHRaenhYJ_P3rggNZyDMML8,1222
10
10
  _tilebox/grpc/aio/pagination.py,sha256=6v95yqgC-m87kD96pjfpIsmY41YuxxR0Se1o3NTrkSs,1115
11
11
  _tilebox/grpc/aio/producer_consumer.py,sha256=aOZhZHD74GZEvfeZ9OzUsVk5uUIuuMQp1V7l3Y1PQ0w,2679
12
12
  _tilebox/grpc/aio/syncify.py,sha256=jhScVqEwsoNP6gLwJpnIIaBqMC3q4XqSkEfIPokYG8c,4035
13
- tilebox_grpc-0.37.0.dist-info/METADATA,sha256=_9ZN4eowxbu6qhKmMMcpdJHXEtT3eEYfGK6HxRA7aFY,2955
14
- tilebox_grpc-0.37.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
- tilebox_grpc-0.37.0.dist-info/RECORD,,
13
+ tilebox_grpc-0.37.1.dist-info/METADATA,sha256=jzqwpuLkmbsQWcKuPF8RsGAex-MAlTlL3noFctQFU58,2955
14
+ tilebox_grpc-0.37.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
15
+ tilebox_grpc-0.37.1.dist-info/RECORD,,