Move API client to its own file
This commit is contained in:
parent
72aaf40f4b
commit
bd5608a1de
53
src/main.rs
53
src/main.rs
@ -1,55 +1,6 @@
|
|||||||
use std::num::NonZero;
|
use toggl::TogglApi;
|
||||||
use std::time::Duration;
|
|
||||||
use axum::async_trait;
|
|
||||||
use governor::clock::DefaultClock;
|
|
||||||
use governor::state::{InMemoryState, NotKeyed};
|
|
||||||
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
|
|
||||||
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff, Jitter};
|
|
||||||
|
|
||||||
struct ReqwestRateLimiter {
|
mod toggl;
|
||||||
rate_limiter: governor::RateLimiter<NotKeyed, InMemoryState, DefaultClock>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ReqwestRateLimiter {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
rate_limiter: governor::RateLimiter::direct(
|
|
||||||
governor::Quota::per_second(NonZero::new(1u32).unwrap())
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl reqwest_ratelimit::RateLimiter for ReqwestRateLimiter {
|
|
||||||
async fn acquire_permit(&self) {
|
|
||||||
self.rate_limiter.until_ready().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TogglApi {
|
|
||||||
client: ClientWithMiddleware,
|
|
||||||
api_key: String,
|
|
||||||
workspace_id: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TogglApi {
|
|
||||||
fn new(api_key: String, workspace_id: u32) -> Self {
|
|
||||||
let rate_limiter = ReqwestRateLimiter::new();
|
|
||||||
let backoff = ExponentialBackoff::builder()
|
|
||||||
.retry_bounds(Duration::from_secs(1), Duration::from_secs(60))
|
|
||||||
.jitter(Jitter::Bounded)
|
|
||||||
.base(2)
|
|
||||||
.build_with_total_retry_duration(Duration::from_secs(24 * 60 * 60));
|
|
||||||
|
|
||||||
let client = ClientBuilder::new(reqwest::Client::new())
|
|
||||||
.with(reqwest_ratelimit::all(rate_limiter))
|
|
||||||
.with(RetryTransientMiddleware::new_with_policy(backoff))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Self { client, api_key, workspace_id }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
|||||||
54
src/toggl/mod.rs
Normal file
54
src/toggl/mod.rs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
use reqwest_retry::policies::ExponentialBackoff;
|
||||||
|
use std::time::Duration;
|
||||||
|
use reqwest_retry::{Jitter, RetryTransientMiddleware};
|
||||||
|
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
|
||||||
|
use std::num::NonZero;
|
||||||
|
use axum::async_trait;
|
||||||
|
use governor::state::{InMemoryState, NotKeyed};
|
||||||
|
use governor::clock::DefaultClock;
|
||||||
|
|
||||||
|
struct ReqwestRateLimiter {
|
||||||
|
rate_limiter: governor::RateLimiter<NotKeyed, InMemoryState, DefaultClock>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ReqwestRateLimiter {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
rate_limiter: governor::RateLimiter::direct(
|
||||||
|
governor::Quota::per_second(NonZero::new(1u32).unwrap())
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl reqwest_ratelimit::RateLimiter for ReqwestRateLimiter {
|
||||||
|
async fn acquire_permit(&self) {
|
||||||
|
self.rate_limiter.until_ready().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TogglApi {
|
||||||
|
client: ClientWithMiddleware,
|
||||||
|
api_key: String,
|
||||||
|
workspace_id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TogglApi {
|
||||||
|
pub fn new(api_key: String, workspace_id: u32) -> Self {
|
||||||
|
let rate_limiter = ReqwestRateLimiter::new();
|
||||||
|
let backoff = ExponentialBackoff::builder()
|
||||||
|
.retry_bounds(Duration::from_secs(1), Duration::from_secs(60))
|
||||||
|
.jitter(Jitter::Bounded)
|
||||||
|
.base(2)
|
||||||
|
.build_with_total_retry_duration(Duration::from_secs(24 * 60 * 60));
|
||||||
|
|
||||||
|
let client = ClientBuilder::new(reqwest::Client::new())
|
||||||
|
.with(reqwest_ratelimit::all(rate_limiter))
|
||||||
|
.with(RetryTransientMiddleware::new_with_policy(backoff))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Self { client, api_key, workspace_id }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user