-
-
Notifications
You must be signed in to change notification settings - Fork 56
Convert to HttpURLConnection/OkHttp #12
Convert to HttpURLConnection/OkHttp #12
Conversation
By analyzing the blame information on this pull request, we identified @wangmengyan95 to be a potential reviewer. |
This looks super good! Just a few nits 😄 It also looks like you've resolved #7 with this as well! |
@@ -38,7 +38,7 @@ public DefaultOAuthProvider(String requestTokenEndpointUrl, String accessTokenEn | |||
|
|||
protected HttpRequest createRequest(String endpointUrl) throws MalformedURLException, | |||
IOException { | |||
HttpURLConnection connection = (HttpURLConnection) new URL(endpointUrl).openConnection(); | |||
HttpURLConnection connection = HttpURLConnectionProvider.getInstance().open(new URL(endpointUrl)); |
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.
Let's pass in a HttpURLConnectionProvider
instance through the constructor to match the CommonsHttpOAuthProvider
.
@zefei updated the pull request. |
private Object okUrlFactory = null; | ||
private Method okUrlFactoryOpen = null; | ||
|
||
protected HttpURLConnectionClient() { |
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.
Might as well make this private
Passing back for revision. I'm ok with the 4 spaces in that file since it matches the rest of that package. |
@zefei updated the pull request. |
Everything looks good! Could you also remove this line since we won't be needing Lastly, it'd be great if we didn't use reflection to utilize OkHttp, so we can rely on compiler errors if any OkHttp APIs change (they shouldn't, but just in case). We have an example on how we did it in our SDK that you can follow and you'll also want to add |
I don't think we can remove apache because Twitter.signRequest is public and allows apache request to be passed. |
When I was trying to use |
Good call!
As long as we don't pull any classes into the runtime that directly import anything from OkHttp before we know OkHttp exists in the runtime, we should be fine. The link I sent you is an example of this and you should be able to test a working implementation by adding/removing OkHttp from your application that depends on ParseTwitterUtils. If you don't have time for this part, feel free to just squash your commits and I can merge. |
I'm still not sure that this is safe, please see this: http://stackoverflow.com/questions/31319953/error-parse-1-9-3-and-okhttp-2-0-0 Even though OkHttp exists in runtime, it could be an incompatible version that satisfies the symbol lookup, but still fails actual signature matching/lookup. Since this is basically runtime dispatching, I can't think of any ways to avoid runtime error without reflection. |
I agree that your solution is safer in the sense that it'll "work" regardless of a breaking change in OkHttp as this library will fall back to HttpURLConnection, however "working" is relative. It won't crash when there's a version mismatch between ParseTwitterUtils and OkHttp, but it won't work as intended either. It would silently fail the SDK would go on unnoticed incorrectly using HttpURLConnection instead of OkHttp even if it existed in the runtime. If we were to fail hard in the case of a breaking OkHttp change, a developer might be burdened to using a older version of OkHttp until this library has been updated but they wouldn't release a "bad" build. There might be an odd case where a developer requires an older version of a library, but we can add some specific checking for the newer API in I'd love to hear your thoughts if you still feel strongly about your opinion on this. |
I agree that falling back silently is bad, I will change how it's handled. But I'm still in favor of reflection. I don't have a strong opinion though as this is a trade off. My thinking is: the nature of this is dynamic dispatching, which is hard to handle at compile time. If we use |
- use DefaultOAuthConsumer/Provider instead of CommonsHttpOauthConsumer/Provider - overloaded Twitter.signRequest to accept HttpURLConnection - use OkHttp instead of HttpURLConnection if OkHttp exists in user's classpath
7c61e40
to
5cd3788
Compare
@zefei updated the pull request. |
Yeah, there's no silver bullet here with regards to working with other APIs. Thanks for putting in the effort for implementing this functionality! |
Convert to HttpURLConnection/OkHttp
Thank you for the very detailed reviews and guidance! |
Fix #9
Test:
HttpURLConnection and OkHttp both work in different dependency settings