Created
February 21, 2018 19:27
-
-
Save astrotars/138a60d3215a5bf32b881f777eb408bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import npm modules | |
import axios from 'axios'; | |
import jwt from 'jsonwebtoken'; | |
// import custom utilities | |
import config from '../../config'; | |
const personalization = data => { | |
// setup promise | |
return new Promise((resolve, reject) => { | |
// build jwt for signing the API call | |
const token = jwt.sign( | |
{ | |
action: '*', | |
feed_id: '*', | |
resource: '*', | |
user_id: '*', | |
}, | |
config.stream.apiSecret, | |
{ algorithm: 'HS256', noTimestamp: true }, | |
); | |
// initiate call via axios (http module) | |
return axios({ | |
baseURL: config.stream.baseUrl, | |
headers: { | |
Authorization: token, | |
'Content-Type': 'application/json', | |
'Stream-Auth-Type': 'jwt', | |
}, | |
method: 'GET', | |
params: { | |
api_key: config.stream.apiKey, | |
user_id: data.userId, | |
}, | |
url: data.endpoint, | |
}) | |
.then(res => { | |
// map over results and deserialize | |
const data = res.data.results.map(result => { | |
return result.foreign_id.split(':')[1]; | |
}); | |
// successfully resolve call and return deserialized data | |
resolve(data); | |
}) | |
.catch(err => { | |
// catch and reject with error | |
reject(err); | |
}); | |
}); | |
}; | |
export default personalization; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment