wasmer_backend_api/
subscription.rs1use crate::{
2 types::{PackageVersionReadySubscription, PackageVersionReadySubscriptionVariables},
3 WasmerClient,
4};
5use anyhow::Context;
6use async_tungstenite::tungstenite::client::IntoClientRequest;
7use cynic::SubscriptionBuilder;
8use graphql_ws_client::Subscription;
9use reqwest::header::HeaderValue;
10use std::future::IntoFuture;
11
12pub async fn package_version_ready(
13 client: &WasmerClient,
14 package_version_id: &str,
15) -> anyhow::Result<
16 Subscription<
17 cynic::StreamingOperation<
18 PackageVersionReadySubscription,
19 PackageVersionReadySubscriptionVariables,
20 >,
21 >,
22> {
23 let mut url = client.graphql_endpoint().clone();
24 if url.scheme() == "http" {
25 url.set_scheme("ws").unwrap();
26 } else if url.scheme() == "https" {
27 url.set_scheme("wss").unwrap();
28 }
29
30 let url = url.to_string();
31 let mut req = url.into_client_request()?;
32
33 req.headers_mut().insert(
34 "Sec-WebSocket-Protocol",
35 HeaderValue::from_str("graphql-transport-ws").unwrap(),
36 );
37
38 if let Some(token) = client.auth_token() {
39 req.headers_mut().insert(
40 reqwest::header::AUTHORIZATION,
41 HeaderValue::from_str(&format!("Bearer {token}"))?,
42 );
43 }
44
45 req.headers_mut()
46 .insert(reqwest::header::USER_AGENT, client.user_agent.clone());
47
48 let (connection, _resp) = async_tungstenite::tokio::connect_async(req)
49 .await
50 .context("could not connect")?;
51
52 let (client, actor) = graphql_ws_client::Client::build(connection).await?;
53 tokio::spawn(actor.into_future());
54
55 let stream = client
56 .subscribe(PackageVersionReadySubscription::build(
57 PackageVersionReadySubscriptionVariables {
58 package_version_id: cynic::Id::new(package_version_id),
59 },
60 ))
61 .await?;
62
63 Ok(stream)
64}