jdrouet / tmdb-api

Yet another TMDB api client written in rust, working with async
13 stars 8 forks source link

make client abstract #67

Closed jdrouet closed 4 months ago

jdrouet commented 5 months ago

In order to give the user the ability to have a feature reach client (like implementing a rate limiting system), we should allow them to implement their own. To do that, we should have a client trait that anybody could implement.

agmbk commented 4 months ago

How would one simply add a before/after request ? Without copying all the current implementation.

jdrouet commented 4 months ago

You just need to implement crate::client::Executor on a struct that would wrap the current default one.

Something like (I didn't test it)

struct MyExecutor {
  inner: tmdb_api::client::reqwest::ReqwestExecutor,
}

#[async_trait::async_trait]
impl super::prelude::Executor for MyExecutor {
    async fn execute<T: serde::de::DeserializeOwned>(
        &self,
        url: &str,
        params: Vec<(&str, Cow<'_, str>)>,
    ) -> Result<T, crate::error::Error> {
        // before
        let result = self.inner.execute(url, params).await;
        // after
        result
    }
}