Skip to content

Commit 8d0cd04

Browse files
authored
Merge pull request kayleg#5 from sync/feature/pubsub-emulator
Add possibility to use google pubsub emulator (given PUBSUB_EMULATOR_HOST env)
2 parents 77f006e + 0ae22cf commit 8d0cd04

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/subscription.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ use crate::error;
33
use crate::message::{FromPubSubMessage, Message};
44
use bytes::buf::BufExt as _;
55
use hyper::{Method, StatusCode};
6+
use lazy_static::lazy_static;
67
use serde_derive::{Deserialize, Serialize};
8+
use std::env;
9+
10+
lazy_static! {
11+
static ref PUBSUB_HOST: String = env::var("PUBSUB_EMULATOR_HOST")
12+
.map(|host| format!("http://{}", host))
13+
.unwrap_or_else(|_| String::from("https://pubsub.googleapis.com"));
14+
}
715

816
#[derive(Deserialize)]
917
struct Response {
@@ -35,7 +43,7 @@ impl Subscription {
3543
.as_ref()
3644
.expect("Subscription was not created using a client");
3745

38-
let uri: hyper::Uri = format!("https://pubsub.googleapis.com/v1/{}:acknowledge", self.name)
46+
let uri: hyper::Uri = format!("{}/v1/{}:acknowledge", *PUBSUB_HOST, self.name)
3947
.parse()
4048
.unwrap();
4149

@@ -57,7 +65,7 @@ impl Subscription {
5765
.as_ref()
5866
.expect("Subscription was not created using a client");
5967

60-
let uri: hyper::Uri = format!("https://pubsub.googleapis.com/v1/{}:pull", self.name)
68+
let uri: hyper::Uri = format!("{}/v1/{}:pull", *PUBSUB_HOST, self.name)
6169
.parse()
6270
.unwrap();
6371

@@ -104,7 +112,7 @@ impl Subscription {
104112
.client
105113
.expect("Subscription was not created using a client");
106114

107-
let uri: hyper::Uri = format!("https://pubsub.googleapis.com/v1/{}", self.name)
115+
let uri: hyper::Uri = format!("{}/v1/{}", *PUBSUB_HOST, self.name)
108116
.parse()
109117
.unwrap();
110118

src/topic.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ use crate::subscription::*;
44
use crate::EncodedMessage;
55
use bytes::buf::BufExt as _;
66
use hyper::{Method, StatusCode};
7+
use lazy_static::lazy_static;
78
use rand::distributions::Alphanumeric;
89
use rand::{thread_rng, Rng};
910
use serde::de::DeserializeOwned;
1011
use serde_derive::{Deserialize, Serialize};
12+
use std::env;
13+
14+
lazy_static! {
15+
static ref PUBSUB_HOST: String = env::var("PUBSUB_EMULATOR_HOST")
16+
.map(|host| format!("http://{}", host))
17+
.unwrap_or_else(|_| String::from("https://pubsub.googleapis.com"));
18+
}
1119

1220
#[derive(Deserialize, Serialize)]
1321
pub struct Topic {
@@ -38,7 +46,7 @@ impl Topic {
3846
client: None,
3947
};
4048

41-
let uri: hyper::Uri = format!("https://pubsub.googleapis.com/v1/{}", new_subscription.name)
49+
let uri: hyper::Uri = format!("{}/v1/{}", *PUBSUB_HOST, new_subscription.name)
4250
.parse()
4351
.unwrap();
4452

@@ -54,7 +62,7 @@ impl Topic {
5462
&self,
5563
data: T,
5664
) -> Result<PublishMessageResponse, error::Error> {
57-
let uri: hyper::Uri = format!("https://pubsub.googleapis.com/v1/{}:publish", self.name)
65+
let uri: hyper::Uri = format!("{}/v1/{}:publish", *PUBSUB_HOST, self.name)
5866
.parse()
5967
.unwrap();
6068

0 commit comments

Comments
 (0)