Skip to content

Instantly share code, notes, and snippets.

@sameerkumar18
Created May 12, 2019 15:37
Show Gist options
  • Save sameerkumar18/2a1b9ba40cf0c388f341801218c77a33 to your computer and use it in GitHub Desktop.
Save sameerkumar18/2a1b9ba40cf0c388f341801218c77a33 to your computer and use it in GitHub Desktop.
Shopify 429, 5XX and malformed JSON patch
# 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', ])
@sameerkumar18
Copy link
Author

This function needs to be called only once when the server is loaded for the first time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment