Skip to content

Commit

Permalink
fixing big test pb + changing API Listing method + new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
icepick4 committed Jan 29, 2023
1 parent 30995ae commit 2046850
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 38 deletions.
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.
54 changes: 28 additions & 26 deletions onepyece/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"episodes": ["id", "count", "title", "saga_id", "arc_id"],
"movies": ["id", "count", "title"],
"tomes": ["id", "count", "title"],
"chapters": ["id", "count", "title", "tome_id", "tome"],
"chapters": ["id", "count", "title", "tome_id"],
"arcs": ["id", "count", "saga_id"],
"sagas": ["id", "count", "title"],
"hakis": ["id", "count", "name", "roman_name", "characters_id"],
"characters": ["id", "count", "name", "job", "bounty", "status", "size"],
"characters": ["id", "count", "name", "job", "bounty", "status", "size", "crew_id", "fruit_id"],
"dials": ["id", "count", "name", "type"],
"luffy/gears": ["id", "count", "title"],
"luffy/techniques": ["id", "count", "name", "translation", "gear_id"],
Expand All @@ -20,50 +20,52 @@
"crews": ["id", "count", "name", "status", "yonko"],
}

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


def check_params(endpoint, search=None, resource_id=None):
def check_params(endpoint, search=None, resource=None):
if endpoint not in ENDPOINTS:
raise ValueError(f"Unknown API endpoint '{endpoint}'")
if search is not None and search not in ENDPOINTS[endpoint]:
raise ValueError(f"Unknown search '{search}' for endpoint '{endpoint}'")
if search is not None and search not in NO_RESOURCE_SEARCHES and resource_id is None:
raise ValueError("Resource ID is required for this search")
if resource_id is not None and not isinstance(resource_id, str):
raise ValueError("Resource ID must be a string, even if it's a number")
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 resource is not None and "id" in search and not isinstance(resource, int):
raise ValueError("Resource must be an integer for this search")
return None


def build_url(endpoint, search=None, resource_id=None):
check_params(endpoint, search, resource_id)
def build_url(endpoint, search=None, resource=None):
check_params(endpoint, search, resource)
if search is not None:
if resource_id is not None:
resource_id = convert_name(resource_id)
return adding_search(endpoint, search, resource_id)
if resource is not None:
resource = convert_resource(resource)
return adding_search(endpoint, search, resource)
return f"{URL}{endpoint}/{search}"
return f"{URL}{endpoint}"


def adding_search(endpoint, search, resource_id=None):
def adding_search(endpoint, search, resource=None):
if search in STRING_SEARCHES:
return f"{URL}{endpoint}/search/{search}/{resource_id}"
elif search in ID_SEARCHES or (endpoint == "arcs" and 'id' in search and search != "id"):
return f"{URL}{endpoint}/search/{search[:-3]}/{resource_id}"
elif search in NO_SEARCH_ID_SEARCHES:
return f"{URL}{endpoint}/{search[:-3]}/{resource_id}"
return f"{URL}{endpoint}/search/{search}/{resource}"
elif search in NO_SEARCH_ID_SEARCHES and endpoint not in ['boats', 'arcs']:
return f"{URL}{endpoint}/{search[:-3]}/{resource}"
elif search in ID_SEARCHES:
return f"{URL}{endpoint}/search/{search[:-3]}/{resource}"
elif search == "title":
return f"{URL}{endpoint}/search/{resource_id}"
return f"{URL}{endpoint}/{resource_id}"
return f"{URL}{endpoint}/search/{resource}"
return f"{URL}{endpoint}/{resource}"


def convert_name(name):
if ' / ' in name:
name = name.split(" / ")[0]
return name.replace(" ", "%20")
def convert_resource(resource):
if isinstance(resource, int):
return resource
if ' / ' in resource:
resource = resource.split(" / ")[0]
return resource.replace(" ", "%20")


def pretty_print(data):
Expand Down
16 changes: 16 additions & 0 deletions onepyece/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def chapter_by_title(resource):
return API("chapters", "title", resource)


def chapter_by_tome_id(resource):
return API("chapters", "tome_id", resource)


def chapter_by_tome(resource):
return API("chapters", "tome", resource)


def count_chapters():
return API("chapters", "count")

Expand Down Expand Up @@ -129,6 +137,14 @@ def character_by_size(resource):
return API("characters", "size", resource)


def character_by_crew_id(resource):
return API("characters", "crew_id", resource)


def character_by_fruit_id(resource):
return API("characters", "fruit_id", resource)


def count_characters():
return API("characters", "count")

Expand Down
30 changes: 19 additions & 11 deletions onepyece/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@


class API:
def __init__(self, endpoint, search=None, resource=None):
def __init__(self, endpoint=None, search=None, resource=None, object=False, data=None):
self.endpoint = endpoint
self.search = search
self.resource = resource
self.url = build_url(endpoint, search, resource)
self.load()
self.object = object
if endpoint is not None:
self.url = build_url(endpoint, search, resource)
if not object:
self.__load()
else:
self.__dict__.update(data)


def __repr__(self):
return f"API(endpoint={self.endpoint}, resource_id={self.resource})"
if self.object:
return f"API(object={self.__dict__})"
if self.resource is not None:
return f"API(endpoint='{self.endpoint}', search='{self.search}', resource='{self.resource}', url='{self.url}')"
if self.search is not None:
return f"API(endpoint='{self.endpoint}', search='{self.search}', url='{self.url}')"
return f"API(endpoint='{self.endpoint}', url='{self.url}')"

def __str__(self):
if getattr(self, "results", None) is not None:
for result in self.results:
print(pretty_print(result))
return ""
if getattr(self, "count", None) is not None:
return f"Total {self.endpoint} found: {self.count}"
return f"Total {self.endpoint} found for this search: {self.count}.\nTo see the results, iterate over the object."
return pretty_print(self.__dict__)

def __iter__(self):
Expand All @@ -37,12 +45,12 @@ def __len__(self):
return self.count
raise TypeError(f"Object of type {type(self)} has no len() for unique result")

def load(self):
def __load(self):
api_data = get_data(self.url)
if api_data is None:
raise ValueError(f"No data found for the given search:{self.search} and resource_id:{self.resource}")
elif isinstance(api_data, list) and len(api_data) > 1:
self.results = api_data
self.results = [API(object=True, data=result) for result in api_data]
self.count = len(api_data)
elif isinstance(api_data, list) and len(api_data) == 1:
self.__dict__.update(api_data[0])
Expand Down
Binary file modified tests/__pycache__/test_common.cpython-39.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_functions.cpython-39.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test_functions(self):
function_name = f"{current_endpoint}_by_{search}"
else:
function_name = f"{search}_{current_endpoint}s"
self.assertTrue(hasattr(functions, function_name))
self.assertTrue(hasattr(functions, function_name))

0 comments on commit 2046850

Please sign in to comment.