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

Add back ability to customise OkHttp client #17237

Closed
wants to merge 1 commit into from

Conversation

cdlewis
Copy link
Contributor

@cdlewis cdlewis commented Dec 16, 2017

Motivation

Prior to 0a71f48, users could customise the OkHttp client used by React Native on Android by calling replaceOkHttpClient in OkHttpClientProvider.

This functionality has a variety of legitimate applications from changing connection timeouts or pool size to Stetho integration. The challenge is to add back support for replacing the client without causing a breaking change or reintroducing the problems @olegbl sought to address in his original commit.

Introducing a client factory archives these aims, it adds a new, backwards compatible interface and is called each time a client is requested rather than re-using the same instance (unless you explicitly want this behaviour, in which case you could replicate it using a static class property inside your custom factory).

A number of PRs have been opened to add this functionality: #14675, #14068.

I don't have a lot of Java experience so I'm open to better/more idiomatic ways to achieve this :)

Test Plan

Create React Native application and set a custom factory in the constructor, e.g. OkHttpClientProvider.setOkHttpClientFactory(new CustomNetworkModule());

Where a custom factory would look like:

class CustomNetworkModule implements OkHttpClientFactory {
    public OkHttpClient createNewNetworkModuleClient() {
        return new OkHttpClient.Builder().build();
    }
}

Related PRs

Remove the existing replace client method to prevent accident use and alert existing users that its functionality has changed: #16972

Release Notes

[Android] [Minor] [Networking] - | Provide interface for customising the OkHttp client used by React Native |

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 16, 2017
@facebook-github-bot
Copy link
Contributor

@cdlewis I tried to find reviewers for this pull request and wanted to ping them to take another look. However, based on the blame information for the files in this pull request I couldn't find any reviewers. This sometimes happens when the files in the pull request are new or don't exist on master anymore. Is this pull request still relevant? If yes could you please rebase? In case you know who has context on this code feel free to mention them in a comment (one person is fine). Thanks for reading and hope you will continue contributing to the project.

@cdlewis
Copy link
Contributor Author

cdlewis commented Jan 15, 2018

@hramos and @mkonicek have been reviewers on similar PRs.

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Jan 29, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hramos is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hramos is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

I tried to merge this pull request into the Facebook internal repo but some checks failed. To unblock yourself please check the following: Does this pull request pass all open source tests on GitHub? If not please fix those. Does the code still apply cleanly on top of GitHub master? If not can please rebase. In all other cases this means some internal test failed, for example a part of a fb app won't work with this pull request. I've added the Import Failed label to this pull request so it is easy for someone at fb to find the pull request and check what failed. If you don't see anyone comment in a few days feel free to comment mentioning one of the core contributors to the project so they get a notification.

@facebook-github-bot facebook-github-bot added cla signed and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Jan 30, 2018
Copy link
Contributor

@hramos hramos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rebase? We just need to move this past a problematic one week old commit before we can import it.

Prior to 0a71f48, users could customise
the OkHttp client used by React Native on Android by calling
replaceOkHttpClient in OkHttpClientProvider.

This functionality has a variety of legitimate applications from
changing connection timeouts or pool size to Stetho integration. The
challenge is to add back support for replacing the client without
causing a breaking change or undoing the problems @olegbl sought to
address in his original commit. Introducing a client factory achives
these aims, it adds a new, backwards compatible interface and is called
each time a client is requested rather than re-using the same instance
(unless you explicitly want this behaviour, in which case you could
replicate it using a static class property inside your custom factory).
@cdlewis cdlewis force-pushed the clewis_okhttpclientfactory branch from 26d0416 to f6ea205 Compare January 30, 2018 18:32
@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Jan 30, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hramos is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

jsworx pushed a commit to jsworx/react-native that referenced this pull request Feb 1, 2018
Summary:
Prior to 0a71f48, users could customise the OkHttp client used by React Native on Android by calling replaceOkHttpClient in OkHttpClientProvider.

This functionality has a variety of legitimate applications from changing connection timeouts or pool size to Stetho integration. The challenge is to add back support for replacing the client without causing a breaking change or reintroducing the problems olegbl sought to address in his original commit.

Introducing a client factory archives these aims, it adds a new, backwards compatible interface and is called each time a client is requested rather than re-using the same instance (unless you explicitly want this behaviour, in which case you could replicate it using a static class property inside your custom factory).

A number of PRs have been opened to add this functionality: facebook#14675, facebook#14068.

I don't have a lot of Java experience so I'm open to better/more idiomatic ways to achieve this :)

Create React Native application and set a custom factory in the constructor, e.g.  `OkHttpClientProvider.setOkHttpClientFactory(new CustomNetworkModule());`

Where a custom factory would look like:

```
class CustomNetworkModule implements OkHttpClientFactory {
    public OkHttpClient createNewNetworkModuleClient() {
        return new OkHttpClient.Builder().build();
    }
}
```

Remove the existing replace client method to prevent accident use and alert existing users that its functionality has changed: facebook#16972

[Android] [Minor] [Networking] - | Provide interface for customising the OkHttp client used by React Native |
Closes facebook#17237

Differential Revision: D6837734

Pulled By: hramos

