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

Modify to supply API Key in header #2

Open
ghost opened this issue Jun 10, 2018 · 7 comments
Open

Modify to supply API Key in header #2

ghost opened this issue Jun 10, 2018 · 7 comments
Labels
enhancement New feature or request

Comments

@ghost
Copy link

ghost commented Jun 10, 2018

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
Copy link
Owner

timdows commented Jun 16, 2018

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 timdows added the enhancement New feature or request label Jun 16, 2018
@timdows
Copy link
Owner

timdows commented Jun 16, 2018

Off course feel free to submit a pull request

@bachoo786
Copy link

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

@timdows
Copy link
Owner

timdows commented Sep 4, 2019

@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
Copy link

@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
Copy link
Owner

timdows commented Sep 5, 2019

@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
Copy link

bachoo786 commented Sep 5, 2019

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants