def __init__(self, base_url, api_key=None, tz_offset=None, max_retries=5, verify=True): assert base_url self.base_url = base_url self.max_retries = max_retries self.tz_offset = str(tz_offset or local_tz_offset()) self.verify = verify self.session = requests.Session() if api_key: self.session.auth = (api_key, '') self.session.headers.update({ 'Content-Type': 'application/json', 'X-TZ-Offset': self.tz_offset })
def __init__(self, base_url, api_key=None, tz_offset=None, max_retries=5, verify=True): assert base_url self.base_url = base_url self.max_retries = max_retries self.tz_offset = str(tz_offset or local_tz_offset()) self.verify = verify self.session = requests.Session() if api_key: self.session.auth = (api_key, '') self.session.headers.update({ 'User-Agent': 'Close/{} python ({})'.format( __version__, requests.utils.default_user_agent() ), 'X-TZ-Offset': self.tz_offset })
self.field_errors = data.get('field-errors', {}) class API(object): def __init__(self, base_url, api_key=None, tz_offset=None, async=False, max_retries=5, verify=True): assert base_url self.base_url = base_url self. async = async self.max_retries = max_retries self.tz_offset = tz_offset or str(local_tz_offset()) self.verify = verify if async: import grequests self.requests = grequests else: self.requests = requests self.session = self.requests.Session() if api_key: self.session.auth = (api_key, '') self.session.headers.update({ 'Content-Type': 'application/json', 'X-TZ-Offset': self.tz_offset })
class APIError(Exception): pass class API(object): def __init__(self, base_url, api_key, tz_offset=None, async=False, max_retries=5): assert base_url self.base_url = base_url self.api_key = api_key self.async = async self.max_retries = max_retries if async: import grequests self.requests = grequests else: self.requests = requests.Session() self.tz_offset = tz_offset or local_tz_offset() def dispatch(self, method_name, endpoint, data=None): method = getattr(self.requests, method_name) retries = 0 while True and retries < self.max_retries: try: response = method( self.base_url+endpoint, data=data != None and json.dumps(data), auth=(self.api_key, ''), headers={'Content-Type': 'application/json', 'X-TZ-Offset': self.tz_offset} ) break except requests.exceptions.ConnectionError:
import six from six.moves.urllib.parse import urlencode class APIError(Exception): pass class API(object): def __init__(self, base_url, api_key, tz_offset=None, async=False, max_retries=5): assert base_url self.base_url = base_url self.async = async self.max_retries = max_retries self.tz_offset = tz_offset or str(local_tz_offset()) if async: import grequests self.requests = grequests else: self.requests = requests self.session = self.requests.Session() self.session.auth = (api_key, '') self.session.headers.update({'Content-Type': 'application/json', 'X-TZ-Offset': self.tz_offset}) def _print_request(self, request): print('{}\n{}\n{}\n\n{}\n{}'.format( '----------- HTTP Request -----------',