Skip to content

Commit 7509e71

Browse files
committed
cherry-pick change from fork
1 parent 7be26da commit 7509e71

File tree

8 files changed

+173
-128
lines changed

8 files changed

+173
-128
lines changed

temporalio/bridge/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class RpcCall:
7777
rpc: str
7878
req: bytes
7979
retry: bool
80-
metadata: Mapping[str, str]
80+
metadata: Mapping[str, str | bytes]
8181
timeout_millis: Optional[int]
8282

8383

@@ -124,7 +124,7 @@ async def call(
124124
req: google.protobuf.message.Message,
125125
resp_type: Type[ProtoMessage],
126126
retry: bool,
127-
metadata: Mapping[str, str],
127+
metadata: Mapping[str, str | bytes],
128128
timeout: Optional[timedelta],
129129
) -> ProtoMessage:
130130
"""Make RPC call using SDK Core."""

temporalio/bridge/src/client.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use temporal_client::{
88
ConfiguredClient, HealthService, HttpConnectProxyOptions, RetryClient, RetryConfig,
99
TemporalServiceClientWithMetrics, TestService, TlsConfig, WorkflowService,
1010
};
11-
use tonic::metadata::MetadataKey;
11+
use tonic::metadata::{
12+
AsciiMetadataKey, AsciiMetadataValue, BinaryMetadataKey, BinaryMetadataValue,
13+
};
1214
use url::Url;
1315

1416
use crate::runtime;
@@ -72,10 +74,18 @@ struct RpcCall {
7274
rpc: String,
7375
req: Vec<u8>,
7476
retry: bool,
75-
metadata: HashMap<String, String>,
77+
metadata: HashMap<String, RpcMetadataValue>,
7678
timeout_millis: Option<u64>,
7779
}
7880

81+
#[derive(FromPyObject)]
82+
enum RpcMetadataValue {
83+
#[pyo3(transparent, annotation = "str")]
84+
Str(String),
85+
#[pyo3(transparent, annotation = "bytes")]
86+
Bytes(Vec<u8>),
87+
}
88+
7989
pub fn connect_client<'a>(
8090
py: Python<'a>,
8191
runtime_ref: &runtime::RuntimeRef,
@@ -536,12 +546,32 @@ fn rpc_req<P: prost::Message + Default>(call: RpcCall) -> PyResult<tonic::Reques
536546
.map_err(|err| PyValueError::new_err(format!("Invalid proto: {err}")))?;
537547
let mut req = tonic::Request::new(proto);
538548
for (k, v) in call.metadata {
539-
req.metadata_mut().insert(
540-
MetadataKey::from_str(k.as_str())
541-
.map_err(|err| PyValueError::new_err(format!("Invalid metadata key: {err}")))?,
542-
v.parse()
543-
.map_err(|err| PyValueError::new_err(format!("Invalid metadata value: {err}")))?,
544-
);
549+
if let Ok(binary_key) = BinaryMetadataKey::from_str(&k) {
550+
let RpcMetadataValue::Bytes(bytes) = v else {
551+
return Err(PyValueError::new_err(format!(
552+
"Invalid metadata value for binary key {k}: expected bytes"
553+
)));
554+
};
555+
556+
req.metadata_mut()
557+
.insert_bin(binary_key, BinaryMetadataValue::from_bytes(&bytes));
558+
} else {
559+
let ascii_key = AsciiMetadataKey::from_str(&k)
560+
.map_err(|err| PyValueError::new_err(format!("Invalid metadata key: {err}")))?;
561+
562+
let RpcMetadataValue::Str(string) = v else {
563+
return Err(PyValueError::new_err(format!(
564+
"Invalid metadata value for ASCII key {k}: expected str"
565+
)));
566+
};
567+
568+
req.metadata_mut().insert(
569+
ascii_key,
570+
AsciiMetadataValue::from_str(&string).map_err(|err| {
571+
PyValueError::new_err(format!("Invalid metadata value: {err}"))
572+
})?,
573+
);
574+
}
545575
}
546576
if let Some(timeout_millis) = call.timeout_millis {
547577
req.set_timeout(Duration::from_millis(timeout_millis));

0 commit comments

Comments
 (0)