Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conformance/client_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ features:
- PROTOCOL_GRPC_WEB
codecs:
- CODEC_PROTO
- CODEC_JSON
compressions:
- COMPRESSION_IDENTITY
- COMPRESSION_GZIP
Expand Down
4 changes: 4 additions & 0 deletions conformance/client_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ async def handle_message(msg: client_compat_pb2.ClientCompatRequest) -> client_c
payloads = []
try:
options = ClientOptions()

if msg.protocol == config_pb2.PROTOCOL_GRPC:
options.protocol = "grpc"
if msg.protocol == config_pb2.PROTOCOL_GRPC_WEB:
Expand All @@ -318,6 +319,9 @@ async def handle_message(msg: client_compat_pb2.ClientCompatRequest) -> client_c
if msg.compression == config_pb2.COMPRESSION_GZIP:
options.request_compression_name = "gzip"

if msg.codec == config_pb2.CODEC_JSON:
options.use_binary_format = False

client = service_connect.ConformanceServiceClient(base_url=url, session=session, options=options)
if msg.stream_type == config_pb2.STREAM_TYPE_UNARY:
if msg.request_delay_ms > 0:
Expand Down
9 changes: 7 additions & 2 deletions src/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from yarl import URL

from connect.code import Code
from connect.codec import Codec, ProtoBinaryCodec
from connect.codec import Codec, CodecNameType, ProtoBinaryCodec, ProtoJSONCodec
from connect.compression import COMPRESSION_IDENTITY, Compression, GZipCompression, get_compresion_from_name
from connect.connect import (
Spec,
Expand Down Expand Up @@ -120,7 +120,12 @@ def __init__(self, raw_url: str, options: ClientOptions):
elif options.protocol == "grpc-web":
self.protocol = ProtocolGRPC(web=True)
self.procedure = proto_path
self.codec = ProtoBinaryCodec()

if options.use_binary_format:
self.codec = ProtoBinaryCodec()
else:
self.codec = ProtoJSONCodec(CodecNameType.JSON)

self.request_compression_name = options.request_compression_name
self.compressions = [GZipCompression()]
if self.request_compression_name and self.request_compression_name != COMPRESSION_IDENTITY:
Expand Down
6 changes: 3 additions & 3 deletions src/connect/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def marshal(self, message: Any) -> bytes:

json_str = json_format.MessageToJson(message)

return json_str.encode("utf-8")
return json_str.encode()

def unmarshal(self, data: bytes, message: Any) -> Any:
"""Unmarshal the given byte data into a protobuf message.
Expand All @@ -269,7 +269,7 @@ def unmarshal(self, data: bytes, message: Any) -> Any:
if not isinstance(obj, google.protobuf.message.Message):
raise ValueError("Data is not a protobuf message")

return json_format.Parse(data.decode("utf-8"), obj, ignore_unknown_fields=True)
return json_format.Parse(data.decode(), obj, ignore_unknown_fields=True)

def marshal_stable(self, message: Any) -> bytes:
"""Serialize a protobuf message to a JSON string encoded as UTF-8 bytes in a deterministic way.
Expand Down Expand Up @@ -298,7 +298,7 @@ def marshal_stable(self, message: Any) -> bytes:
parsed = json.loads(json_str)
compacted_json = json.dumps(parsed, separators=(",", ":"))

return compacted_json.encode("utf-8")
return compacted_json.encode()

def is_binary(self) -> bool:
"""Determine if the codec is binary.
Expand Down
3 changes: 3 additions & 0 deletions src/connect/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ClientOptions(BaseModel):
protocol: Literal["connect", "grpc", "grpc-web"] = Field(default="connect")
"""The protocol to use for the request."""

use_binary_format: bool = Field(default=True)
"""A boolean indicating whether to use binary format for the request."""

def merge(self, override_options: "ClientOptions | None" = None) -> "ClientOptions":
"""Merge this options object with an override options object.

Expand Down
Loading