add ap date format and fix default client for header val
This commit is contained in:
parent
6ae253b40d
commit
a731490af4
|
@ -33,7 +33,6 @@ class httpClient:
|
||||||
def _fetch(self, url, headers={}, method='GET', data=None, cached=True):
|
def _fetch(self, url, headers={}, method='GET', data=None, cached=True):
|
||||||
cached_data = self.cache.fetch(url)
|
cached_data = self.cache.fetch(url)
|
||||||
#url = url.split('#')[0]
|
#url = url.split('#')[0]
|
||||||
#print(url)
|
|
||||||
|
|
||||||
if cached and cached_data:
|
if cached and cached_data:
|
||||||
logging.debug(f'Returning cached data for {url}')
|
logging.debug(f'Returning cached data for {url}')
|
||||||
|
@ -61,7 +60,8 @@ class httpClient:
|
||||||
resp = self.client.request(method, url, headers=headers)
|
resp = self.client.request(method, url, headers=headers)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ConnectionError(f'Failed to fetch url: {e}')
|
logging.debug(f'Failed to fetch url: {e}')
|
||||||
|
return
|
||||||
|
|
||||||
if cached:
|
if cached:
|
||||||
logging.debug(f'Caching {url}')
|
logging.debug(f'Caching {url}')
|
||||||
|
@ -90,20 +90,19 @@ class httpClient:
|
||||||
'''
|
'''
|
||||||
Return the body as a dict if it's json
|
Return the body as a dict if it's json
|
||||||
'''
|
'''
|
||||||
|
|
||||||
headers = kwargs.get('headers')
|
headers = kwargs.get('headers')
|
||||||
|
|
||||||
if not headers:
|
if not headers:
|
||||||
kwargs['headers'] = {}
|
kwargs['headers'] = {}
|
||||||
|
|
||||||
kwargs['headers'].update({'Accept': 'application/json'})
|
kwargs['headers'].update({'Accept': 'application/json'})
|
||||||
|
|
||||||
resp = self._fetch(*args, **kwargs)
|
resp = self._fetch(*args, **kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(resp.data.decode())
|
data = json.loads(resp.data.decode())
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(resp.data.decode())
|
|
||||||
logging.debug(f'Failed to load json: {e}')
|
logging.debug(f'Failed to load json: {e}')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -149,11 +148,11 @@ def ValidateSignature(headers, method, path, client=None, agent=None):
|
||||||
headers (dict): All of the headers to be used to check a signature. The signature header must be included too
|
headers (dict): All of the headers to be used to check a signature. The signature header must be included too
|
||||||
method (str): The http method used in relation to the headers
|
method (str): The http method used in relation to the headers
|
||||||
path (str): The path of the request in relation to the headers
|
path (str): The path of the request in relation to the headers
|
||||||
client (pool object): Specify a urllib3 pool to use for fetching the actor. optional
|
client (pool object): Specify a httpClient to use for fetching the actor. optional
|
||||||
agent (str): User agent used for fetching actor data. optional
|
agent (str): User agent used for fetching actor data. optional
|
||||||
'''
|
'''
|
||||||
|
|
||||||
client = httpClient(agent) if not client else client
|
client = httpClient(agent=agent) if not client else client
|
||||||
headers = {k.lower(): v for k,v in headers.items()}
|
headers = {k.lower(): v for k,v in headers.items()}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -38,8 +38,12 @@ def randomgen(chars=20):
|
||||||
return ''.join(random.choices(string.ascii_letters + string.digits, k=chars))
|
return ''.join(random.choices(string.ascii_letters + string.digits, k=chars))
|
||||||
|
|
||||||
|
|
||||||
def formatUTC(timestamp=None):
|
def formatUTC(timestamp=None, ap=False):
|
||||||
date = datetime.fromtimestamp(timestamp) if timestamp else datetime.utcnow()
|
date = datetime.fromtimestamp(timestamp) if timestamp else datetime.utcnow()
|
||||||
|
|
||||||
|
if ap:
|
||||||
|
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
|
||||||
return date.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
return date.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue