-
Notifications
You must be signed in to change notification settings - Fork 124
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
Separate HTTP client from the Spotify client #186
Conversation
This is ready for discussion and review now |
Great job! LGTM, there is a small nitpick that is there any way to reduce the two kinds of wrapper functions or combine them into single wrapper function internally? it seems a little bit redundancy. |
Unfortunately no. We need wrappers for two different reasons: to request auth tokens and similars, and to perform a request to the Spotify API with said tokens. So the former can't include the auth header and only needs its spotify prefix appended, and the latter needs both the prefix and the header.
|
@@ -113,4 +132,103 @@ impl Spotify { | |||
url.to_string() | |||
} | |||
} | |||
|
|||
/// The headers required for authenticated requests to the API | |||
fn auth_headers(&self) -> ClientResult<Headers> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this function mark as inline
function as the below functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this one should be inline
. I only added that for wrapper functions, but this is an actual function. It contains more code than the wrappers and would be repeated a lot of times by being inline. Just like how endpoint_url
isn't inline
either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I get it.
So I've come to the conclusion that the HTTP client should be separate from the Spotify client, and be kept as one of its members instead. This completely separates the logic from the HTTP abstraction with the Spotify client and makes the code more modular and cleaner.
This means that, to implement the
get
,post
, and similar methods inside the http-specific modules (likehttp:reqwest
,http::ureq
), instead of doingimpl Spotify
directly, the implementation is forReqwestClient
orUreqClient
. The latter is then exported asHTTPClient
so that it can be used to perform requests. All these clients must implement the same trait as before,BaseClient
, which is now renamed asBaseHTTPClient
. This is just to make sure the interface exported byureq
andreqwest
is the same.What's now improved:
No need for this part in the Spotify client, which was hardcoded to be used later on only on the
http
module, a quite bad practice:This is now a generic HTTP struct, which is always included, be it reqwest or ureq. For the latter it will be empty because it doesn't really require an instance of its client, but it avoids the conditional compilation of this field:
Before, the reqwest and ureq implementations of the HTTP features were tied to what we needed to perform HTTP requests:
Now, the HTTP client doesn't know about the authentication process required for Spotify requests, and this has to be implemented in the Spotify client instead. Thus, this part is no longer repeated for each HTTP implementation (ureq, reqwest).
What's worse:
This requires implementing wrappers over the HTTP clients. We currently need two different wrappers:
get
,post
, and similars, which only append the Spotify url to the relative one. These are used for the authentication process, to request the token and similars.These wrappers aren't really necessary, but they guarantee we aren't forgetting to append the spotify url or the auth headers, while also reducing the endpoint boilerplate required (a goal in Meta-Issue #127).