Skip to content

Commit

Permalink
improved get_airport and get_airport_details methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanExtreme002 committed Dec 24, 2023
1 parent eadb6de commit e8a6705
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion python/FlightRadar24/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""

__author__ = "Jean Loui Bernard Silva de Jesus"
__version__ = "1.3.17"
__version__ = "1.3.19"

from .api import FlightRadar24API, FlightTrackerConfig
from .entities import Airport, Entity, Flight
30 changes: 25 additions & 5 deletions python/FlightRadar24/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def get_airport(self, code: str, *, details: bool = False) -> Airport:
:param code: ICAO or IATA of the airport
:param details: If True, it returns an Airport instance with detailed information.
"""
if 4 < len(code) or len(code) < 3:
raise ValueError(f"The code '{code}' is invalid. It must be the IATA or ICAO of the airport.")

if details:
airport = Airport()

Expand All @@ -100,9 +103,9 @@ def get_airport(self, code: str, *, details: bool = False) -> Airport:
content = response.get_content()

if not content or not isinstance(content, dict) or not content.get("details"):
raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.");
raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.")

return Airport(info=content["details"])
return Airport(info = content["details"])

def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1) -> Dict:
"""
Expand All @@ -112,6 +115,9 @@ def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1)
:param flight_limit: Limit of flights related to the airport
:param page: Page of result to display
"""
if 4 < len(code) or len(code) < 3:
raise ValueError(f"The code '{code}' is invalid. It must be the IATA or ICAO of the airport.")

request_params = {"format": "json"}

if self.__login_data is not None:
Expand All @@ -126,10 +132,24 @@ def get_airport_details(self, code: str, flight_limit: int = 100, page: int = 1)
response = APIRequest(Core.api_airport_data_url, request_params, Core.json_headers, exclude_status_codes=[400,])
content: Dict = response.get_content()

if response.get_status_code() == 400 and isinstance(content, dict) and content.get("errors"):
raise ValueError(content["errors"]["errors"]["parameters"]["limit"]["notBetween"])
if response.get_status_code() == 400 and content.get("errors"):
errors = content["errors"]["errors"]["parameters"]

if errors.get("limit"):
raise ValueError(errors["limit"]["notBetween"])

raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.", errors)

result = content["result"]["response"]

# Check whether it received data of an airport.
data = result.get("airport", dict()).get("pluginData", dict())

return content["result"]["response"]
if not "details" in data and len(data.get("runways", [])) == 0 and len(data) <= 3:
raise AirportNotFoundError(f"Could not find an airport by the code '{code}'.")

# Return the airport details.
return result

def get_airports(self) -> List[Airport]:
"""
Expand Down

0 comments on commit e8a6705

Please sign in to comment.