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

Feature/http requester external client #654

Open
wants to merge 5 commits into
base: v3
Choose a base branch
from

Conversation

skradel
Copy link

@skradel skradel commented Nov 6, 2019

Q A
Bug fix? no
New feature? yes
BC breaks? no
Need Doc update yes

Describe your change

This change adds an additional constructor to ApacheHttpRequester to lift the dependency creation out of the class's private implementation.

What problem is this fixing?

Managing the HttpClient dependency within ApacheHttpRequester precludes using an existing client; e.g., it may be desirable to use an instrumented instance or subclass of CloseableHttpAsyncClient already available within a framework.

@skradel skradel requested a review from aseure as a code owner November 6, 2019 23:31
@Ant-hem
Copy link
Member

Ant-hem commented Nov 7, 2019

Hi @skradel, Thanks for your contribution.

I understood your request, however I think the library already has a feature which is covering this use-case. One can inject his own managed instance of an httpClient within the configuration builder like this:

SearchConfig config =
new SearchConfig.Builder("YourApplicationID", "YourAdminAPIKey")
    .build();

HttpRequester myCustomRequester = new myCustomRequester();

SearchClient client = new SearchClient(config, myCustomRequester);

One requirement is: the requester should implement the following interface: HttpRequester. What you could do is to copy/paste the current implementation you just modified. If the solution I provided is not working for you, let us know and please detail your use-case so we could think on a solution.

@skradel
Copy link
Author

skradel commented Nov 7, 2019

Hi @Ant-hem - the reason for this modification is indeed to facilitate the use of using the HttpRequester as a parameter to SearchClient; however, the provided ApacheHttpRequester has dozens of disparate responsibilities (exception mapping, compression, composing requests, etc.) that implement an otherwise-invisible contract. Copy+paste of over 200 lines of source would create an ongoing requirement to review Algolia's implementation of that class on every point release thereafter. Short of refactoring the existing implementation into a set of single-responsibility classes, the option to supply its heavyweight dependency--the HttpClient--seems a good first step.

@Ant-hem
Copy link
Member

Ant-hem commented Nov 8, 2019

Thanks for your answer. I understand better your request now.

To achieve that, we would also need to overload DefaultSearchClient/DefaultInsightsClient/DefaultAnalyticsClient.create(..) methods to properly inject a CloseableHttpAsyncClient, because ApacheHttpRequester() constructors are not public. I have a good idea how we could do it in a clean way.

However, before going further I'll look if there would be any side-effects sharing the same CloseableHttpAsyncClient instance across multiple Search/Analytics/Clients instances.

Any concerns about it @aseure & @BenoitPerrot?

@skradel
Copy link
Author

skradel commented Nov 8, 2019

@Ant-hem I'd propose it would be okay to leave the Default*Clients as-is and direct users to construct (non-default) SearchClient / InsightsClient / etc. explicitly; however, I failed to notice that ApacheHttpRequester is marked final, which suggests that removing the final keyword and/or increasing ctor visibility to public would be possible cures. Alternately, consider shifting the utility methods (performRequestAsync, buildRequest, etc.) to an abstract superclass.

Interestingly ApacheHttpRequester only accesses its HttpClient in two places--to dispatch an async request, and on shutdown.

@Ant-hem
Copy link
Member

Ant-hem commented Nov 12, 2019

Thanks - I'll have a look in the coming days, I am pretty busy with another project ATM. I'll write you some requirements we would love for this enhancement.

@aseure
Copy link

aseure commented Jan 26, 2022

Hey team 👋 I hope you're doing well. Could you either close that PR or assign it to someone else. I don't work at Algolia anymore and I still have that PR to review in my assigned PR on Github. Thank you and all the best to you folks :)

@Ant-hem Ant-hem removed the request for review from aseure January 26, 2022 08:58
@skradel skradel requested a review from aallam as a code owner March 18, 2022 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants