2017-02-01 11:39:52 +01:00
|
|
|
import 'whatwg-fetch';
|
|
|
|
|
|
|
|
function checkStatus(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
return response.text().then((text) => {
|
|
|
|
throw new Error(text);
|
|
|
|
});
|
2017-01-31 14:40:01 +01:00
|
|
|
}
|
2017-02-01 11:39:52 +01:00
|
|
|
return response;
|
|
|
|
}
|
2017-01-31 14:40:01 +01:00
|
|
|
|
2017-02-01 11:39:52 +01:00
|
|
|
function parseJson(response) {
|
|
|
|
return response.json();
|
|
|
|
}
|
2017-01-31 14:40:01 +01:00
|
|
|
|
2017-02-01 11:39:52 +01:00
|
|
|
function encodeQueryParams(params) {
|
|
|
|
return '?' + Object.keys(params).map((k) => {
|
|
|
|
return k + '=' + encodeURIComponent(params[k]);
|
|
|
|
}).join('&');
|
|
|
|
}
|
2017-01-31 14:40:01 +01:00
|
|
|
|
2017-02-01 11:39:52 +01: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 14:40:01 +01:00
|
|
|
};
|
2017-01-30 16:50:31 +01:00
|
|
|
|
2017-02-01 11:39:52 +01:00
|
|
|
|
2017-01-30 16:50:31 +01:00
|
|
|
export default class RtsClient {
|
|
|
|
constructor(url) {
|
|
|
|
this._url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTeamsConfig() {
|
2017-02-01 11:39:52 +01:00
|
|
|
return request(this._url + '/teams');
|
2017-01-30 16:50:31 +01:00
|
|
|
}
|
2017-01-31 12:13:05 +01: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.
|
2017-02-10 17:50:25 +01:00
|
|
|
* @param {string} sid the sign-up identity server session ID .
|
|
|
|
* @param {string} clientSecret the sign-up client secret.
|
2017-02-01 11:39:52 +01:00
|
|
|
* @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon
|
2017-01-31 12:13:05 +01:00
|
|
|
* success.
|
|
|
|
*/
|
2017-02-10 17:50:25 +01:00
|
|
|
trackReferral(referrer, sid, clientSecret) {
|
2017-02-01 11:39:52 +01:00
|
|
|
return request(this._url + '/register',
|
|
|
|
{
|
2017-02-01 12:16:14 +01:00
|
|
|
body: {
|
|
|
|
referrer: referrer,
|
2017-02-10 17:50:25 +01:00
|
|
|
session_id: sid,
|
|
|
|
client_secret: clientSecret,
|
2017-02-01 12:16:14 +01:00
|
|
|
},
|
2017-02-01 11:39:52 +01:00
|
|
|
method: 'POST',
|
|
|
|
}
|
|
|
|
);
|
2017-01-31 12:13:05 +01:00
|
|
|
}
|
|
|
|
|
2017-02-01 12:16:14 +01:00
|
|
|
getTeam(teamToken) {
|
2017-02-01 11:39:52 +01:00
|
|
|
return request(this._url + '/teamConfiguration',
|
|
|
|
{
|
2017-02-01 12:16:14 +01:00
|
|
|
qs: {
|
|
|
|
team_token: teamToken,
|
|
|
|
},
|
2017-02-01 11:39:52 +01:00
|
|
|
}
|
|
|
|
);
|
2017-01-31 12:13:05 +01:00
|
|
|
}
|
2017-02-03 15:34:24 +01:00
|
|
|
|
2017-02-07 13:01:44 +01:00
|
|
|
/**
|
|
|
|
* Signal to the RTS that a login has occurred and that a user requires their team's
|
|
|
|
* token.
|
|
|
|
* @param {string} userId the user ID of the user who is a member of a team.
|
|
|
|
* @returns {Promise} a promise that resolves to { team_token: 'sometoken' } upon
|
|
|
|
* success.
|
|
|
|
*/
|
2017-02-03 15:34:24 +01:00
|
|
|
login(userId) {
|
|
|
|
return request(this._url + '/login',
|
|
|
|
{
|
|
|
|
qs: {
|
|
|
|
user_id: userId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-01-30 16:50:31 +01:00
|
|
|
}
|