Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Extend chcp.fetchUpdate method to accept config-url and additional http headers #153

Closed
nikDemyankov opened this issue May 9, 2016 · 19 comments

Comments

@nikDemyankov
Copy link
Member

nikDemyankov commented May 9, 2016

Right now chcp.fecthUpdate method can accept only callback method. It would be nice to be able to pass additional data to it. For example:

var options = {
  'config-file': 'https://mydomain.com/chcp.json',
  'request-headers': {
    'foo': 'bar'
  }
};

chcp.fetchUpdate(function (error, data) {
}, options);

where:

  • config-file - url of the chcp.json config on the server. Right now you need to use chcp.configure() to set it before fetching the update. If implemented - we don't need to do this anymore.
  • request-headers - additional http headers, that we can set from JS side. For example, auth headers.

If you are native developer and want to use an embedded web views - you can define fetch update preferences from the native side. For that you need to obtain the instance of the plugin, that is initialised for your web view - and set default fetch update options property as described below.

iOS:

NSURL *chcpJsonfileURL = [NSURL URLWithString:@"http://somedomain.com/path/chcp.json"];
NSDictionary<NSString *, NSString *> *requestHeaders = @{@"foo": @"bar"};

HCPFetchUpdateOptions *options = [[HCPFetchUpdateOptions alloc] initWithConfigURL:chcpJsonFileURL requestHeaders:requestHeaders];
chcpPluginInstance.defaultFetchUpdateOptions = options;

Android:

final String chcpJsonFileURL = "http://somedomain.com/path/chcp.json";
final Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("foo", "bar");
final FetchUpdateOptions options = new FetchUpdateOptions(chcpJsonFileURL, requestHeaders);

chcpPluginInstance.setDefaultFetchUpdateOptions(options);
@nikDemyankov
Copy link
Member Author

We need a way to set preferences via native side: e31be1d#commitcomment-17418537

nikDemyankov added a commit that referenced this issue May 24, 2016
…define default download preferences from the native side.

#153
@pablomaurer
Copy link
Contributor

pablomaurer commented May 27, 2016

Tried it today with the current repo version like

    function loadUpdate() {
        chcp.fetchUpdate(function (error, data) {
            if (error) {
                console.error('updateService: Failed to load the update', error);
            } else {
                console.log('updateService: loaded new update', data);
            }
        }, {Authorization: 'Basic ' + btoa('user:password')});
    }

Got the error -1. But maybe it's not fully finished or did i do something wrong?

@nikDemyankov
Copy link
Member Author

You are using it wrong. Headers should be putted in request-headers property as described in the first post. In your case it will be:

function loadUpdate() {
  var options = {
    'request-headers': {
      'Authorization': 'Basic ' + btoa('user:password')
    }
  };
  chcp.fetchUpdate(function (error, data) {
    if (error) {
      console.error('updateService: Failed to load the update', error);
    } else {
      console.log('updateService: loaded new update', data);
    }
  }, options});
}

@nikDemyankov
Copy link
Member Author

And it's only for iOS. Android is not yet implemented.

@pablomaurer
Copy link
Contributor

pablomaurer commented May 27, 2016

Oh sorry, I must have overseen this. Thank you very much, mr King!
It works perfectly!

@wallstudios
Copy link

Am I able to include additional headers for automatic updates etc? Is there any more info (docs) on this?
Also Android version?

@nikDemyankov
Copy link
Member Author

@wallstudios Right now this functionality is for manual updates only: you can't define custom headers in config.xml. Also, I'm not sure that you should. But if needed - I can add this feature later on.

At the moment there is no additional documentation regarding headers. What more do you want me to add to the first post in this thread?

Android version is under construction.

@wallstudios
Copy link

@nikDemyankov thanks. I'm not sure what other documentation I might need. I'll let you know some feedback once I've implemented and tested.

@wallstudios
Copy link

forgot to mention yesterday, if I install via cordova plugin add cordova-hot-code-push-plugin the files you added the other day to "/src/ios/Updater/" aren't being installed. But if I install direct from the git they do. cordova plugin add https://github.com/nordnet/cordova-hot-code-push.git

@wallstudios
Copy link

@nikDemyankov sorry forgot to ask. Any idea how far off the Android version is?

Just wanted to reiterate how much I appreciate the work that has gone into this plugin. Great job.
cheers.

@nikDemyankov
Copy link
Member Author

forgot to mention yesterday, if I install via cordova plugin add cordova-hot-code-push-plugin the files you added the other day to "/src/ios/Updater/" aren't being installed.

Yes, because I didn't release it on npmjs.com since it's not finished yet.

Any idea how far off the Android version is?

I'm hoping to implement it either by the end of this week, or in the beginning of the next one. Should not be too hard, just need to solve some stuff first.

Just wanted to reiterate how much I appreciate the work that has gone into this plugin

Thanks, that means a lot :)

nikDemyankov added a commit that referenced this issue Jun 2, 2016
nikDemyankov added a commit that referenced this issue Jun 3, 2016
…r URLConnection. Adding headers to all requests, if they are set.

#153
nikDemyankov added a commit that referenced this issue Jun 3, 2016
@nikDemyankov
Copy link
Member Author

Added code for android. Should work now for both platforms. I hope...

@wallstudios
Copy link

@nikDemyankov can confirm that it works as expected in both Android and iOS. :) thanks again.

@nikDemyankov
Copy link
Member Author

@wallstudios awesome, thanks for testing!

@nikDemyankov
Copy link
Member Author

Released.

@kaynz
Copy link

kaynz commented Jun 21, 2016

Is it possible to provide headers from config.xml?

@nikDemyankov
Copy link
Member Author

No, not at the moment. In most cases you need this functionality to provide authorization headers, and hardcoding some keys in config.xml might not be the best approach. That's why it's not implemented in config.xml. But if needed - I can add it in next release.

@kaynz
Copy link

kaynz commented Jun 21, 2016

It sounded pretty cool for me. If I'm right, you can easily update the application with
<auto-download enabled="true"/> <auto-install enabled="true"/>
and without writing any further JS code.

@nikDemyankov
Copy link
Member Author

Yeah, these are set to true by default and plugin handles automatically all download/install stuff for you. I still think that it might not be a great idea to hardcode some keys in the app. But sure, will add this feature :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants