javascriptangularjscallbackhttp-headersfeedly

Node & Angular, Feedly RSS API call, get data from service and pass to controller. Callback or forming url with auth header for https.get() request


I have a controller.js file that I am trying to get data from my feed-service.js file. FEEDLY_USER_ID and FEEDLY_ACCESS_TOKEN are accessible and defined in a separate config.js file.

controller.js:

$scope.feedlyGlobalAll = FeedService.getGlobalAll();

feed-service.js:

var request = require('request');
var globalOptions = {
    url: 'https://cloud.feedly.com/v3/streams/contents?streamId=user/' + FEEDLY_USER_ID + '/category/global.all',
    auth: {
        'bearer': FEEDLY_ACCESS_TOKEN
    }
};

service.getGlobalAll = function(){
    request.get(globalOptions, function(error, response, body){
        if(!error && response.statusCode == 200){
            service.globalAll = JSON.parse(body);
            return service.globalAll;
        }
        // error code here //
   })
}

I'm using an npm package called "request" to make the GET because I couldn't get https.get() work. This Feedly API call requires the URL, my user ID, and an access token passed in the header.

I've been reading and apparently I'm supposed to use callbacks or promises, but I can't get either of them to work. With http.get(), I can utilize promises by http.get().then(yada yada), which works for the forecast.io call I'm making elsewhere. The request module apparently doesn't allow .then(). I typically run into TypeError and .then() is not a function.

When I tried doing https.get(), here is the code I was using. I was never able to acquire a successful response.

var url = {
    url: 'https://cloud.feedly.com/v3/streams/contents?streamId=user/' + FEEDLY_USER_ID + '/category/global.all',
    'headers': {
        'Authorization': 'Bearer ' + FEEDLY_ACCESS_TOKEN
    }
};

https = require('https');
https.get(url).then(yada yada)

I tried numerous things in the var url for the https.get call. I tried with and without quotes, I tried just auth: bearer token as it works with the request module, but wasn't able to get it to work.

Solution to this issue would either be:

  1. Callback from feed-service to controller using existing request module code.
  2. Figuring out the correct way to form the url for the https.get request and then I can utilize its promise function.

Solution

  • Use a promise inside your request callback:

    service.getGlobalAll = function() {
       return $q(function(resolve, reject) {
          request.get(globalOptions, function(error, response, body){
             if(!error && response.statusCode == 200){
                service.globalAll = JSON.parse(body);
                resolve(service.globalAll);
            } else {
              reject();
            }
         });
       });
    };
    

    $q (angular's promise api) can be injected as a dependency in your service. The above code will return a promise that will resolve when your library's ajax call returns, so you can access it with .then().