fbshipit-source-id: 81e63df7716e6f9039ea12e99233f6336c6dd7ef
Plo4ox pushed a commit to Plo4ox/react-native that referenced this pull request Feb 17, 2018
Summary:
Prior to 0a71f48, users could customise the OkHttp client used by React Native on Android by calling replaceOkHttpClient in OkHttpClientProvider.

This functionality has a variety of legitimate applications from changing connection timeouts or pool size to Stetho integration. The challenge is to add back support for replacing the client without causing a breaking change or reintroducing the problems olegbl sought to address in his original commit.

Introducing a client factory archives these aims, it adds a new, backwards compatible interface and is called each time a client is requested rather than re-using the same instance (unless you explicitly want this behaviour, in which case you could replicate it using a static class property inside your custom factory).

A number of PRs have been opened to add this functionality: facebook#14675, facebook#14068.

I don't have a lot of Java experience so I'm open to better/more idiomatic ways to achieve this :)

Create React Native application and set a custom factory in the constructor, e.g.  `OkHttpClientProvider.setOkHttpClientFactory(new CustomNetworkModule());`

Where a custom factory would look like:

```
class CustomNetworkModule implements OkHttpClientFactory {
    public OkHttpClient createNewNetworkModuleClient() {
        return new OkHttpClient.Builder().build();
    }
}
```

Remove the existing replace client method to prevent accident use and alert existing users that its functionality has changed: facebook#16972

[Android] [Minor] [Networking] - | Provide interface for customising the OkHttp client used by React Native |
Closes facebook#17237

Differential Revision: D6837734

Pulled By: hramos

fbshipit-source-id: 81e63df7716e6f9039ea12e99233f6336c6dd7ef
@denisk20
Copy link

@cdlewis I'm trying to use this but I'm getting an error if I add the

OkHttpClientProvider.setOkHttpClientFactory(new PinningOkHttpClientFactory());

into my Application.onCreate(). Removing the line fixes the error.

The error:

java.lang.ClassCastException: okhttp3.CookieJar$1 cannot be cast to com.facebook.react.modules.network.CookieJarContainer
    at com.facebook.react.modules.network.NetworkingModule.<init>(NetworkingModule.java:141)
    at com.facebook.react.modules.network.NetworkingModule.<init>(NetworkingModule.java:164)
    at com.facebook.react.shell.MainReactPackage$18.get(MainReactPackage.java:245)
    at com.facebook.react.shell.MainReactPackage$18.get(MainReactPackage.java:242)
    at com.facebook.react.LazyReactPackage.createNativeModules(LazyReactPackage.java:79)
    at com.facebook.react.NativeModuleRegistryBuilder.processPackage(NativeModuleRegistryBuilder.java:106)
    at com.facebook.react.ReactInstanceManager.processPackage(ReactInstanceManager.java:1172)
    at com.facebook.react.ReactInstanceManager.processPackages(ReactInstanceManager.java:1142)
    at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1080)
    at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:113)
    at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:911)
    at java.lang.Thread.run(Thread.java:764)

facebook-github-bot pushed a commit that referenced this pull request Jan 28, 2019
Summary:
This method was originally intended to replace the OkHttp client used by React Native's networking library. However it has effectively been a noop since 0a71f48#diff-177100ae5a977e4060b54cc2b34c79a7, when the Networking library was modified to create a new client rather than use the reference provided by OkHttpClientProvider.

Leaving this code in place is dangerous. There is no indication to users upgrading React Native that the method is no longer replacing the OkHttpClient used by the Networking library. Any functionality reliant on overriding the client will silently break. This caused us some problems internally.

There's been a PR out for some time that seeks to reintroduce this functionality: #14068

I've also put up a new PR that adds an interface for replacing the client without introducing breaking changes: #17237

Do our unit tests continue to pass? Should be safe as this method is not used anywhere inside React Native.

[ANDROID] [BREAKING] [Networking] - removed replaceOkHttpClient method in OkHttpClientProvider.
Pull Request resolved: #16972

Differential Revision: D13838805

Pulled By: cpojer

fbshipit-source-id: 43606d1d141afb9b5dda4dd64e5ac5448771b45c
matt-oakes pushed a commit to matt-oakes/react-native that referenced this pull request Feb 7, 2019
Summary:
This method was originally intended to replace the OkHttp client used by React Native's networking library. However it has effectively been a noop since facebook@0a71f48#diff-177100ae5a977e4060b54cc2b34c79a7, when the Networking library was modified to create a new client rather than use the reference provided by OkHttpClientProvider.

Leaving this code in place is dangerous. There is no indication to users upgrading React Native that the method is no longer replacing the OkHttpClient used by the Networking library. Any functionality reliant on overriding the client will silently break. This caused us some problems internally.

There's been a PR out for some time that seeks to reintroduce this functionality: facebook#14068

I've also put up a new PR that adds an interface for replacing the client without introducing breaking changes: facebook#17237

Do our unit tests continue to pass? Should be safe as this method is not used anywhere inside React Native.

[ANDROID] [BREAKING] [Networking] - removed replaceOkHttpClient method in OkHttpClientProvider.
Pull Request resolved: facebook#16972

Differential Revision: D13838805

Pulled By: cpojer

fbshipit-source-id: 43606d1d141afb9b5dda4dd64e5ac5448771b45c
@hramos hramos added the Merged This PR has been merged. label Mar 8, 2019
@react-native-bot react-native-bot removed Import Started This pull request has been imported. This does not imply the PR has been approved. Import Failed labels Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants