Skip to content

Commit

Permalink
Fix all tests + linting the project + fixing some issues about endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
icepick4 committed Feb 14, 2024
1 parent eda9c30 commit 126773b
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 127 deletions.
4 changes: 3 additions & 1 deletion onepyece/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"haki_by_id",
"haki_by_name",
"haki_by_roman_name",
"haki_by_characters_id",
"haki_by_character_id",
"count_hakis",
"character_by_id",
"character_by_name",
Expand Down Expand Up @@ -63,6 +63,8 @@
"count_fruits",
"sword_by_id",
"sword_by_name",
"sword_by_type",
"sword_by_state",
"count_swords",
"boat_by_id",
"boat_by_name",
Expand Down
Binary file modified onepyece/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified onepyece/__pycache__/api.cpython-39.pyc
Binary file not shown.
Binary file modified onepyece/__pycache__/common.cpython-39.pyc
Binary file not shown.
Binary file modified onepyece/__pycache__/functions.cpython-39.pyc
Binary file not shown.
Binary file modified onepyece/__pycache__/interface.cpython-39.pyc
Binary file not shown.
10 changes: 6 additions & 4 deletions onepyece/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def get_data(url):
:param url: The url to get the data from
:return: The data from the API or None if no data is found
"""
# response = requests.get(url, timeout=5)
# response.raise_for_status()
# return None if response.json() is None or response.json() == [] else response.json()
return 10
response = requests.get(url, timeout=5)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
return None
return None if response.json() is None or response.json() == [] else response.json()
39 changes: 26 additions & 13 deletions onepyece/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"chapters": ["id", "count", "title", "tome_id"],
"arcs": ["id", "count", "title", "saga_id"],
"sagas": ["id", "count", "title"],
"hakis": ["id", "count", "name", "roman_name", "characters_id"],
"hakis": ["id", "count", "name", "roman_name", "character_id"],
"characters": ["id", "count", "name", "job", "bounty", "status", "size", "crew_id", "fruit_id"],
"dials": ["id", "count", "name", "type"],
"luffy/gears": ["id", "count", "title"],
Expand All @@ -25,14 +25,23 @@

STRING_SEARCHES = ["name", "job", "bounty", "status", "size",
"type", "roman_name", "sea", "affiliation", "title"]
ID_SEARCHES = ["saga_id", "arc_id", "characters_id",
"gear_id", "tome_id", "fruit_id", "crew_id", "crew_id", "captain_id"]
ID_SEARCHES = [
"saga_id",
"arc_id",
"character_id",
"gear_id",
"tome_id",
"fruit_id",
"crew_id",
"crew_id",
"captain_id"
]
NO_RESOURCE_SEARCHES = ["count", "yonko"]

AUTHORIZED_LANGS = ['fr', 'en']


def check_params(endpoint, search=None, resource=None):
def check_params(endpoint, lang="en", search=None, resource=None):
"""
Check if the parameters are valid for the API
Expand All @@ -47,11 +56,14 @@ def check_params(endpoint, search=None, resource=None):
:return: None if the parameters are valid
:raises ValueError: If the parameters are not valid
"""
endpoint_no_lang = endpoint.split('/')[0]
if endpoint_no_lang not in ENDPOINTS:
raise ValueError(f"Unknown API endpoint '{endpoint_no_lang}', authorized endpoints are {[key for key in ENDPOINTS]}")
if search is not None and search not in ENDPOINTS[endpoint_no_lang]:
raise ValueError(f"Unknown search '{search}' for endpoint '{endpoint_no_lang}', authorized searches are {[key for key in ENDPOINTS[endpoint_no_lang]]}")
if lang not in AUTHORIZED_LANGS:
raise ValueError(f"Unknown language '{lang}', authorized languages are {AUTHORIZED_LANGS}")
if endpoint not in ENDPOINTS:
raise ValueError(f"Unknown API endpoint '{endpoint}', authorized endpoints are {[key for key in ENDPOINTS]}")
if search is not None and search not in ENDPOINTS[endpoint]:
raise ValueError(f"""Unknown search '{search}' for endpoint '{endpoint}',
authorized searches are {[key for key in ENDPOINTS[endpoint]]}
""")
if search is not None and search not in NO_RESOURCE_SEARCHES and resource is None:
raise ValueError("Resource is required for this search")
if search in [NO_RESOURCE_SEARCHES] and resource is not None:
Expand All @@ -60,7 +72,7 @@ def check_params(endpoint, search=None, resource=None):
raise ValueError("Resource must be an integer for this search")


def build_url(endpoint, search=None, resource=None):
def build_url(endpoint, lang="en", search=None, resource=None):
"""
Build the url for the API
Expand All @@ -72,7 +84,8 @@ def build_url(endpoint, search=None, resource=None):
:return: The url for the API
"""
check_params(endpoint, search, resource)
check_params(endpoint, lang, search, resource)
endpoint = f"{endpoint}/{lang}" if lang is not None else f"{endpoint}/en"
if search is not None:
if resource is not None:
resource = convert_resource(resource)
Expand All @@ -97,10 +110,10 @@ def adding_search(endpoint, search, resource=None):
"""
# there are some exceptions to the rule because of the API
if search in STRING_SEARCHES:
return f"{URL}{endpoint}/search/?{search}={resource}"
return f"{URL}{endpoint}/search?{search}={resource}"

if search in ID_SEARCHES:
return f"{URL}{endpoint}/{search[:-3]}/{resource}/"
return f"{URL}{endpoint}/{search[:-3]}/{resource}"

return f"{URL}{endpoint}/{resource}"

Expand Down
Loading

0 comments on commit 126773b

Please # to comment.