timdows / MMM-JsonTable

A module for the MagicMirror project which creates a table filled with a list gathered from a json request.
MIT License
25 stars 31 forks source link

Modify to supply API Key in header #2

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi, I'm totally new to this but I wanted to know how would I go about modifying this module to add in a API key in the header so I can pull in the json feed? Here is the request example I'm given that i'd like to use.

curl --get --include 'https://transloc-api-1-2.p.mashape.com/arrival-estimates.json agencies=yale&callback=call' \ -H 'X-Mashape-Key: lM66qSxXHcmsh5qSfHLz5vvHrP7zp1Ra8ncjsnCDxETtwCYwJP' \ -H 'Accept: application/json'

timdows commented 6 years ago

That should be doable You can modify the node_helper.js file and add the headers to the request itself.

Some example I found https://stackoverflow.com/questions/17121846/node-js-how-to-send-headers-with-form-data-using-request-module

If there is a lot of demand to add this, we can look into a way to make this configurable

timdows commented 6 years ago

Off course feel free to submit a pull request

bachoo786 commented 5 years ago

hey @timdows can you please add the api key for json? I have the same issue as the OP.

timdows commented 5 years ago

@bachoo786 @clombardozzi Might be it as easy as adding the headers to the node_helper.js? Let me know if this works, and I'll try to add it to the config file

getJson: function(url) {
        var self = this;

        request({
            url: url,
            method: 'GET',
            headers: {
                'X-Mashape-Key': 'lM66qSxXHcmsh5qSfHLz5vvHrP7zp1Ra8ncjsnCDxETtwCYwJP'
            }
        }, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                var json = JSON.parse(body);
                // Send the json data back with the url to distinguish it on the receiving part
                self.sendSocketNotification("MMM-JsonTable_JSON_RESULT", { url: url, data: json });
            }
        });
    },
bachoo786 commented 5 years ago

@timdows thanks. i have inserted the code above in node_helper.js but I still get the following error:

Json data is not of type array! Maybe the config arrayName is not used and should be, or is configured wrong

here is what I have in my config.js:

{
        module: 'MMM-JsonTable',
        position: 'top_right',
        header: 'Test',
        config: {
                url: 'https://transportapi.com/v3/uk/train/station/hrw///timetable.json?app_id=bc370f15&app_key=7afee0527e55b33e74960f092eb08d2a&destination=eus&operator=lm&train_status=passenger', // Required
                arrayName: 'items' // Optional
        }
},

Can you please help mate?

Thanks.

timdows commented 5 years ago

@bachoo786 I see the next issue, that is that the JSON response does not send something back that this module can work with (it sends back a nested object with the array to display in 'departues.all'

This might work, but it is a hack, result: image

https://github.com/timdows/MMM-JsonTable/blob/master/MMM-JsonTable.js#L61

if (this.config.arrayName) {
    items = this.jsonData[this.config.arrayName]["all"];
} else {
    items = this.jsonData;
}

Config

{
    module: 'MMM-JsonTable',
    position: 'top_right',
    header: 'Test',
    config: {
        url: 'https://transportapi.com/v3/uk/train/station/hrw/2019-09-05/10:49/timetable.json?app_id=bc370f15&app_key=7afee0527e55b33e74960f092eb08d2a&destination=eus&operator=lm&train_status=passenger',
        arrayName: 'departures',
        keepColumns: ["destination_name", "mode", "aimed_arrival_time"]
    }
},
bachoo786 commented 5 years ago

@timdows thanks for this hack.

I copied your config above and it works perfect.

I am wondering why is the information not updating ?

EDIT: I figured it out as the url that you have in your post is specific to 10:49