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

Provide getter for OkHttpClient and method to provide one to the SquareClient.Builder #3

Open
finci-square opened this issue Feb 20, 2020 · 9 comments

Comments

@finci-square
Copy link

finci-square commented Feb 20, 2020

Two reasons:

  1. OkHttpClients are intended to be shared app wide, so, if you're using OkHttp for other HTTP api's you would want to share the OkHttp client instance. (read: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/). To facilitate, I think we need a builder method to provide it and a getter on the SquareClient.
  2. I want to customize the client, like, for example, applying an interceptor to capture response time metrics: https://square.github.io/okhttp/interceptors/
@finci-square finci-square changed the title Provide getter for okhttp client and builder method to provide your own okhttp client Provide getter for OkHttpClient and method to provide one to the SquareClient.Builder Feb 20, 2020
@jessdelacruzsantos
Copy link
Contributor

Hello, it is in our future road map allow users to configure pass in their own http client. Until then, I am sorry to say your app might have to operate with two OkHttpClients.

@finci-square
Copy link
Author

finci-square commented Feb 24, 2020

ah so I discovered you can aggressively shutdown the underlying OkHttpClient instance by calling SquareClient.shutdown();, so at least that partially fixes my problem. Still would be better to have both a means to provide the client to the builder and get the OkHttpClient from the SquareClient instance

@finci-square
Copy link
Author

finci-square commented Apr 6, 2020

just hit another use case where I need the client- I would like to register an interceptor on the client created by the SDK so I can capture response time metrics as described here: https://square.github.io/okhttp/interceptors/

@sseaman
Copy link

sseaman commented Apr 14, 2020

@jfinci Thought the SquareClient is final, does wrapping it so you can do your interception work for you? You could also use AspectJ to intercept calls. Not sure if that gets you what you want, but it might.

We do have a task on our roadmap to support providing your own HTTP client, but we can't commit to a date at this time.

@finci-square
Copy link
Author

I've implemented my own Configuration to replace SquareClient and implemented my own HttpClient to replace the OkClient, which, unfortunately, requires some copy pasta and will make upgrades harder

@sseaman
Copy link

sseaman commented Jul 16, 2020

We now have interfaces for these objects with the intent of allowing developers to implement whatever they so choose. This should allow more flexibility and customization.

@sseaman sseaman closed this as completed Jul 16, 2020
@finci-square
Copy link
Author

I think this needs to be reopened. Setup a meeting with me if you want to discuss more. Its great that you have a interface, but, I don't want to reimplement the entire OkClient- I only want to provide by my OkHttpClient instance so that I have a single instance in my app for both Square and non-square API's. So i end up copy-pasta 500 lines of code from the OkClient into my own MyOkClient and omitting the methods for new-ing a OkHttpClient which is still not ideal. Your offering flexibility at the cost of significantly more over head when the use case is clear and easy to solve for.

@finci-square
Copy link
Author

In order to use my own client i also need to copy pasta ALL of https://github.com/square/square-java-sdk/blob/master/src/main/java/com/squareup/square/SquareClient.java to my own MySquareClient which is again really unfortunate

@zenmasterjobo
Copy link

hey @finci-square

I've been told that our SDK supports this now, and you could achieve it by doing the following.

SquareClient client = new SquareClient.Builder()
.httpClientConfig(configBuilder -> configBuilder.httpClientInstance(clientInstance))
.build();

you can then get the method from the square client with

okhttp3.OkHttpClient clientInstance = client.getHttpClientConfig().getHttpClientInstance();

Let me know if that solves the issue, and we can close this ticket

Cheers!

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

No branches or pull requests

4 participants