Created
April 19, 2019 07:26
-
-
Save nagyv/d821e1bafe0dfde4c83370a724de36a8 to your computer and use it in GitHub Desktop.
get and create record in GameSparks Data Service
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
const axios = require('axios') | |
require('dotenv').config() | |
const username = process.env.LOGIN | |
const password = process.env.PASSWORD | |
const game = process.env.GAME | |
async function jwtAuth(username, password, game, filter) { | |
const response = await axios({ | |
url: `https://auth.gamesparks.net/restv2/auth/game/${game}/jwt`, | |
auth: { | |
username, password | |
} | |
}) | |
return response.data['X-GS-JWT'] | |
} | |
async function getEndpoints(token, game) { | |
const response = await axios({ | |
url: `https://config2.gamesparks.net/restv2/game/${game}/endpoints`, | |
headers: { | |
"X-GS-JWT": token | |
} | |
}) | |
return response.data | |
} | |
async function getToken(base, token, game, id) { | |
const response = await axios({ | |
url: `${base}/restv2/game/${game}/data/RegistrationTokens/${id}`, | |
headers: { | |
"X-GS-JWT": token, | |
"Content-Type": "application/json" | |
} | |
}) | |
return response.data | |
} | |
async function createToken(base, token, game) { | |
const item = { | |
"owner": null, | |
"gameType": "GAME_BASIC", | |
"created": Date.now(), | |
"updated": Date.now(), | |
"token": "teszt" | |
} | |
try { | |
const response = await axios({ | |
url: `${base}/restv2/game/${game}/data/RegistrationTokens/${item.token}`, | |
method: 'put', | |
data: item, | |
headers: { | |
"X-GS-JWT": token, | |
"Content-Type": "application/json" | |
} | |
}) | |
return response.data | |
} catch (err) { | |
return [err.response.status, err.response.statusText, err.response] | |
} | |
} | |
async function run() { | |
const token = await jwtAuth(username, password, game, 'data') | |
const urls = await getEndpoints(token, game) | |
const base = urls.liveNosql | |
const getResponse = await getToken(base, token, game, 'bika') | |
console.log('Token received', getResponse) | |
const createResponse = await createToken(base, token, game) | |
console.log('Created', createResponse) | |
} | |
run().then(() => { | |
console.log('Exit') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment