2017-02-01 18:39:52 +08:00
|
|
|
import 'whatwg-fetch';
|
|
|
|
|
|
|
|
function checkStatus(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
return response.text().then((text) => {
|
|
|
|
throw new Error(text);
|
|
|
|
});
|
2017-01-31 21:40:01 +08:00
|
|
|
}
|
2017-02-01 18:39:52 +08:00
|
|
|
return response;
|
|
|
|
}
|
2017-01-31 21:40:01 +08:00
|
|
|
|
2017-02-01 18:39:52 +08:00
|
|
|
function parseJson(response) {
|
|
|
|
return response.json();
|
|
|
|
}
|
2017-01-31 21:40:01 +08:00
|
|
|
|
2017-02-01 18:39:52 +08:00
|
|
|
function encodeQueryParams(params) {
|
|
|
|
return '?' + Object.keys(params).map((k) => {
|
|
|
|
return k + '=' + encodeURIComponent(params[k]);
|
|
|
|
}).join('&');
|
|
|
|
}
|
2017-01-31 21:40:01 +08:00
|
|
|
|
2017-02-01 18:39:52 +08:00
|
|
|
const request = (url, opts) => {
|
|
|
|
if (opts && opts.qs) {
|
|
|
|
url += encodeQueryParams(opts.qs);
|
|
|
|
delete opts.qs;
|
|
|
|
}
|
|
|
|
if (opts && opts.body) {
|
|
|
|
if (!opts.headers) {
|
|
|
|
opts.headers = {};
|
|
|
|
}
|
|
|
|
opts.body = JSON.stringify(opts.body);
|
|
|
|
opts.headers['Content-Type'] = 'application/json';
|
|
|
|
}
|
|
|
|
return fetch(url, opts)
|
|
|
|
.then(checkStatus)
|
|
|
|
.then(parseJson);
|
2017-01-31 21:40:01 +08:00
|
|
|
};
|
2017-01-30 23:50:31 +08:00
|
|
|
|
2017-02-01 18:39:52 +08:00
|
|
|
|
2017-01-30 23:50:31 +08:00
|
|
|
export default class RtsClient {
|
|
|
|
constructor(url) {
|
|
|
|
this._url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTeamsConfig() {
|
2017-02-01 18:39:52 +08:00
|
|
|
return request(this._url + '/teams');
|
2017-01-30 23:50:31 +08:00
|
|
|
}
|
2017-01-31 19:13:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Track a referral with the Riot Team Server. This should be called once a referred
|
|
|
|
* user has been successfully registered.
|
|
|
|
* @param {string} referrer the user ID of one who referred the user to Riot.
|
|
|
|
* @param {string} user_id the user ID of the user being referred.
|
|
|
|
* @param {string} user_email the email address linked to `user_id`.
|
2017-02-01 18:39:52 +08:00
|
|
|
* @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon
|
2017-01-31 19:13:05 +08:00
|
|
|
* success.
|
|
|
|
*/
|
|
|
|
trackReferral(referrer, user_id, user_email) {
|
2017-02-01 18:39:52 +08:00
|
|
|
return request(this._url + '/register',
|
|
|
|
{
|
|
|
|
body: {referrer, user_id, user_email},
|
|
|
|
method: 'POST',
|
|
|
|
}
|
|
|
|
);
|
2017-01-31 19:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getTeam(team_token) {
|
2017-02-01 18:39:52 +08:00
|
|
|
return request(this._url + '/teamConfiguration',
|
|
|
|
{
|
|
|
|
qs: {team_token},
|
|
|
|
}
|
|
|
|
);
|
2017-01-31 19:13:05 +08:00
|
|
|
}
|
2017-01-30 23:50:31 +08:00
|
|
|
}
|