-
Notifications
You must be signed in to change notification settings - Fork 15
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
Cache Control #55
Cache Control #55
Conversation
@@ -365,6 +366,32 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> { | |||
NetworkAgent.requestWillBeSent(requestIdCounter, options); | |||
} | |||
|
|||
let cacheControlBuilder = new okhttp3.CacheControl.Builder(); |
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.
We probably want more control right? Like where to store and the max size?
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.
I think we do. I will add the option to add the cache location and size.
@@ -377,6 +404,27 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> { | |||
// make the actual async call | |||
com.klippa.NativeScriptHTTP.Async.Http.MakeRequest(javaOptions, completeCallback, new java.lang.Integer(requestIdCounter)); | |||
|
|||
|
|||
//Temporary - Replace with cacheControlBuilder | |||
if (options.cachePolicy) { |
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.
Why is this necessary?
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.
This is not necessary, I will remove this. This was just a temporary add on to handle the cache post the request.
@@ -377,6 +404,27 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> { | |||
// make the actual async call | |||
com.klippa.NativeScriptHTTP.Async.Http.MakeRequest(javaOptions, completeCallback, new java.lang.Integer(requestIdCounter)); |
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.
Can't we add the CacheControl to the javaOptions?
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.
Yes, we can. I was not sure if you'll accept changes to the Async.java, so I just suggested the additional parameter way.
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.
Yeah, the java code is part of this repository and is an integral part of how the HTTP client works, it's also only used by this plugin, so you are free to make changes to it.
NOCACHE | ||
} | ||
|
||
export interface HttpsRequestOptions extends HttpRequestOptions { |
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.
I'm not sure if you can just rename this everywhere, won't this break support with the Core HTTP methods?
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.
I think we need to take a call on this one. If people are using this plugin then, probably they won't be using the native ones and there won't be a problem, and if they use the native ones and pass those options with this plugin then they will have everything except the cache control.
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.
Well the thing is that this plugin allows you to automatically replace all http calls with this module. To make that work, the functions and interfaces have to match the original, I suppose, we would have to check that.
@@ -130,6 +132,29 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> { | |||
|
|||
urlRequest.HTTPMethod = Utils.isDefined(options.method) ? options.method : GET; | |||
|
|||
if (options.cachePolicy) { |
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.
Do we also have something like the cache builder for iOS to have more control over it?
The code for the AAR is here: https://github.com/klippa-app/nativescript-http/blob/master/src/platforms/android/java/com/klippa/NativeScriptHTTP/Async.java You can directly edit that code and NativeScript should re-generate the AAR. |
PR Checklist
What is the current behavior?
No Cache Control.
What is the new behavior?
Ability to control Cache for HTTP Requests.
Fixes/Implements/Closes #54.
Hi @jerbob92 ,
I tried implementing cache control in the plugin and it works partially. For iOS I was able to directly set Cache Policy as urlRequest.cachePolicy = NSURLRequestCachePolicy.UseProtocolCachePolicy; or whatever.
But for Android I noticed that you are making the request with the AAR created by you and not directly with okhttp3.
I thought of rewriting the
request
function for android of the plugin with OkHttp. However before I start that, I thought of getting in touch with you for a possibility to add cache control parameter tocom.klippa.NativeScriptHTTP.Async.Http.MakeRequest(requestOptions, callbackComplete, requestId)
Currently there are 3 parameters in MakeRequest
We can add in a fourth Parameter
CacheControlPolicy
which will be added to theokhttp request
.You must be building a request of
okHttp
inside theMakeRequest
function similar tovar request = new okhttp3.Request.Builder();
we can just add the cache control builder to that request to make it workrequest.cacheControl(cacheControlPolicy);
I have committed my changes to the forked repository here.
Please have a look at the
request
function inhttp.ios.ts
andhttp.android.ts
and let me know.