square / square-java-sdk

Java client library for the Square API

Home Page:https://developer.squareup.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

finci-square opened this issue · comments

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/

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.

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

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/

@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.

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

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.

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.

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

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!