this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Store HTTP Client in AppState

moritz.vongoewels.de f306ae0d 0a54229d

verified
+33 -20
+7 -1
src/bin/checkpoints.rs
··· 2 2 3 3 use axum::ServiceExt; 4 4 use checkpoints::{ 5 - AppState, 5 + AppState, HTTP_CLIENT_TIMEOUT, USER_AGENT, 6 6 api::{self, TemplateEcs}, 7 7 }; 8 8 use clap::Parser; ··· 156 156 157 157 let http_config = config.http.clone(); 158 158 159 + let http_client = reqwest::Client::builder() 160 + .user_agent(USER_AGENT) 161 + .timeout(HTTP_CLIENT_TIMEOUT) 162 + .build()?; 163 + 159 164 let state = AppState { 160 165 config, 161 166 templates, 167 + http_client, 162 168 oauth_client, 163 169 }; 164 170
+4
src/lib.rs
··· 4 4 pub mod config; 5 5 pub mod strava; 6 6 7 + pub const USER_AGENT: &str = concat!("DSD-Checkpoints-v", env!("CARGO_PKG_VERSION")); 8 + pub const HTTP_CLIENT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); 9 + 7 10 #[derive(Clone, Debug)] 8 11 pub struct AppState<'a> { 9 12 pub config: config::Config, 10 13 pub oauth_client: auth::OAuthClient, 14 + pub http_client: reqwest::Client, 11 15 12 16 pub templates: minijinja::Environment<'a>, 13 17 }
+22 -19
src/strava.rs
··· 5 5 use serde::{Deserialize, Serialize, de::DeserializeOwned}; 6 6 use tracing::{debug, info, warn}; 7 7 8 - use crate::{AppState, AthleteId, AthleteName, api::auth}; 8 + use crate::{AppState, AthleteId, AthleteName, HTTP_CLIENT_TIMEOUT, USER_AGENT, api::auth}; 9 9 10 10 #[derive(Debug, Deserialize, Serialize, Clone)] 11 11 pub struct Athlete { ··· 83 83 todo!() 84 84 } 85 85 86 + fn build_strava_athlete_client( 87 + access_token: &AccessToken, 88 + ) -> Result<reqwest::Client, reqwest::Error> { 89 + let headers = http::HeaderMap::from_iter([( 90 + header::AUTHORIZATION, 91 + header::HeaderValue::from_str(&format!("Bearer {}", access_token.token.secret())).unwrap(), 92 + )]); 93 + 94 + reqwest::Client::builder() 95 + .user_agent(USER_AGENT) 96 + .timeout(HTTP_CLIENT_TIMEOUT) 97 + .default_headers(headers) 98 + .build() 99 + } 100 + 86 101 pub fn update_activities_system( 87 102 handle: &crate::TokioRuntime, 88 103 state: &AppState, 89 104 athletes: ecsdb::query::Query<(ecsdb::Entity, AthleteId)>, 90 105 ) -> Result<(), anyhow::Error> { 91 - let client = reqwest::Client::builder() 92 - .connection_verbose(true) 93 - .build()?; 94 - 95 106 for (athlete, athlete_id) in athletes.iter() { 96 107 let _span = tracing::info_span!("athlete", %athlete_id).entered(); 97 108 98 109 info!("Processing"); 99 110 100 - let access_token = handle 101 - .0 102 - .block_on(auth::refresh_access_token(&client, state, athlete))?; 111 + let access_token = handle.0.block_on(auth::refresh_access_token( 112 + &state.http_client, 113 + state, 114 + athlete, 115 + ))?; 103 116 104 - let headers = http::HeaderMap::from_iter([( 105 - header::AUTHORIZATION, 106 - header::HeaderValue::from_str(&format!("Bearer {}", access_token.token.secret())) 107 - .unwrap(), 108 - )]); 109 - 110 - let client = reqwest::Client::builder() 111 - .default_headers(headers) 112 - .connection_verbose(true) 113 - .build() 114 - .unwrap(); 117 + let client = build_strava_athlete_client(&access_token)?; 115 118 116 119 let activities = handle.0.block_on(fetch_activities( 117 120 &client,