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

Cache Control #55

Closed
wants to merge 3 commits into from
Closed

Cache Control #55

wants to merge 3 commits into from

Conversation

pradumnk-mahanta
Copy link

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 to com.klippa.NativeScriptHTTP.Async.Http.MakeRequest(requestOptions, callbackComplete, requestId)

Currently there are 3 parameters in MakeRequest

RequestOptions
CallbackComplete
Request Id Counter

We can add in a fourth Parameter CacheControlPolicy which will be added to the okhttp request.

You must be building a request of okHttp inside the MakeRequest function similar to var request = new okhttp3.Request.Builder(); we can just add the cache control builder to that request to make it work request.cacheControl(cacheControlPolicy);

I have committed my changes to the forked repository here.

Please have a look at the request function in http.ios.ts and http.android.ts and let me know.

@@ -365,6 +366,32 @@ export function request(options: HttpRequestOptions): Promise<HttpResponse> {
NetworkAgent.requestWillBeSent(requestIdCounter, options);
}

let cacheControlBuilder = new okhttp3.CacheControl.Builder();
Copy link
Contributor

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?

Copy link
Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Author

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));
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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) {
Copy link
Contributor

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?

@jerbob92
Copy link
Contributor

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.

@jerbob92 jerbob92 closed this Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Review Request] Roadmap Step - Cache Control
2 participants