wasmer_backend_api/
subscription.rs

1use crate::{
2    WasmerClient,
3    types::{
4        AutobuildDeploymentSubscription, AutobuildDeploymentSubscriptionVariables,
5        PackageVersionReadySubscription, PackageVersionReadySubscriptionVariables, Uuid,
6    },
7};
8use anyhow::Context;
9use async_tungstenite::tungstenite::client::IntoClientRequest;
10use cynic::SubscriptionBuilder;
11use graphql_ws_client::Subscription;
12use reqwest::header::HeaderValue;
13use std::future::IntoFuture;
14
15pub async fn package_version_ready(
16    client: &WasmerClient,
17    package_version_id: &str,
18) -> anyhow::Result<
19    Subscription<
20        cynic::StreamingOperation<
21            PackageVersionReadySubscription,
22            PackageVersionReadySubscriptionVariables,
23        >,
24    >,
25> {
26    let mut url = client.graphql_endpoint().clone();
27    if url.scheme() == "http" {
28        url.set_scheme("ws").unwrap();
29    } else if url.scheme() == "https" {
30        url.set_scheme("wss").unwrap();
31    }
32
33    let url = url.to_string();
34    let mut req = url.into_client_request()?;
35
36    req.headers_mut().insert(
37        "Sec-WebSocket-Protocol",
38        HeaderValue::from_str("graphql-transport-ws").unwrap(),
39    );
40
41    if let Some(token) = client.auth_token() {
42        req.headers_mut().insert(
43            reqwest::header::AUTHORIZATION,
44            HeaderValue::from_str(&format!("Bearer {token}"))?,
45        );
46    }
47
48    req.headers_mut()
49        .insert(reqwest::header::USER_AGENT, client.user_agent.clone());
50
51    let (connection, _resp) = async_tungstenite::tokio::connect_async(req)
52        .await
53        .context("could not connect")?;
54
55    let (client, actor) = graphql_ws_client::Client::build(connection).await?;
56    tokio::spawn(actor.into_future());
57
58    let stream = client
59        .subscribe(PackageVersionReadySubscription::build(
60            PackageVersionReadySubscriptionVariables {
61                package_version_id: cynic::Id::new(package_version_id),
62            },
63        ))
64        .await?;
65
66    Ok(stream)
67}
68
69pub async fn autobuild_deployment(
70    client: &WasmerClient,
71    build_id: &str,
72) -> anyhow::Result<
73    Subscription<
74        cynic::StreamingOperation<
75            AutobuildDeploymentSubscription,
76            AutobuildDeploymentSubscriptionVariables,
77        >,
78    >,
79> {
80    let mut url = client.graphql_endpoint().clone();
81    if url.scheme() == "http" {
82        url.set_scheme("ws").unwrap();
83    } else if url.scheme() == "https" {
84        url.set_scheme("wss").unwrap();
85    }
86
87    let url = url.to_string();
88    let mut req = url.into_client_request()?;
89
90    req.headers_mut().insert(
91        "Sec-WebSocket-Protocol",
92        HeaderValue::from_str("graphql-transport-ws").unwrap(),
93    );
94
95    if let Some(token) = client.auth_token() {
96        req.headers_mut().insert(
97            reqwest::header::AUTHORIZATION,
98            HeaderValue::from_str(&format!("Bearer {token}"))?,
99        );
100    }
101
102    req.headers_mut()
103        .insert(reqwest::header::USER_AGENT, client.user_agent.clone());
104
105    let (connection, _resp) = async_tungstenite::tokio::connect_async(req)
106        .await
107        .context("could not connect")?;
108
109    let (client, actor) = graphql_ws_client::Client::build(connection).await?;
110    tokio::spawn(actor.into_future());
111
112    let stream = client
113        .subscribe(AutobuildDeploymentSubscription::build(
114            AutobuildDeploymentSubscriptionVariables {
115                build_id: Uuid(build_id.to_string()),
116            },
117        ))
118        .await?;
119
120    Ok(stream)
121}