eecs448-project-4/app/services/sports_data.service.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

const { Service } = require('flitter-di')
const axios = require('axios').default;
/**
* A service class for interacting with data from the SportsDataIO API.
2020-11-08 18:34:50 +00:00
* @extends Service
*/
class SportsDataService extends Service {
static get services() {
return [...super.services, 'configs', 'models', 'utility']
}
2020-11-08 18:34:50 +00:00
/**
* Resolves true if the game is currently in the draft stage.
* @return {Promise<boolean>}
*/
async is_draft_stage() {
const Setting = this.models.get('models::setting')
return this.utility.infer(await Setting.get('in_draft_stage'))
}
2020-11-08 18:34:50 +00:00
/**
* Resolves to the current week number of gameplay.
* @return {Promise<number>}
*/
async current_play_week() {
const Setting = this.models.get('models::setting')
return this.utility.infer(await Setting.get('current_week'))
}
2020-11-08 18:34:50 +00:00
/**
* 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) {
return this.get_request(`Players/${team_key}`)
}
2020-11-08 18:34:50 +00:00
/**
* Fetches a list of all active teams from the sports data API.
* @return {Promise<Array<any>>}
*/
async get_active_teams() {
return this.get_request('Teams')
}
2020-11-08 18:34:50 +00:00
/**
* 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') {
const response = await axios.get(this.url(path, base))
return response.data
}
2020-11-08 18:34:50 +00:00
/**
* Fetches a list of player stats for all players in the league for the given week.
* @param {number} week_num
* @return {Promise<Array<any>>}
2020-11-08 18:34:50 +00:00
*/
async get_week_player_stats(week_num) {
return this.get_request(`PlayerGameProjectionStatsByWeek/${this.configs.get('server.sports_data.season')}/${week_num}`, 'projections')
}
2020-11-08 18:34:50 +00:00
/**
* 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') {
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')}`
}
}
module.exports = exports = SportsDataService