Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization of reqwest client #33

Open
torfsen opened this issue Aug 4, 2021 · 3 comments
Open

Allow customization of reqwest client #33

torfsen opened this issue Aug 4, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@torfsen
Copy link
Contributor

torfsen commented Aug 4, 2021

Currently, roux::Subreddit uses reqwest::Client with its default settings, which might not be applicable to all use cases. For example, the default Client does not have any timeouts, so if connectivity is lost then requests simply hang indefinitely.

I'd like the possibility to supply my own Client instance when creating a Subreddit instance.

@beanpuppy
Copy link
Collaborator

Thanks for the issue, you raise a good point. But I'm a little wary of allowing people to supply their own clients because I feel that it would go against the "idea" of a wrapper which is to try to keep people away from the lower level details like this and not have to worry about any other libraries (maybe except Tokio, but that's almost required when using async Rust).

Instead, what's your opinion on passing a config struct to roux which will build the client for you? I think this is better because it would prevent you from having to manually set the things roux does like auth headers (I know Subreddit doesn't to this but I've just been slow at adding oauth to it) and makes it less likely for people to mess it up.

@beanpuppy beanpuppy added the enhancement New feature or request label Aug 6, 2021
@torfsen
Copy link
Contributor Author

torfsen commented Aug 9, 2021

I don't think there is "the one" right approach of supporting customization here, and my suggestion of providing a custom Client instance was simply my first idea. Your point of hiding most of the complexity and the 3rd-party dependencies are very valid.

I'm not that familiar with the reqwest API in general, so I don't know whether it makes sense to replicate reqwest's config options in the roux API -- it might just be a lot of work that might could be spend more benefically elsewhere.

In any case, I guess it would be a feature for more advanced users, so I do think a little complexity is OK as long as there's the regular old "it just works" API that you currently have.

And perhaps there is even a middle way, for example the possibility to create your own Client instance but using roux utility functions for some of it, or roux adjusting the settings of a user-provided Client.

To be honest, I don't really care too much about the precise details, and I trust that you as the library author are probably in the best position to judge which approach is most appropriate.

@akarras
Copy link
Contributor

akarras commented Aug 10, 2021

I think my preferred method would be to allow the user to supply their own client, or maybe they could just configure it using a closure.
Something like

pub use reqwest::ClientBuilder as ReqwestBuilder;

impl Reddit {
   /// Creates a `Reddit` instance with user_agent, client_id, and client_secret, and
    /// provides a ClientBuilder that allows the user to configure the inner request client.
    pub fn new_with_options<F>(
        user_agent: &str,
        client_id: &str,
        client_secret: &str,
        options: F,
    ) -> Result<Reddit, RouxError>
    where
        F: FnOnce(ReqwestBuilder) -> ReqwestBuilder,
    {
        Ok(Reddit {
            config: config::Config::new(user_agent, client_id, client_secret),
            client: options(Client::builder()).build()?,
        })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants