Move API client to its own file

This commit is contained in:
Joshua Coles 2024-07-15 13:28:16 +01:00
parent 72aaf40f4b
commit bd5608a1de
2 changed files with 56 additions and 51 deletions

View File

@ -1,55 +1,6 @@
use std::num::NonZero;
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};
use toggl::TogglApi;
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;
}
}
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 }
}
}
mod toggl;
#[tokio::main]
async fn main() {

54
src/toggl/mod.rs Normal file
View 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 }
}
}