Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pyEnsemblRest #20

Merged
merged 11 commits into from
Nov 22, 2024
Prev Previous commit
Next Next commit
Refactor fixes and more tests.
  • Loading branch information
gawbul committed Nov 21, 2024
commit e288b5399e989dcbbe6bfc041a7cdb3257f8e875
25 changes: 15 additions & 10 deletions pyensemblrest/ensemblrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@

# FakeResponse object
class FakeResponse(object):
def __init__(self, response: Response | Any, text: str = ""):
self.headers: CaseInsensitiveDict[str] | dict[str, Any] = response.headers
self.status_code: int = 400
def __init__(
self,
headers: CaseInsensitiveDict[str] | dict[str, Any],
status_code: int,
text: str,
):
self.headers = headers
self.status_code = status_code
self.text: str = text


Expand Down Expand Up @@ -65,7 +70,7 @@ def __init__(
self.last_data: dict[Any, Any] = {}
self.last_method: str = ""
self.last_attempt: int = 0
self.last_response: Response | FakeResponse
self.last_response: Response | FakeResponse = Response()

# the maximum number of attempts
self.max_attempts: int = 5
Expand Down Expand Up @@ -270,7 +275,7 @@ def __get_response(self) -> Response | FakeResponse:
self.req_count = 0

# my response
resp: Response | FakeResponse
resp: Response | FakeResponse = Response()

# deal with exceptions
try:
Expand Down Expand Up @@ -301,8 +306,9 @@ def __get_response(self) -> Response | FakeResponse:

# create a fake response in order to redo the query
resp = FakeResponse(
self.last_response,
json.dumps(
headers=self.last_response.headers,
status_code=400,
text=json.dumps(
{"message": repr(e), "error": "%s timeout" % ensembl_user_agent}
),
)
Expand Down Expand Up @@ -339,7 +345,6 @@ def parseResponse(
# Handle content in different way relying on content-type
if content_type == "application/json":
content = json.loads(resp.text)

else:
# Default
content = resp.text
Expand Down Expand Up @@ -392,7 +397,7 @@ def __check_retry(self, resp: Response | FakeResponse) -> bool:
@staticmethod
def __get_rate_limit(
headers: CaseInsensitiveDict[str] | dict[str, Any],
) -> tuple[int | None, int | None, int | None, float | None, None]:
) -> tuple[int | None, int | None, int | None, float | None, int | None]:
"""Read rate limited attributes"""

# initialize some values
Expand All @@ -410,7 +415,7 @@ def __get_rate_limit(
logger.debug("X-RateLimit-Reset: %s" % rate_reset)

if "X-RateLimit-Period".lower() in keys:
rate_reset = int(headers["X-RateLimit-Period"])
rate_period = int(headers["X-RateLimit-Period"])
logger.debug("X-RateLimit-Period: %s" % rate_period)

if "X-RateLimit-Limit".lower() in keys:
Expand Down
Loading