Skip to content

Commit

Permalink
refactor: reduce code duplication in vehicle response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tigattack committed Aug 20, 2024
1 parent a486d46 commit a57d13e
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions dvsa_mot_history/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,47 +78,36 @@ async def _make_api_request(self, url: str) -> Union[Dict[str, Any], ErrorRespon
status_code=response.status, message="Unknown error", errors=None
)

async def get_vehicle_history_by_registration(
self, registration: str
async def _process_vehicle_history_response(
self, response_json: Dict[str, Any] | ErrorResponse
) -> Union[VehicleWithMotResponse, NewRegVehicleResponse, ErrorResponse]:
"""Get MOT history for a vehicle by registration."""
url = build_url(VEHICLE_BY_REGISTRATION, registration=registration)
response_json = await self._make_api_request(url)

"""Process the vehicle history response."""
if isinstance(response_json, ErrorResponse):
return response_json

classified_response: Union[VehicleWithMotResponse, NewRegVehicleResponse]

if hasattr(response_json, "motTests"):
if "motTests" in response_json:
response_json["motTests"] = await try_cast_mot_class(response_json)
classified_response = VehicleWithMotResponse(**response_json)
return VehicleWithMotResponse(**response_json)
elif "motTestDueDate" in response_json:
return NewRegVehicleResponse(**response_json)

else:
classified_response = NewRegVehicleResponse(**response_json)
raise ValueError("Unexpected response format")

return classified_response
async def get_vehicle_history_by_registration(
self, registration: str
) -> Union[VehicleWithMotResponse, NewRegVehicleResponse, ErrorResponse]:
"""Get MOT history for a vehicle by registration."""
url = build_url(VEHICLE_BY_REGISTRATION, registration=registration)
response_json = await self._make_api_request(url)
return await self._process_vehicle_history_response(response_json)

async def get_vehicle_history_by_vin(
self, vin: str
) -> Union[VehicleWithMotResponse, NewRegVehicleResponse, ErrorResponse]:
"""Get MOT history for a vehicle by VIN."""
url = build_url(VEHICLE_BY_VIN, vin=vin)
response_json = await self._make_api_request(url)

if isinstance(response_json, ErrorResponse):
return response_json

classified_response: Union[VehicleWithMotResponse, NewRegVehicleResponse]

if hasattr(response_json, "motTests"):
response_json["motTests"] = await try_cast_mot_class(response_json)
classified_response = VehicleWithMotResponse(**response_json)

else:
classified_response = NewRegVehicleResponse(**response_json)

return classified_response
return await self._process_vehicle_history_response(response_json)

async def get_bulk_download(self) -> Union[BulkDownloadResponse, ErrorResponse]:
"""Get MOT history in bulk."""
Expand Down

0 comments on commit a57d13e

Please sign in to comment.