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