Moved requester get functions to use lambda

This commit is contained in:
Samuel Roach 2022-06-06 13:52:15 +01:00
parent 9da10a35c1
commit 36a90c3b39

View File

@ -1,6 +1,7 @@
""" Requester class """ """ Requester class """
import json import json
from typing import Callable
import requests import requests
from uritemplate import URITemplate from uritemplate import URITemplate
@ -111,12 +112,8 @@ class Requester():
"criteria": criteria "criteria": criteria
} }
def _run_request(self, season, round_no, criteria, resource, value=None, limit=None,
def run_request(self, season, round_no, criteria, resource, value=None, limit=None,
offset=None) -> dict: offset=None) -> dict:
"""
Takes values to run the request and return a dict
"""
url_tmpl = URITemplate('https://ergast.com/api{/series}{/season}{/round}' url_tmpl = URITemplate('https://ergast.com/api{/series}{/season}{/round}'
'{/criteria*}{/resource}{/value}.json{?limit,offset}') '{/criteria*}{/resource}{/value}.json{?limit,offset}')
url = url_tmpl.expand(host=HOST, series=SERIES, url = url_tmpl.expand(host=HOST, series=SERIES,
@ -129,6 +126,16 @@ class Requester():
return json.loads(response.text) return json.loads(response.text)
raise Exception(f"Failed with status code {response.status_code}. Error: {response.reason}") raise Exception(f"Failed with status code {response.status_code}. Error: {response.reason}")
def _get_api_json(self, get_params: Callable, get_criteria: Callable,
resource: str, param: dict) -> dict:
params = get_params(param)
filters = get_criteria(params, resource)
return self._run_request(season=params["season"], round_no=params["round"],
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
# #
# Race and Results # Race and Results
# #
@ -137,13 +144,10 @@ class Requester():
""" """
Get the Circuits JSON from the Ergast API Get the Circuits JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "circuits") self._get_race_results_criteria,
"circuits",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["CircuitTable"]["Circuits"] return api_json["MRData"]["CircuitTable"]["Circuits"]
@ -152,13 +156,10 @@ class Requester():
""" """
Get the Constructors JSON from the Ergast API Get the Constructors JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "constructors") self._get_race_results_criteria,
"constructors",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["ConstructorTable"]["Constructors"] return api_json["MRData"]["ConstructorTable"]["Constructors"]
@ -167,13 +168,10 @@ class Requester():
""" """
Get the Drivers JSON from the Ergast API Get the Drivers JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "drivers") self._get_race_results_criteria,
"drivers",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["DriverTable"]["Drivers"] return api_json["MRData"]["DriverTable"]["Drivers"]
@ -182,13 +180,10 @@ class Requester():
""" """
Get the Qualifying JSON from the Ergast API Get the Qualifying JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "qualifying") self._get_race_results_criteria,
"qualifying",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]
@ -196,13 +191,10 @@ class Requester():
""" """
Get the Sprints JSON from the Ergast API Get the Sprints JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "sprint") self._get_race_results_criteria,
"sprint",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]
@ -210,13 +202,10 @@ class Requester():
""" """
Get the Results JSON from the Ergast API Get the Results JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "results") self._get_race_results_criteria,
"results",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]
@ -224,13 +213,10 @@ class Requester():
""" """
Get the Races JSON from the Ergast API Get the Races JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "races") self._get_race_results_criteria,
"races",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]
@ -238,13 +224,10 @@ class Requester():
""" """
Get the Seasons JSON from the Ergast API Get the Seasons JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "seasons") self._get_race_results_criteria,
"seasons",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["SeasonTable"]["Seasons"] return api_json["MRData"]["SeasonTable"]["Seasons"]
@ -252,13 +235,10 @@ class Requester():
""" """
Get the Statuses JSON from the Ergast API Get the Statuses JSON from the Ergast API
""" """
params = self._get_race_results_params(param) api_json = self._get_api_json(self._get_race_results_params,
filters = self._get_race_results_criteria(params, "status") self._get_race_results_criteria,
"status",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["StatusTable"]["Status"] return api_json["MRData"]["StatusTable"]["Status"]
@ -270,13 +250,10 @@ class Requester():
""" """
Get the Driver Standings JSON from the Ergast API Get the Driver Standings JSON from the Ergast API
""" """
params = self._get_standings_params(param) api_json = self._get_api_json(self._get_standings_params,
filters = self._get_standings_criteria(params, "driverStandings") self._get_standings_criteria,
"driverStandings",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["StandingsTable"]["StandingsLists"] return api_json["MRData"]["StandingsTable"]["StandingsLists"]
@ -284,13 +261,10 @@ class Requester():
""" """
Get the Constructor Standings JSON from the Ergast API Get the Constructor Standings JSON from the Ergast API
""" """
params = self._get_standings_params(param) api_json = self._get_api_json(self._get_standings_params,
filters = self._get_standings_criteria(params, "constructorStandings") self._get_standings_criteria,
"constructorStandings",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["StandingsTable"]["StandingsLists"] return api_json["MRData"]["StandingsTable"]["StandingsLists"]
@ -302,13 +276,10 @@ class Requester():
""" """
Get the Laps JSON from the Ergast API Get the Laps JSON from the Ergast API
""" """
params = self._get_laps_pit_stops_params(param) api_json = self._get_api_json(self._get_laps_pit_stops_params,
filters = self._get_laps_pit_stops_criteria(params, "laps") self._get_laps_pit_stops_criteria,
"laps",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]
@ -316,12 +287,9 @@ class Requester():
""" """
Get the Pit Stops JSON from the Ergast API Get the Pit Stops JSON from the Ergast API
""" """
params = self._get_laps_pit_stops_params(param) api_json = self._get_api_json(self._get_laps_pit_stops_params,
filters = self._get_laps_pit_stops_criteria(params, "pitstops") self._get_laps_pit_stops_criteria,
"pitstops",
api_json = self.run_request(season=params["season"], round_no=params["round"], param)
criteria=filters["criteria"], resource=filters["resource"],
value=filters["value"], limit=params["paging"]["limit"],
offset=params["paging"]["offset"])
return api_json["MRData"]["RaceTable"]["Races"] return api_json["MRData"]["RaceTable"]["Races"]