Skip to content

Commit

Permalink
chore: updated TopicClient to accept impl Into<String> to accept stri…
Browse files Browse the repository at this point in the history
…ngs like CacheClient methods
  • Loading branch information
anitarua committed May 3, 2024
1 parent 17bfcfc commit cd75851
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
8 changes: 2 additions & 6 deletions example/src/bin/docs_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,11 +796,7 @@ pub async fn example_API_TopicPublish(
topic_name: &String,
) -> Result<(), MomentoError> {
match topic_client
.publish(
cache_name.to_string(),
topic_name.to_string(),
"Hello, Momento!",
)
.publish(cache_name, topic_name, "Hello, Momento!")
.await
{
Ok(_) => println!("Published message!",),
Expand All @@ -817,7 +813,7 @@ pub async fn example_API_TopicSubscribe(
) -> Result<(), MomentoError> {
// Make a subscription
let mut subscription = topic_client
.subscribe(cache_name.to_string(), topic_name.to_string(), None)
.subscribe(cache_name, topic_name, None)
.await
.expect("subscribe rpc failed");

Expand Down
32 changes: 16 additions & 16 deletions src/topics/topic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl TopicClient {
/// You don't create topics, you just start using them.
pub async fn publish(
&self,
cache_name: String,
topic: String,
cache_name: impl Into<String>,
topic: impl Into<String>,
value: impl IntoTopicValue,
) -> Result<(), MomentoError> {
TopicClient::actually_publish(&mut self.client.clone(), cache_name, topic, value).await
Expand All @@ -82,23 +82,23 @@ impl TopicClient {
/// Use this if you have &mut, as it will save you a small amount of overhead for reusing the client.
pub async fn publish_mut(
&mut self,
cache_name: String,
topic: String,
cache_name: impl Into<String>,
topic: impl Into<String>,
value: impl IntoTopicValue,
) -> Result<(), MomentoError> {
TopicClient::actually_publish(&mut self.client, cache_name, topic, value).await
}

async fn actually_publish(
client: &mut PubsubClient<ChannelType>,
cache_name: String,
topic: String,
cache_name: impl Into<String>,
topic: impl Into<String>,
value: impl IntoTopicValue,
) -> Result<(), MomentoError> {
client
.publish(PublishRequest {
cache_name,
topic,
cache_name: cache_name.into(),
topic: topic.into(),
value: Some(pubsub::TopicValue {
kind: Some(value.into_topic_value()),
}),
Expand All @@ -112,8 +112,8 @@ impl TopicClient {
/// You don't create topics, you just start using them.
pub async fn subscribe(
&self,
cache_name: String,
topic: String,
cache_name: impl Into<String> + Clone,
topic: impl Into<String> + Clone,
resume_at_topic_sequence_number: Option<u64>,
) -> Result<Subscription, MomentoError> {
TopicClient::actually_subscribe(
Expand All @@ -127,23 +127,23 @@ impl TopicClient {

async fn actually_subscribe(
mut client: PubsubClient<ChannelType>,
cache_name: String,
topic: String,
cache_name: impl Into<String> + Clone,
topic: impl Into<String> + Clone,
resume_at_topic_sequence_number: Option<u64>,
) -> Result<Subscription, MomentoError> {
let tonic_stream = client
.subscribe(SubscriptionRequest {
cache_name: cache_name.clone(),
topic: topic.clone(),
cache_name: cache_name.clone().into(),
topic: topic.clone().into(),
resume_at_topic_sequence_number: resume_at_topic_sequence_number
.unwrap_or_default(),
})
.await?
.into_inner();
Ok(Subscription {
client,
cache_name,
topic,
cache_name: cache_name.into(),
topic: topic.into(),
current_sequence_number: resume_at_topic_sequence_number.unwrap_or_default(),
current_subscription: SubscriptionState::Subscribed(tonic_stream),
})
Expand Down

0 comments on commit cd75851

Please sign in to comment.