Skip to content

Commit 648ab71

Browse files
committed
cherry-pick change from fork
1 parent bb90aab commit 648ab71

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,
@@ -533,12 +543,32 @@ fn rpc_req<P: prost::Message + Default>(call: RpcCall) -> PyResult<tonic::Reques
533543
.map_err(|err| PyValueError::new_err(format!("Invalid proto: {err}")))?;
534544
let mut req = tonic::Request::new(proto);
535545
for (k, v) in call.metadata {
536-
req.metadata_mut().insert(
537-
MetadataKey::from_str(k.as_str())
538-
.map_err(|err| PyValueError::new_err(format!("Invalid metadata key: {err}")))?,
539-
v.parse()
540-
.map_err(|err| PyValueError::new_err(format!("Invalid metadata value: {err}")))?,
541-
);
546+
if let Ok(binary_key) = BinaryMetadataKey::from_str(&k) {
547+
let RpcMetadataValue::Bytes(bytes) = v else {
548+
return Err(PyValueError::new_err(format!(
549+
"Invalid metadata value for binary key {k}: expected bytes"
550+
)));
551+
};
552+
553+
req.metadata_mut()
554+
.insert_bin(binary_key, BinaryMetadataValue::from_bytes(&bytes));
555+
} else {
556+
let ascii_key = AsciiMetadataKey::from_str(&k)
557+
.map_err(|err| PyValueError::new_err(format!("Invalid metadata key: {err}")))?;
558+
559+
let RpcMetadataValue::Str(string) = v else {
560+
return Err(PyValueError::new_err(format!(
561+
"Invalid metadata value for ASCII key {k}: expected str"
562+
)));
563+
};
564+
565+
req.metadata_mut().insert(
566+
ascii_key,
567+
AsciiMetadataValue::from_str(&string).map_err(|err| {
568+
PyValueError::new_err(format!("Invalid metadata value: {err}"))
569+
})?,
570+
);
571+
}
542572
}
543573
if let Some(timeout_millis) = call.timeout_millis {
544574
req.set_timeout(Duration::from_millis(timeout_millis));

0 commit comments

Comments
 (0)