Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _create_prelim(self):
"""
Step 0: Register intent to upload files
"""
self._verify(self.payload)
if "key" in self.payload[0] and self.payload[0]["key"]:
if next((i for i in self.payload if "key" not in i), False):
raise ze.UnsupportedParams(
"Can't pass payload entries with and without keys to Zupload"
)
return None # Don't do anything if payload comes with keys
liblevel = "/{t}/{u}/items"
# Create one or more new attachments
headers = {"Zotero-Write-Token": token(), "Content-Type": "application/json"}
headers.update(self.zinstance.default_headers())
# If we have a Parent ID, add it as a parentItem
if self.parentid:
for child in self.payload:
child["parentItem"] = self.parentid
to_send = json.dumps(self.payload)
self.zinstance._check_backoff()
req = requests.post(
url=self.zinstance.endpoint
+ liblevel.format(
rul.append( child['data']['url'] )
if child['data']['itemType'] == 'attachment':
if child['data']['linkMode'] == "imported_file":
res.append({
'md5' : child['data']['md5'],
'fname' : child['data']['filename']
})
elif child['data']['linkMode'] == "linked_file":
res.append({
'path' : child['data']['path']
})
except zotero_errors.UnsupportedParams:
pass
return (res, rul)
def retry_zotero(wrapped, _instance, args, kwargs):
"""
Retry the wrapped function if the Zotero API call fails.
Caution: This decorator should only be used on simple functions, as the
whole function is called repeatedly as long a Zotero fails.
"""
attempts = 1
while True:
try:
return wrapped(*args, **kwargs)
except (
requests.exceptions.ConnectionError,
zotero_errors.HTTPError,
zotero_errors.UnsupportedParams
) as e:
current_app.logger.exception(e)
if attempts < current_app.config['KERKO_ZOTERO_MAX_ATTEMPTS']:
current_app.logger.warning(
"The Zotero API call has failed in {func}. "
"New attempt in {wait} seconds...".format(
func=wrapped.__name__,
wait=current_app.config['KERKO_ZOTERO_WAIT']
)
)
attempts += 1
sleep(current_app.config['KERKO_ZOTERO_WAIT'])
else:
current_app.logger.error(
"The maximum number of API call attempts to Zotero has "
"been reached. Stopping."
def error_handler(req):
""" Error handler for HTTP requests
"""
error_codes = {
400: ze.UnsupportedParams,
401: ze.UserNotAuthorised,
403: ze.UserNotAuthorised,
404: ze.ResourceNotFound,
409: ze.Conflict,
412: ze.PreConditionFailed,
413: ze.RequestEntityTooLarge,
428: ze.PreConditionRequired,
429: ze.TooManyRequests,
}
def err_msg(req):
""" Return a nicely-formatted error message
"""
return "\nCode: %s\nURL: %s\nMethod: %s\nResponse: %s" % (
req.status_code,
# error.msg,