Formatting and naming tweaks
Also moved the apiKey to be a class property that can optionally be specified by the caller. If not specified, the Android app's key is used. Finally, made 'headers' optional for ShiftRequest::_request(). If not specified, the full set of headers, including API key, is used.
This commit is contained in:
31
index.js
31
index.js
@ -2,10 +2,8 @@ const http = require('http')
|
|||||||
const querystring = require('querystring')
|
const querystring = require('querystring')
|
||||||
const zlib = require('zlib')
|
const zlib = require('zlib')
|
||||||
|
|
||||||
let apiKey = 'YXBpLnNoaWZ0c3RhdHMuY29tLDE5YjhhZGIwNDVjZjAxMzJhM2E5N2VmZDQ1YTRj'
|
|
||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
request(options) {
|
_request(options) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
http.get(options, (response) => {
|
http.get(options, (response) => {
|
||||||
const { statusCode } = response
|
const { statusCode } = response
|
||||||
@ -46,15 +44,20 @@ class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = class ShiftRequest extends Request {
|
module.exports = class ShiftRequest extends Request {
|
||||||
request(options) {
|
constructor(apiKey) {
|
||||||
return super.request({
|
super()
|
||||||
|
this.apiKey = apiKey || 'YXBpLnNoaWZ0c3RhdHMuY29tLDE5YjhhZGIwNDVjZjAxMzJhM2E5N2VmZDQ1YTRj'
|
||||||
|
}
|
||||||
|
|
||||||
|
_request(options) {
|
||||||
|
return super._request({
|
||||||
hostname: 'api.shiftstats.com',
|
hostname: 'api.shiftstats.com',
|
||||||
path: this.url(options.url, options.query),
|
path: this._url(options.url, options.query),
|
||||||
headers: options.headers
|
headers: options.headers || this._headers()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
basicHeaders() {
|
_basicHeaders() {
|
||||||
return {
|
return {
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@ -65,26 +68,26 @@ module.exports = class ShiftRequest extends Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
headers() {
|
_headers() {
|
||||||
if (typeof this.headersWithTicket === 'undefined') {
|
if (typeof this.headersWithTicket === 'undefined') {
|
||||||
this.headersWithTicket = Object.assign({'Authorization': `StatsAuth ticket="${this.ticketHash}"`}, this.basicHeaders())
|
this.headersWithTicket = Object.assign({'Authorization': `StatsAuth ticket="${this.ticketHash}"`}, this._basicHeaders())
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.headersWithTicket
|
return this.headersWithTicket
|
||||||
}
|
}
|
||||||
|
|
||||||
url(path, query) {
|
_url(path, query) {
|
||||||
return `/${path}?${querystring.stringify(query)}`
|
return `/${path}?${querystring.stringify(query)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
login() {
|
login() {
|
||||||
return this.request({ url: 'login', query: {key: apiKey}, headers: this.basicHeaders() }).then((json) => {
|
return this._request({ url: 'login', query: {key: this.apiKey}, headers: this._basicHeaders() }).then((json) => {
|
||||||
this.ticketHash = json.ticket.hash
|
this.ticketHash = json.ticket.hash
|
||||||
return json
|
return json
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
divisionStandings(division_id, type = 'Regular Season') {
|
divisionStandings(divisionId, type = 'Regular Season') {
|
||||||
return this.request({ url: `division/${division_id}/standings`, query: {type: type}, headers: this.headers()})
|
return this._request({ url: `division/${divisionId}/standings`, query: {type: type}})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user