Created
May 12, 2019 15:37
-
-
Save sameerkumar18/2a1b9ba40cf0c388f341801218c77a33 to your computer and use it in GitHub Desktop.
Shopify 429, 5XX and malformed JSON patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For handling Shopify 429 - https://gist.github.com/wowkin2/079844c867a1a06ce15ea1e4ffdee87c | |
def patch_shopify(): | |
connection_func = ShopifyConnection._open | |
decode_func = formats.JSONFormat.decode | |
def patch_decode(resource_string): | |
count_format_error = 0 | |
while count_format_error <= 2: | |
try: | |
return decode_func(resource_string) | |
except formats.Error as err: | |
retry_after = 1 | |
log.info(f'Shopify wrapper exception due to malformed JSON response {err}' | |
f' will retry to send request in {retry_after} seconds') | |
time.sleep(retry_after) | |
if count_format_error > 2: | |
log.info(f'JSON Error Patch Maximum Retries Executed. Final Count - {count_format_error}') | |
raise err | |
else: | |
log.info(f'JSON Error Patch Retrying. Current Count - {count_format_error}') | |
count_format_error += 1 | |
formats.JSONFormat.decode = patch_decode | |
def patched_open(self, *args, **kwargs): | |
count_client_error = 0 | |
count_server_error = 0 | |
while True: | |
try: | |
# Raise an error here to test this patch [Example - raise pyactiveresource.formats.Error] | |
return connection_func(self, *args, **kwargs) | |
except pyactiveresource.connection.ClientError as ce: | |
if ce.response.code == 429: | |
# Checks for Retry-After header in 429 else defaults to 4 sec | |
retry_after = float(ce.response.headers.get('Retry-After', 4)) | |
log.info(f'Service exceeds Shopify API call limit, ' | |
f'will retry to send request in {retry_after} seconds') | |
time.sleep(retry_after) | |
if count_client_error >= 15: | |
log.info(f'Client Error Patch Maximum Retries Executed. Final Count - {count_client_error}') | |
raise ce | |
else: | |
log.info(f'Client Error Patch Retrying. Current Count - {count_client_error}') | |
count_client_error += 1 | |
else: | |
raise ce | |
except pyactiveresource.connection.ServerError as se: | |
# For handling Server Errors like 500, 502, 503 etc | |
if 500 <= se.code <= 599: | |
retry_after = 1 | |
log.info(f'Server responded with error code {se.code}' | |
f'will retry to send request in {retry_after} seconds') | |
time.sleep(retry_after) | |
if count_server_error >= 5: | |
log.info(f'Server Error Patch Maximum Retries Executed. Final Count - {count_server_error}') | |
raise se | |
else: | |
log.info(f'Server Error Patch Retrying. Current Count - {count_server_error}') | |
count_server_error += 1 | |
else: | |
raise se | |
ShopifyConnection._open = patched_open | |
patch_shopify() | |
@api_view(http_method_names=['post', ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function needs to be called only once when the server is loaded for the first time.