1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
import shopify
class Limits(object):
"""
API Calls Limit
https://help.shopify.com/en/api/getting-started/api-call-limit
Conversion of lib/shopify_api/limits.rb
"""
# num_requests_executed/max_requests
# Eg: 1/40
CREDIT_LIMIT_HEADER_PARAM = "X-Shopify-Shop-Api-Call-Limit"
@classmethod
def response(cls):
if not shopify.Shop.connection.response:
shopify.Shop.current()
return shopify.Shop.connection.response
@classmethod
def api_credit_limit_param(cls):
response = cls.response()
_safe_header = getattr(response, "headers", "")
if not _safe_header:
raise Exception("No shopify headers found")
if cls.CREDIT_LIMIT_HEADER_PARAM in response.headers:
credits = response.headers[cls.CREDIT_LIMIT_HEADER_PARAM]
return credits.split("/")
else:
raise Exception("No valid api call header found")
@classmethod
def credit_left(cls):
"""
How many more API calls can I make?
"""
return int(cls.credit_limit() - cls.credit_used())
@classmethod
def credit_maxed(cls):
"""
Have I reached my API call limit?
"""
return bool(cls.credit_left() <= 0)
@classmethod
def credit_limit(cls):
"""
How many total API calls can I make?
"""
return int(cls.api_credit_limit_param()[1])
@classmethod
def credit_used(cls):
"""
How many API calls have I made?
"""
return int(cls.api_credit_limit_param()[0])
|