UNPKG

gamerocket

Version:

Gamerocket NodeJS Client Library

133 lines (114 loc) 3.94 kB
(function() { var Buffer, Encryption, Http, exceptions, gamerocket, http, https, querystring; http = require('http'); https = require('https'); Buffer = require('buffer').Buffer; Encryption = require('./encryption').Encryption; gamerocket = require('../gamerocket'); exceptions = require('./exceptions'); querystring = require('querystring'); Http = (function() { function Http(config) { this.config = config; } Http.prototype.checkHttpStatus = function(status) { switch (status.toString()) { case '200': case '201': case '422': return null; case '401': return exceptions.AuthenticationError(); case '403': return exceptions.AuthorizationError(); case '404': return exceptions.NotFoundError(); case '426': return exceptions.UpgradeRequired(); case '500': return exceptions.ServerError(); case '503': return exceptions.DownForMaintenanceError(); default: return exceptions.UnexpectedError('Unexpected HTTP response: ' + status); } }; Http.prototype["delete"] = function(url, callback) { return this.request('DELETE', url, null, callback); }; Http.prototype.get = function(url, callback) { return this.request('GET', url, null, callback); }; Http.prototype.post = function(url, body, callback) { return this.request('POST', url, body, callback); }; Http.prototype.put = function(url, body, callback) { return this.request('PUT', url, body, callback); }; Http.prototype.request = function(method, url, body, callback) { var client, options, requestBody, theRequest, url_sign, _this = this; if (this.config.environment.ssl) { client = https; url_sign = 'https://' + this.config.environment.server + ':' + this.config.environment.port + this.config.apiVersion + url; } else { client = http; url_sign = 'http://' + this.config.environment.server + ':' + this.config.environment.port + this.config.apiVersion + url; } if (body === null) { body = {}; } body['signature'] = Encryption.sign(method, url_sign, body, this.config.secretKey); if (method === 'GET') { url += '?' + querystring.stringify(body); } else if (method === 'DELETE') { url += '?' + querystring.stringify(body); } options = { host: this.config.environment.server, port: this.config.environment.port, method: method, path: '1' + url, headers: { 'X-ApiVersion': this.config.apiVersion, 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'GameRocket Node ' + gamerocket.version } }; if (body) { requestBody = querystring.stringify(body); options.headers['Content-Length'] = Buffer.byteLength(requestBody).toString(); } theRequest = client.request(options, function(response) { body = ''; response.on('data', function(responseBody) { return body += responseBody; }); return response.on('end', function() { var error, json; error = _this.checkHttpStatus(response.statusCode); if (error) { return callback(error, null); } if (body !== ' ') { json = JSON.parse(body); if (json['error']) { return callback(json, null); } else { return callback(null, json); } } else { return callback(null, null); } }); }); if (body) { theRequest.write(requestBody); } return theRequest.end(); }; return Http; })(); exports.Http = Http; }).call(this);