refactor: improve BlockingLoreAPI errors and remove its unwraps #54
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR was built over
lorenzo/use-mockall-lib
(#45) to avoid dealing with conflicts after it's merge, but they have no direct relation so you can ignore 38e6b14With that being said, this is another PR which does not include any new functionality, it aims to improve general code quality by removing repetitive code and unnecessary unwraps mostly in
src/lore_api_client.rs
. The improvements are as follows:ClientError
) for all the client traits (and consequently for the client itself)I thought it didn't make much sense to have a specific error to each trait, since all of them were focused on handling request errors (expect for
EndOfFeed
). Besides, by using thethiserror
create we can convert errors easily, allowingClientError
to capture allrewquest:Error
s and convert it into aClientError
. Also there were also unnecessary unwraps that could be removed by simply using?
.It was also another point I thought could be improved, since all trait implementations had the same steps to get a response body for given target url.
reqwest::blocking::get
directlyAccording to
reqwest::blocking::get
docs, this method should be avoided since it creates a single client for each of the requests. Even though this will probably not have noticeable changes in performance, I thought it was better to makerewquest::blocking::Client
an attribute ofBlockingAPIClient
so that all requests can use the same HTTP client.Regarding
EndOfFeed
, I struggled to decide if it made more sense to implement it inLoreSession
or in the API client. In the end I decided to keep it where it was.Lastly, I considered using
Arc
aroundBlockingAPIClient
, but I realized this only makes a difference in multi-threaded contexts. I don't know if it could help resolve #3, but since I couldn't reproduce this flaky behavior I don't know if it persists.EDIT: After reading about Rc (the single-threaded equivalent of Arc) and the reqwest client, I found that it can be used to share the same client across structs that need the client. That said, simply cloning the client is also an option since it internally reuses underlying resources, so it doesn't recreate all pools, connections, etc., with every clone. Both solutions are likely better than creating a new client each time, so I will add another commit to implement this. As always, let me know what you think.
Helps #28