-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Split OkHttpNetworkFetcher's fetch method to make part of it reusable #1459
Conversation
@@ -256,6 +262,18 @@ public ImageRequestBuilder setPostprocessor(Postprocessor postprocessor) { | |||
return mPostprocessor; | |||
} | |||
|
|||
/** Sets the HTTP headers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @rigdern.
We just need to add to this doc (and maybe where you declare mHeaders
too) to make it clear that support for these added headers is dependent on the NetworkFetcher
being used.
Ideally we'd add support to the basic HttpUrlConnectionNetworkFetcher
too but I can live without that as long as this is well enough documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kriwan, thanks for the feedback! I added some comments to document this.
@rigdern updated the pull request - view changes |
9c5ca1d
to
5eaf11d
Compare
@rigdern updated the pull request - view changes |
The doc you've added wasn't quite what I meant, which was that the headers are only supported if you use a That said I don't want to delay this over a doc which I can fix myself - particularly as we have a new release coming which this could be included in - so I'm going to import it then make the slight change myself. @facebook-github-bot import |
Thanks for importing. If you are an FB employee go to Phabricator to review internal test results. |
Thanks, @kirwan. Sorry, I wrote this change a while ago and I forgot that |
Any chance of getting this into the project? It would be very useful. |
Thanks again for the pull request. While the changes certainly would unblock the React Native feature, I do not feel too comfortable adding HTTP headers to the First, the HTTP headers would only apply to a certain URI scheme (http) and is not relevant e.g. for local file requests. After adding headers, we might also want to add support for auth tokens in cookies and so on leading to some confusing parts in the core api. Second, sending tokens from a higher level seems to be a specific use case due to the nature of React Native. Most clients would use an OkHttp interceptor (see code below [1]) to solve this. Passing down auth tokens from the UI would look like an anti-pattern there. Finally, so far the URI was able to fully describe the source of the content. If the image might change depending on the used headers, this would require us to rethink cache keys. ProposalsNevertheless, I see that this is crucial for writing applications in React Native that require auth headers. I'd like to propose the following alternatives: (1) We find a way to leverage the OkHttp3 interceptor in ReactNative. Frankly, I don't know how feasible this is to do. However, the big benefit would be that one can properly do the rewriting of all request parameters. In addition, client logic could handle a (2) In the React Native Android code, we create a custom (3) Refactor the Fresco code to have a I'm looking forward to hear what you think about these ideas :) Maybe @oprisnik or @kirwan also chime in. Personally, I feel that (2) would be something that we can get working quite soon. I'm more than happy to help you working on those so that we get them in soon. Cheers, Code attachments[1] Best-practice of adding auth headers using interceptor of okhttp
|
Thanks for the details. I agree that option 2 sounds best. Are you considering that the user could provide an optional "NetworkFetcher" from their JS code in the form of a fetch request? From there they are free to provide custom headers and cache keys. I don't fully understand option 3 - how would it manifest in the client code? |
@lambdapioneer, thanks for the feedback. Option (2) sounds very similar to the change I originally submitted to the react-native repo (rigdern/react-native@2743ba2) but in this comment @dmmiller suggested this alternative approach of putting part of the change into Fresco. @dmmiller do you have any thoughts on @lambdapioneer proposals? |
I am fine with (2). The fresco team certainly knows better if this doesn't really belong in Fresco and if there is a better way to put it in. I suggested it since it looked like just a copy/paste + a new method in our code. If we can do it through inheritance that might be better. |
Based on @dmmiller's response, I will continue with (2). @lambdapioneer, can you provide feedback on my implementation of (2) at rigdern/react-native@2743ba2? |
@rigdern, I've added my comments to the implementation at rigdern/react-native@2743ba2. Looks very good and happy to support from Fresco side |
Subclasses overriding the fetch method might want to reuse some of its functionality. This change splits the fetch method to make this reusability possible.
5eaf11d
to
9aa1e75
Compare
@rigdern updated the pull request - view changes - changes since last import |
@lambdapioneer Thank you for your suggestions. I applied them and updated the Fresco and React Native PRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, just two small style remarks :)
Will already import and then update with when the code style is updated
.url(uri.toString()) | ||
.get() | ||
.build(); | ||
protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move this method below the other public
methods and re-format:
protected void fetchWithRequest(
final OkHttpNetworkFetchState fetchState,
final Callback callback,
final Request request) {
call, | ||
new IOException("Unexpected HTTP code " + response), | ||
callback); | ||
call, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
- Moved protected method fetchWithRequest to be below the public methods - Reformated fetchWithRequest to put each argument in its signature on a separate line.
@rigdern updated the pull request - view changes - changes since last import |
@lambdapioneer I made the styling tweaks you requested. I also retitled the PR to better match the approach we're now taking. |
Subclasses overriding the
fetch
method might want to reuse some of its functionality. This change splits thefetch
method into 2 methods to make this reusability possible:fetch
andfetchWithRequest
.This Fresco change will be consumed by React Native PR facebook/react-native#7791 which enables React Native apps to specify headers to include in the HTTP request when fetching a remote image. For example, one might leverage this when fetching an image from an endpoint that requires authentication.
Adam Comella
Microsoft Corp.