Last active
September 3, 2017 17:06
-
-
Save ProGM/4c6f8a36ac6e1c151f85d8ad62cd453c to your computer and use it in GitHub Desktop.
Example indiexpo API implementation in Javascript
This file contains hidden or 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
/* | |
* An usage example of the previous library | |
*/ | |
function loadData() { | |
// Send score 1 | |
indiexpo.sendScore(1).then(function(data) { | |
console.log('score sent, response:', data); | |
}); | |
// Get current user username | |
indiexpo.getUserData().then(function(data) { | |
console.log('user data, response:', data); | |
}); | |
// Gets the current score chart | |
indiexpo.getScores().then(function(data) { | |
console.log('current scores, response:', data); | |
}); | |
} | |
var indiexpo = new IndiexpoClient('YOUR GAME KEY HERE'); | |
// If the user is already logged in previously | |
if (indiexpo.isAuthenticated()) { | |
loadData(); | |
} else { | |
// Requesting user key. | |
// If you're outside the indiexpo iframe, it asks for copy/paste the code from a popup window. | |
// Otherwise, it uses an xhr request | |
indiexpo.requestUserKey() | |
.then(function(userKey) { | |
// | |
indiexpo.authenticate(userKey).then(function(data) { | |
console.log('Correctly authenticated!'); | |
loadData(); | |
}); | |
}); | |
} |
This file contains hidden or 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
/* | |
* JQuery based API client for indiexpo | |
* | |
*/ | |
(function($) { | |
var BASE_URL = 'http://www.indiexpo.dev/api/v1/'; | |
function IndiexpoClient(gameKey) { | |
this.gameKey = gameKey; | |
this.accessToken = localStorage.getItem('indiexpo_access_token'); | |
} | |
IndiexpoClient.prototype.requestUserKey = function(cb) { | |
var q = $.Deferred(); | |
if (window.location.hostname == 'assets.indiexpo.net') { | |
return $.get(BASE_URL + 'authentication'); | |
} else { | |
window.open(BASE_URL + 'authentication'); | |
setTimeout(function() { | |
var val = prompt('Insert your user code. (Please allow your popup block)'); | |
if (val) { | |
q.resolve(val); | |
} else { | |
q.reject(); | |
} | |
}, 0); | |
return q; | |
} | |
}; | |
IndiexpoClient.prototype.isAuthenticated = function() { | |
return !!this.accessToken; | |
}; | |
IndiexpoClient.prototype.authenticate = function(userToken) { | |
var that = this; | |
var q = $.Deferred(); | |
$.post({ | |
url: BASE_URL + 'authentication', | |
data: { game_key: this.gameKey, user_token: userToken } | |
}) | |
.then( | |
function(data) { | |
that.accessToken = data.access_token; | |
localStorage.setItem('indiexpo_access_token', that.accessToken); | |
q.resolve(that.accessToken); | |
}, | |
function() { | |
localStorage.removeItem('indiexpo_access_token'); | |
q.reject(); | |
} | |
); | |
return q; | |
}; | |
IndiexpoClient.prototype.sendScore = function(score) { | |
if (!this.accessToken) { | |
throw 'Missing authentication. Please call IndiexpoClient.authenticate(...)'; | |
} | |
return $.post({ | |
url: BASE_URL + 'score', | |
headers: { 'Authentication': "bearer " + this.accessToken }, | |
data: { score: score } | |
}); | |
}; | |
IndiexpoClient.prototype.getUserData = function() { | |
if (!this.accessToken) { | |
throw 'Missing authentication. Please call IndiexpoClient.authenticate(...)'; | |
} | |
return $.get({ | |
url: BASE_URL + 'user', | |
headers: { 'Authentication': "bearer " + this.accessToken } | |
}); | |
}; | |
IndiexpoClient.prototype.getScores = function() { | |
if (!this.accessToken) { | |
throw 'Missing authentication. Please call IndiexpoClient.authenticate(...)'; | |
} | |
return $.get({ | |
url: BASE_URL + 'scores', | |
headers: { 'Authentication': "bearer " + this.accessToken } | |
}); | |
}; | |
window.IndiexpoClient = IndiexpoClient; | |
})($); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment