1- from fastapi import APIRouter , Depends , Request
1+ # Copyright 2023 Ben Chapman
2+ #
3+ # This file is part of The EV Finder.
4+ #
5+ # The EV Finder is free software: you can redistribute it and/or modify it under the
6+ # terms of the GNU General Public License as published by the Free Software Foundation,
7+ # either version 3 of the License, or (at your option) any later version.
8+ #
9+ # The EV Finder is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+ # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+ # See the GNU General Public License for more details.
12+ #
13+ # You should have received a copy of the GNU General Public License along with The EV Finder.
14+ # If not, see <https://www.gnu.org/licenses/>.
15+
16+ import datetime
217
18+ from fastapi import APIRouter , Depends , Request
319from src .libs .common_query_params import CommonInventoryQueryParams
4- from src .libs .responses import error_response , send_response
520from src .libs .http import AsyncHTTPClient
21+ from src .libs .responses import error_response , send_response
622
723router = APIRouter (prefix = "/api" )
824verify_ssl = True
@@ -15,15 +31,26 @@ async def get_genesis_inventory(
1531 common_params : CommonInventoryQueryParams = Depends (),
1632) -> dict :
1733 """Makes a request to the Genesis Inventory API and returns a JSON object containing
18- the inventory results for a given vehicle model, zip code and search radius.
34+ the inventory results for a given vehicle model and zip code.
35+
36+ The Genesis API does not accept a search radius nor a model year, rather a maxdealers
37+ URI path segment. The EV Finder frontend deals with filtering the results to display
38+ the relevant information for the search performed.
39+
40+ Args:
41+ req (Request): The HTTP request from the EV Finder application.
42+ common_params (CommonInventoryQueryParams, optional): The EV Finder query params.
43+ Typically zip, year, model and radius. Defaults to Depends().
44+
45+ Returns:
46+ dict: A JSON object containing the inventory results for the given search.
1947 """
20- params = {
21- "zip" : common_params .zip ,
22- "year" : common_params .year ,
23- "modelname" : common_params .model ,
24- "radius" : common_params .radius ,
25- "maxdealers" : 25 ,
26- }
48+
49+ zipcode = common_params .zip
50+ modelname = common_params .model
51+ maxdealers = 50
52+ todays_date = datetime .datetime .now ().strftime ("%Y-%m-%d" )
53+
2754 headers = {
2855 "User-Agent" : req .headers .get ("User-Agent" ),
2956 "Referer" : f"{ genesis_base_url } /us/en/new/inventory.html" ,
@@ -33,9 +60,10 @@ async def get_genesis_inventory(
3360 base_url = genesis_base_url , timeout_value = 30.0 , verify = verify_ssl
3461 ) as http :
3562 inv = await http .get (
36- uri = ("/bin/api/v1/inventory" ),
63+ uri = (
64+ f"/bin/api/v1/inventory.json/{ modelname } /{ zipcode } /{ maxdealers } /{ todays_date } "
65+ ),
3766 headers = headers ,
38- params = params ,
3967 )
4068
4169 inventory_data = inv .json ()
@@ -45,7 +73,9 @@ async def get_genesis_inventory(
4573 response_data = inventory_data ,
4674 )
4775 else :
48- error_message = "An error occurred with the Genesis API"
76+ error_message = (
77+ "An error occurred obtaining vehicle inventory for this search."
78+ )
4979 return error_response (
5080 error_message = error_message , error_data = inventory_data
5181 )
@@ -79,5 +109,7 @@ async def get_genesis_vin_detail(req: Request) -> dict:
79109 response_data = vin_data ,
80110 )
81111 else :
82- error_message = "An error occurred with the Genesis API"
112+ error_message = (
113+ "An error occurred obtaining VIN information for this vehicle."
114+ )
83115 return error_response (error_message = error_message , error_data = vin_data )
0 commit comments