@ -3,39 +3,74 @@ const axios = require('axios').default;
/ * *
/ * *
* A service class for interacting with data from the SportsDataIO API .
* A service class for interacting with data from the SportsDataIO API .
* @ extends Service
* /
* /
class SportsDataService extends Service {
class SportsDataService extends Service {
static get services ( ) {
static get services ( ) {
return [ ... super . services , 'configs' , 'models' , 'utility' ]
return [ ... super . services , 'configs' , 'models' , 'utility' ]
}
}
/ * *
* Resolves true if the game is currently in the draft stage .
* @ return { Promise < boolean > }
* /
async is _draft _stage ( ) {
async is _draft _stage ( ) {
const Setting = this . models . get ( 'models::setting' )
const Setting = this . models . get ( 'models::setting' )
return this . utility . infer ( await Setting . get ( 'in_draft_stage' ) )
return this . utility . infer ( await Setting . get ( 'in_draft_stage' ) )
}
}
/ * *
* Resolves to the current week number of gameplay .
* @ return { Promise < number > }
* /
async current _play _week ( ) {
async current _play _week ( ) {
const Setting = this . models . get ( 'models::setting' )
const Setting = this . models . get ( 'models::setting' )
return this . utility . infer ( await Setting . get ( 'current_week' ) )
return this . utility . infer ( await Setting . get ( 'current_week' ) )
}
}
/ * *
* Fetches a list of all players on the given team from the sports data API .
* @ param { string } team _key
* @ return { Promise < Array < any >> }
* /
async get _team _players ( team _key ) {
async get _team _players ( team _key ) {
return this . get _request ( ` Players/ ${ team _key } ` )
return this . get _request ( ` Players/ ${ team _key } ` )
}
}
/ * *
* Fetches a list of all active teams from the sports data API .
* @ return { Promise < Array < any >> }
* /
async get _active _teams ( ) {
async get _active _teams ( ) {
return this . get _request ( 'Teams' )
return this . get _request ( 'Teams' )
}
}
/ * *
* Make a get request to the sports data API .
* @ param { string } path
* @ param { string } [ base = 'scores' ] - the API domain ( scores , projections , & c . )
* @ return { Promise < any > }
* /
async get _request ( path , base = 'scores' ) {
async get _request ( path , base = 'scores' ) {
const response = await axios . get ( this . url ( path , base ) )
const response = await axios . get ( this . url ( path , base ) )
return response . data
return response . data
}
}
/ * *
* Fetches a list of player stats for all players in the league for the given week .
* @ param { number } week _num
* @ return { Promise < Array < any >>> }
* /
async get _week _player _stats ( week _num ) {
async get _week _player _stats ( week _num ) {
return this . get _request ( ` PlayerGameProjectionStatsByWeek/ ${ this . configs . get ( 'server.sports_data.season' ) } / ${ week _num } ` , 'projections' )
return this . get _request ( ` PlayerGameProjectionStatsByWeek/ ${ this . configs . get ( 'server.sports_data.season' ) } / ${ week _num } ` , 'projections' )
}
}
/ * *
* Resolve an endpoint and an API domain to a fully - qualified URL to the sports data API .
* @ param { string } path
* @ param { string } [ base = 'scores' ] - the API domain ( scores , projections , & c . )
* @ return { string }
* /
url ( path , base = 'scores' ) {
url ( path , base = 'scores' ) {
if ( path . startsWith ( '/' ) ) path = path . slice ( 1 )
if ( path . startsWith ( '/' ) ) path = path . slice ( 1 )
return ` https://api.sportsdata.io/v3/nfl/ ${ base } /json/ ${ path } ?key= ${ this . configs . get ( 'server.sports_data.api_key' ) } `
return ` https://api.sportsdata.io/v3/nfl/ ${ base } /json/ ${ path } ?key= ${ this . configs . get ( 'server.sports_data.api_key' ) } `