Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 17 additions & 34 deletions examples/eventsub_websocket/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#![warn(clippy::unwrap_in_result)]
pub mod opts;
pub mod util;
pub mod websocket;

use clap::Parser;
pub use opts::Secret;
use reqwest::Client;

use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use opts::Opts;

use eyre::Context;

use twitch_api::{client::ClientDefault, HelixClient};

static HELIX_CLIENT: LazyLock<HelixClient<'_, Client>> = LazyLock::new(|| {
HelixClient::with_client(
<reqwest::Client>::default_client_with_name(Some(
"twitch-rs/eventsub"
.parse()
.wrap_err_with(|| "when creating header name")
.unwrap(),
))
.wrap_err_with(|| "when creating client")
.unwrap(),
)
});

#[tokio::main]
async fn main() -> Result<(), eyre::Report> {
// Setup dotenv, tracing and error reporting with eyre
Expand All @@ -39,15 +52,7 @@ async fn main() -> Result<(), eyre::Report> {
/// Run the application
pub async fn run(opts: Arc<Opts>) -> eyre::Result<()> {
// Create the HelixClient, which is used to make requests to the Twitch API
let client: HelixClient<_> = twitch_api::HelixClient::with_client(
<reqwest::Client>::default_client_with_name(Some(
"twitch-rs/eventsub"
.parse()
.wrap_err_with(|| "when creating header name")
.unwrap(),
))
.wrap_err_with(|| "when creating client")?,
);
let client: &'static HelixClient<_> = LazyLock::force(&HELIX_CLIENT);

// Get the access token from the cli, dotenv or an oauth service
let token: twitch_oauth2::UserToken =
Expand All @@ -67,27 +72,5 @@ pub async fn run(opts: Arc<Opts>) -> eyre::Result<()> {
token.user_id.clone()
};

let websocket_client = websocket::WebsocketClient {
session_id: None,
token,
client,
user_id,
connect_url: twitch_api::TWITCH_EVENTSUB_WEBSOCKET_URL.clone(),
opts,
};

let websocket_client = tokio::spawn(async move { websocket_client.run().await });

tokio::try_join!(flatten(websocket_client))?;
Ok(())
}

async fn flatten<T>(
handle: tokio::task::JoinHandle<Result<T, eyre::Report>>,
) -> Result<T, eyre::Report> {
match handle.await {
Ok(Ok(result)) => Ok(result),
Ok(Err(err)) => Err(err),
Err(e) => Err(e).wrap_err_with(|| "handling failed"),
}
websocket::run(client, token, opts, user_id).await
}
1 change: 0 additions & 1 deletion examples/eventsub_websocket/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub async fn make_token<'a>(
UserToken::from_token(client, token.into())
.await
.context("could not get/make access token")
.map_err(Into::into)
}

/// Get an access token from either the cli, dotenv (via [clap::Arg::env]) or an oauth service
Expand Down
Loading
Loading