question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow pluggable OkHttp client

See original GitHub issue

The auth0 client internally uses OkHttp as its http client. Quickly skimming the codebase, it looks like the API is designed to abstract/hide this from the SDK consumer.

The problem

However, this makes it difficult to re-use an existing OkHttp client (for example, a singleton OkHttp bean in a spring boot project). Sharing a single okhttp client is the recommended approach according to the OkHttp docs: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#okhttpclients-should-be-shared

Ideal Solution

Ideally, the constructors would accept a pre-existing OkHttp client. For ex:

public AuthAPI(..., OkHttpClient client) {
  ...
}

It could still initialize the client with its own configuration with newBuilder(): https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#customize-your-client-with-newbuilder

Alternatives and current work-arounds

There are no current work-arounds apart from forking the SDK to add this constructor.

I’m happy to take up the implementation of this if it’s something the maintainers are open to merging!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jimmyjamescommented, Mar 10, 2021

While I do think allowing for a pluggable HTTP client is something this library should support, I don’t think exposing the OkHttp dependency directly is the best solution. It couples this library more/too closely with a specific Http client, which could lead to issues with versioning/configuration (see #324 for an example of how upgrading to OkHttp v4 negatively impacted users using Spring’s dependency management), along with leaking too much dependency details as discussed above.

I think a more optimal solution is to provide a generic networking client interface for which we can provide a default implementation, yet also allow clients to provide an implementation providing their own networking client if desired. That’s something that we are considering for the next major version release.

If there are additional networking configurations required in v1, HttpOptions could be modified within the v1 stream to support that, for which we can address via issues/PRs.

0reactions
gsdattacommented, Oct 27, 2020

@jimmyjames no specific client configurations – I was just looking to reuse a OkHttpClient which I would pass in to the SDK instead of having the SDK instantiate one on its own (since there are ramifications to that).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Interceptors in OkHTTP - Baeldung
As the name suggests, interceptors are pluggable Java components that we can use to intercept and process requests before they are sent to...
Read more >
OkHttpClient - OkHttp - Square Open Source
This builds a client that shares the same connection pool, thread pools, and configuration. Use the builder methods to add configuration to the...
Read more >
Okhttp with different configuration - Stack Overflow
This example ServiceInterceptor.java checks each request and decides how to authenticate in a pluggable manner.
Read more >
Pluggable HTTP Modules with the Azure SDK for Java
Selecting OkHttp as the HTTP transport​​ Instead, the implementation is provided by a dependency. In the default case, that is azure-core-http- ...
Read more >
How to use OkHttp 3 with Retrofit 1 - Future Studio
addNetworkInterceptor(/** Your Network Interceptor **/) httpBuilder.authenticator(/** Your Authenticator **/) // create the OkHttpClient ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found