diff --git a/README.md b/README.md index 54169a1..59c424d 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,8 @@ from semantics3 import Products # Set up a client to talk to the Semantics3 API using your Semantics3 API Credentials sem3 = Products( - api_key = "SEM3xxxxxxxxxxxxxxxxxxxxxx", - api_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + api_key=os.environ['SEMANTICS3_API_KEY'], + api_secret=os.environ['SEMANTICS3_API_SECRET'] ) ``` @@ -94,6 +94,31 @@ for i in sem3.iter(): sleep(1) #respect rate limit ``` +### Request Timeout + +The RealTime API has a hard server-side timeout of **60s** (up to **120s** for some customers) by default after which the client request will be aborted. You can +override this behaviour by setting a client-side timeout value as shown below: + +```python +import os +from requests.exceptions import ReadTimeout, ConnectTimeout +from semantics3 import Products + +sem3 = Products( + api_key=os.environ['SEMANTICS3_API_KEY'], + api_secret=os.environ['SEMANTICS3_API_SECRET'], + timeout=5 +) + +try: + sem3.run_query('realtime/skus', 'GET', { 'url': 'https://www.walmart.com/ip/325408104' }) +except (ReadTimeout, ConnectTimeout): + print('request timed out in 5s') + +``` + + + ### UPC Query Running a UPC/EAN/GTIN query is as simple as running a search query: diff --git a/semantics3/products.py b/semantics3/products.py index 6bc7452..a07d6b0 100644 --- a/semantics3/products.py +++ b/semantics3/products.py @@ -5,8 +5,8 @@ class Products(Semantics3Request): - def __init__(self, api_key, api_secret, api_base='https://api.semantics3.com/v1/'): - Semantics3Request.__init__(self, api_key, api_secret, 'products', api_base) + def __init__(self, api_key, api_secret, api_base='https://api.semantics3.com/v1/', timeout=120): + Semantics3Request.__init__(self, api_key, api_secret, 'products', api_base, timeout) def get_products(self): return self.get() diff --git a/semantics3/semantics3.py b/semantics3/semantics3.py index 5b36b48..c6657e0 100644 --- a/semantics3/semantics3.py +++ b/semantics3/semantics3.py @@ -15,7 +15,7 @@ class Semantics3Request: - def __init__(self, api_key=None, api_secret=None, endpoint=None, api_base='https://api.semantics3.com/v1/'): + def __init__(self, api_key=None, api_secret=None, endpoint=None, api_base='https://api.semantics3.com/v1/', timeout=120): if api_key is None: raise Semantics3Error( 'API Credentials Missing', @@ -36,6 +36,7 @@ def __init__(self, api_key=None, api_secret=None, endpoint=None, api_base='https self.query_result = None self.cache_size = 10 self.api_base = api_base + self.timeout = timeout def fetch(self, method, endpoint, params): api_endpoint = url_normalize(self.api_base + endpoint) @@ -44,14 +45,16 @@ def fetch(self, method, endpoint, params): method, api_endpoint, params = params, - headers={'User-Agent':'Semantics3 Python Lib/0.2'} + headers={'User-Agent':'Semantics3 Python Lib/0.2'}, + timeout=self.timeout ) else: content = self.oauth.request( method, api_endpoint, data = json.dumps(params), - headers={'User-Agent':'Semantics3 Python Lib/0.2', 'Content-Type':'application/json'} + headers={'User-Agent':'Semantics3 Python Lib/0.2', 'Content-Type':'application/json'}, + timeout=self.timeout ) return content