square / square-java-sdk

Java client library for the Square API
https://developer.squareup.com
Other
59 stars 29 forks source link

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

Open finci-square opened 4 years ago

finci-square commented 4 years ago

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/

jessdelacruzsantos commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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.

finci-square commented 3 years ago

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 commented 3 years ago

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 commented 6 months ago

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!