Skip to content

Commit

Permalink
refactor: Use http.HTTPStatus in more places (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon authored Dec 16, 2024
1 parent 7c159b1 commit 95f96da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
27 changes: 14 additions & 13 deletions tap_intacct/sage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""API Base class with util functions""" # noqa: D415

import datetime as dt
import http
import json
import logging
import re
Expand Down Expand Up @@ -181,23 +182,23 @@ def _post_request(self, dict_body: dict, api_url: str) -> dict: # noqa: C901, P
parsed_xml = xmltodict.parse(response.text)
parsed_response = json.loads(json.dumps(parsed_xml))
except: # noqa: E722
if response.status_code == 502: # noqa: PLR2004
if response.status_code == http.HTTPStatus.BAD_GATEWAY: # 502
raise BadGatewayError( # noqa: B904, TRY003
f"Response status code: {response.status_code}, response: {response.text}" # noqa: EM102
)
if response.status_code == 503: # noqa: PLR2004
if response.status_code == http.HTTPStatus.SERVICE_UNAVAILABLE: # 503
raise OfflineServiceError( # noqa: B904, TRY003
f"Response status code: {response.status_code}, response: {response.text}" # noqa: EM102
)
if response.status_code == 429: # noqa: PLR2004
if response.status_code == http.HTTPStatus.TOO_MANY_REQUESTS: # 429
raise RateLimitError( # noqa: B904, TRY003
f"Response status code: {response.status_code}, response: {response.text}" # noqa: EM102
)
raise InvalidXmlResponse( # noqa: B904, TRY003
f"Response status code: {response.status_code}, response: {response.text}" # noqa: EM102
)

if response.status_code == 200: # noqa: PLR2004
if response.status_code == http.HTTPStatus.OK: # 200
if parsed_response["response"]["control"]["status"] == "success":
api_response = parsed_response["response"]["operation"]

Expand Down Expand Up @@ -240,38 +241,38 @@ def _post_request(self, dict_body: dict, api_url: str) -> dict: # noqa: C901, P
exception_msg = parsed_response.get("response", {}).get("errormessage", {}).get("error", {})
correction = exception_msg.get("correction", {}) # type: ignore[union-attr]

if response.status_code == 400: # noqa: PLR2004
if response.status_code == http.HTTPStatus.BAD_REQUEST: # 400
if exception_msg.get("errorno") == "GW-0011": # type: ignore[union-attr]
raise AuthFailure( # noqa: TRY003
f"One or more authentication values are incorrect. Response:{parsed_response}" # noqa: EM102
)
raise InvalidRequest("Invalid request", parsed_response) # noqa: EM101, TRY003

if response.status_code == 401: # noqa: PLR2004
if response.status_code == http.HTTPStatus.UNAUTHORIZED: # 401
raise InvalidTokenError( # noqa: TRY003
f"Invalid token / Incorrect credentials. Response: {parsed_response}" # noqa: EM102
)

if response.status_code == 403: # noqa: PLR2004
if response.status_code == http.HTTPStatus.FORBIDDEN: # 403
raise NoPrivilegeError( # noqa: TRY003
f"Forbidden, the user has insufficient privilege. Response: {parsed_response}" # noqa: EM102
)

if response.status_code == 404: # noqa: PLR2004
if response.status_code == http.HTTPStatus.NOT_FOUND: # 404
raise NotFoundItemError( # noqa: TRY003
f"Not found item with ID. Response: {parsed_response}" # noqa: EM102
)

if response.status_code == http.HTTPStatus.INTERNAL_SERVER_ERROR: # 500
raise InternalServerError( # noqa: TRY003
f"Internal server error. Response: {parsed_response}" # noqa: EM102
)

if response.status_code == 498: # noqa: PLR2004
raise ExpiredTokenError( # noqa: TRY003
f"Expired token, try to refresh it. Response: {parsed_response}" # noqa: EM102
)

if response.status_code == 500: # noqa: PLR2004
raise InternalServerError( # noqa: TRY003
f"Internal server error. Response: {parsed_response}" # noqa: EM102
)

if correction and "Please Try Again Later" in correction:
raise PleaseTryAgainLaterError(parsed_response)

Expand Down
6 changes: 2 additions & 4 deletions tap_intacct/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def __init__(
i for i, t in self.schema["properties"].items() if t.get("format", "") == "date-time"
]
self.numeric_fields = [
i
for i, t in self.schema["properties"].items()
if "number" in t.get("type", "")
i for i, t in self.schema["properties"].items() if "number" in t.get("type", "")
]

@property
Expand Down Expand Up @@ -275,7 +273,7 @@ def prepare_request_payload(
A dictionary with the JSON body for a POST requests.
"""
if self.name == "audit_history":
raise Exception("TODO hanlde audit streams") # noqa: EM101, TRY002, TRY003
raise Exception("TODO handle audit streams") # noqa: EM101, TRY002, TRY003

rep_key = REP_KEYS.get(self.name, GET_BY_DATE_FIELD)
orderby = {
Expand Down

0 comments on commit 95f96da

Please sign in to comment.