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

ObsoleteUrlFactory overrides OkHttpClient’s interceptors #1262

Closed
Typraeurion opened this issue Oct 4, 2021 · 2 comments
Closed

ObsoleteUrlFactory overrides OkHttpClient’s interceptors #1262

Typraeurion opened this issue Oct 4, 2021 · 2 comments

Comments

@Typraeurion
Copy link

Describe the bug
I’ve been troubleshooting why my custom network interceptor that I added to the OkHttpClient was never called. After tracing through the code, I found that ObsoleteUrlFactory.buildCall is explicitly removing all interceptors set on the OkHttpClient and adding its own:

            OkHttpClient.Builder clientBuilder = client.newBuilder();
            clientBuilder.interceptors().clear();
            clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
            clientBuilder.networkInterceptors().clear();
            clientBuilder.networkInterceptors().add(networkInterceptor);

To Reproduce
Steps to reproduce the behavior:

    public class TimingInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            System.out.println("Intercepting network call from OkHttp");
        }

    }

    public GHUser getCurrentUser(String authToken)
            throws ForbiddenException, IOException {

        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        clientBuilder.addNetworkInterceptor(new TimingInterceptor());
        OkHttpConnector httpConnector = new OkHttpConnector(clientBuilder.build());
        GitHub gitHub = new GitHubBuilder()
                .withConnector(httpConnector)
                .withOAuthToken(authToken)
                .build();
        GHMyself me = gitHub.getMyself();

        return me;

    }

Expected behavior
Application displays “Intercepting network call from OkHttp” when getCurrentUser is called.

Desktop (please complete the following information):

  • Java: 1.8.0
  • github-api: 1.333
  • okhttp: 4.9.2 (previously tried 3.14.9, with the same result)
@bitwiseman
Copy link
Member

bitwiseman commented Oct 4, 2021

Generally speaking, this is a known limitation of ObsoleteUrlFactory. This is another reason we need to implement #1085 so we can stop using the deprecated/obsolete code path.

You are welcome to take a swing at implementing a fix in ObsoleteUrlFactory, but I suspect there was a good reason why the original implementation does not allow that. If you choose to try fixing you will need to implement extensive testing in that area.

If you are interested in working on #1085, let's talk over there.

@bitwiseman
Copy link
Member

bitwiseman commented Nov 10, 2021

@Typraeurion
We won't be fixing this in ObsoleteUrlFactory. In the next release (code already merged to main) we've added OkHttpGitHubConnector which does not depend on ObsoleteUrlFactory allowing for use of interceptors.

If you are using OkHttpConnector switch to OkHttpGitHubConnector.

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

No branches or pull requests

2 participants