Skip to content

Commit

Permalink
Merge pull request #242 from whisperfish/stories
Browse files Browse the repository at this point in the history
Stories APIs
  • Loading branch information
rubdos authored Sep 1, 2023
2 parents 8789920 + 37cf26a commit 477c739
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 18 deletions.
64 changes: 58 additions & 6 deletions libsignal-service-actix/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ impl AwcPushService {
method: Method,
endpoint: Endpoint,
path: impl AsRef<str>,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
) -> Result<ClientRequest, ServiceError> {
let url = self.cfg.base_url(endpoint).join(path.as_ref())?;
log::debug!("HTTP request {} {}", method, url);
let mut builder = self.client.request(method, url.as_str());
for &header in additional_headers {
builder = builder.insert_header(header);
}
builder = match credentials_override {
HttpAuthOverride::NoOverride => {
if let Some(credentials) = self.credentials.as_ref() {
Expand Down Expand Up @@ -159,14 +163,21 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
) -> Result<T, ServiceError>
where
for<'de> T: Deserialize<'de>,
{
use awc::error::{ConnectError, SendRequestError};
let mut response = self
.request(Method::GET, endpoint, path, credentials_override)?
.request(
Method::GET,
endpoint,
path,
additional_headers,
credentials_override,
)?
.send()
.await
.map_err(|e| match e {
Expand Down Expand Up @@ -212,6 +223,7 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
) -> Result<T, ServiceError>
where
for<'de> T: Deserialize<'de>,
Expand All @@ -221,6 +233,7 @@ impl PushService for AwcPushService {
Method::DELETE,
endpoint,
path,
additional_headers,
HttpAuthOverride::NoOverride,
)?
.send()
Expand Down Expand Up @@ -268,6 +281,7 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -276,7 +290,13 @@ impl PushService for AwcPushService {
S: Serialize,
{
let mut response = self
.request(Method::PUT, endpoint, path, credentials_override)?
.request(
Method::PUT,
endpoint,
path,
additional_headers,
credentials_override,
)?
.send_json(&value)
.await
.map_err(|e| ServiceError::SendError {
Expand Down Expand Up @@ -314,6 +334,7 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -322,7 +343,13 @@ impl PushService for AwcPushService {
S: Serialize,
{
let mut response = self
.request(Method::PATCH, endpoint, path, credentials_override)?
.request(
Method::PATCH,
endpoint,
path,
additional_headers,
credentials_override,
)?
.send_json(&value)
.await
.map_err(|e| ServiceError::SendError {
Expand Down Expand Up @@ -360,6 +387,7 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -368,7 +396,13 @@ impl PushService for AwcPushService {
S: Serialize,
{
let mut response = self
.request(Method::POST, endpoint, path, credentials_override)?
.request(
Method::POST,
endpoint,
path,
additional_headers,
credentials_override,
)?
.send_json(&value)
.await
.map_err(|e| ServiceError::SendError {
Expand Down Expand Up @@ -406,13 +440,20 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
) -> Result<T, ServiceError>
where
T: Default + ProtobufMessage,
{
let mut response = self
.request(Method::GET, endpoint, path, credentials_override)?
.request(
Method::GET,
endpoint,
path,
additional_headers,
credentials_override,
)?
.send()
.await
.map_err(|e| ServiceError::SendError {
Expand All @@ -433,6 +474,7 @@ impl PushService for AwcPushService {
&mut self,
endpoint: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
value: S,
) -> Result<D, ServiceError>
where
Expand All @@ -442,7 +484,13 @@ impl PushService for AwcPushService {
let buf = value.encode_to_vec();

let mut response = self
.request(Method::PUT, endpoint, path, HttpAuthOverride::NoOverride)?
.request(
Method::PUT,
endpoint,
path,
additional_headers,
HttpAuthOverride::NoOverride,
)?
.content_type(HeaderValue::from_static("application/x-protobuf"))
.send_body(buf)
.await
Expand Down Expand Up @@ -470,6 +518,7 @@ impl PushService for AwcPushService {
Method::GET,
Endpoint::Cdn(cdn_id),
path,
&[],
HttpAuthOverride::Unidentified,
)?
.send()
Expand Down Expand Up @@ -508,6 +557,7 @@ impl PushService for AwcPushService {
Method::POST,
Endpoint::Cdn(0),
path,
&[],
HttpAuthOverride::NoOverride,
)?;

Expand Down Expand Up @@ -575,13 +625,15 @@ impl PushService for AwcPushService {
async fn ws(
&mut self,
path: &str,
additional_headers: &[(&str, &str)],
credentials: Option<ServiceCredentials>,
keep_alive: bool,
) -> Result<SignalWebSocket, ServiceError> {
let (ws, stream) = AwcWebSocket::with_client(
&mut self.client,
self.cfg.base_url(Endpoint::Service),
path,
additional_headers,
credentials.as_ref(),
)
.await?;
Expand Down
7 changes: 6 additions & 1 deletion libsignal-service-actix/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl AwcWebSocket {
client: &mut awc::Client,
base_url: impl std::borrow::Borrow<Url>,
path: &str,
additional_headers: &[(&str, &str)],
credentials: Option<&ServiceCredentials>,
) -> Result<(Self, <Self as WebSocketService>::Stream), AwcWebSocketError>
{
Expand All @@ -160,7 +161,11 @@ impl AwcWebSocket {
}

log::trace!("Will start websocket at {:?}", url);
let (response, framed) = client.ws(url.as_str()).connect().await?;
let mut ws = client.ws(url.as_str());
for (key, value) in additional_headers {
ws = ws.header(*key, *value);
}
let (response, framed) = ws.connect().await?;

log::debug!("WebSocket connected: {:?}", response);

Expand Down
39 changes: 37 additions & 2 deletions libsignal-service-hyper/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl HyperPushService {
method: Method,
endpoint: Endpoint,
path: impl AsRef<str>,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
body: Option<RequestBody>,
) -> Result<Response<Body>, ServiceError> {
Expand All @@ -97,6 +98,10 @@ impl HyperPushService {
.uri(url.as_str())
.header(USER_AGENT, &self.user_agent);

for (header, value) in additional_headers {
builder = builder.header(*header, *value);
}

match credentials_override {
HttpAuthOverride::NoOverride => {
if let Some(HttpAuth { username, password }) =
Expand Down Expand Up @@ -279,13 +284,21 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
) -> Result<T, ServiceError>
where
for<'de> T: Deserialize<'de>,
{
let mut response = self
.request(Method::GET, service, path, credentials_override, None)
.request(
Method::GET,
service,
path,
additional_headers,
credentials_override,
None,
)
.await?;

Self::json(&mut response).await
Expand All @@ -295,6 +308,7 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
) -> Result<T, ServiceError>
where
for<'de> T: Deserialize<'de>,
Expand All @@ -304,6 +318,7 @@ impl PushService for HyperPushService {
Method::DELETE,
service,
path,
additional_headers,
HttpAuthOverride::NoOverride,
None,
)
Expand All @@ -316,6 +331,7 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -334,6 +350,7 @@ impl PushService for HyperPushService {
Method::PUT,
service,
path,
additional_headers,
credentials_override,
Some(RequestBody {
contents: json,
Expand All @@ -349,6 +366,7 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -367,6 +385,7 @@ impl PushService for HyperPushService {
Method::PATCH,
service,
path,
additional_headers,
credentials_override,
Some(RequestBody {
contents: json,
Expand All @@ -382,6 +401,7 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
value: S,
) -> Result<D, ServiceError>
Expand All @@ -400,6 +420,7 @@ impl PushService for HyperPushService {
Method::POST,
service,
path,
additional_headers,
credentials_override,
Some(RequestBody {
contents: json,
Expand All @@ -415,13 +436,21 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
credentials_override: HttpAuthOverride,
) -> Result<T, ServiceError>
where
T: Default + libsignal_service::prelude::ProtobufMessage,
{
let mut response = self
.request(Method::GET, service, path, credentials_override, None)
.request(
Method::GET,
service,
path,
additional_headers,
credentials_override,
None,
)
.await?;

Self::protobuf(&mut response).await
Expand All @@ -431,6 +460,7 @@ impl PushService for HyperPushService {
&mut self,
service: Endpoint,
path: &str,
additional_headers: &[(&str, &str)],
value: S,
) -> Result<D, ServiceError>
where
Expand All @@ -444,6 +474,7 @@ impl PushService for HyperPushService {
Method::PUT,
service,
path,
additional_headers,
HttpAuthOverride::NoOverride,
Some(RequestBody {
contents: protobuf,
Expand All @@ -465,6 +496,7 @@ impl PushService for HyperPushService {
Method::GET,
Endpoint::Cdn(cdn_id),
path,
&[],
HttpAuthOverride::Unidentified, // CDN requests are always without authentication
None,
)
Expand Down Expand Up @@ -533,6 +565,7 @@ impl PushService for HyperPushService {
Method::POST,
Endpoint::Cdn(0),
path,
&[],
HttpAuthOverride::NoOverride,
Some(RequestBody {
contents: body_contents,
Expand All @@ -549,13 +582,15 @@ impl PushService for HyperPushService {
async fn ws(
&mut self,
path: &str,
additional_headers: &[(&str, &str)],
credentials: Option<ServiceCredentials>,
keep_alive: bool,
) -> Result<SignalWebSocket, ServiceError> {
let (ws, stream) = TungsteniteWebSocket::with_tls_config(
Self::tls_config(&self.cfg),
self.cfg.base_url(Endpoint::Service),
path,
additional_headers,
credentials.as_ref(),
)
.await?;
Expand Down
Loading

0 comments on commit 477c739

Please sign in to comment.