Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pass
time.sleep(0.5)
retries += 1
if retries >= num_retries:
self.assertTrue(False, error_description)
return False
api = Node("localhost", 11224, "test_token")
online_node = ProcessingNode.objects.get(pk=3)
self.assertTrue(online_node.update_node_info(), "Could update info")
# Cannot call info(), options() without tokens
api.token = "invalid"
self.assertRaises(NodeResponseError, api.info)
self.assertRaises(NodeResponseError, api.options)
# Cannot call create_task() without token
import glob
self.assertRaises(NodeResponseError, api.create_task, glob.glob("nodeodm/fixtures/test_images/*.JPG"))
# Can call create_task() with token
api.token = "test_token"
res = api.create_task(
glob.glob("nodeodm/fixtures/test_images/*.JPG"))
uuid = res.uuid
self.assertTrue(uuid != None)
# Can call task_info() with token
task_info = api.get_task(uuid).info()
self.assertTrue(isinstance(task_info.date_created, datetime))
def post(self, url, data=None, headers={}):
try:
res = requests.post(self.url(url), data=data, headers=headers, timeout=self.timeout)
if res.status_code == 401:
raise NodeResponseError("Unauthorized. Do you need to set a token?")
elif res.status_code != 200 and res.status_code != 403:
raise NodeServerError(res.status_code)
if "Content-Type" in res.headers and "application/json" in res.headers['Content-Type']:
result = res.json()
if 'error' in result:
raise NodeResponseError(result['error'])
return result
else:
return res
except json.decoder.JSONDecodeError as e:
raise NodeServerError(str(e))
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
raise NodeConnectionError(str(e))
def get(self, url, query={}, **kwargs):
try:
res = requests.get(self.url(url, query), timeout=self.timeout, **kwargs)
if res.status_code == 401:
raise NodeResponseError("Unauthorized. Do you need to set a token?")
elif not res.status_code in [200, 403, 206]:
raise NodeServerError("Unexpected status code: %s" % res.status_code)
if "Content-Type" in res.headers and "application/json" in res.headers['Content-Type']:
result = res.json()
if 'error' in result:
raise NodeResponseError(result['error'])
return result
else:
return res
except json.decoder.JSONDecodeError as e:
raise NodeServerError(str(e))
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
raise NodeConnectionError(str(e))
def handle_task_new_response(self, result):
if isinstance(result, dict) and 'uuid' in result:
return Task(self, result['uuid'])
elif isinstance(result, dict) and 'error' in result:
raise NodeResponseError(result['error'])
else:
raise NodeServerError('Invalid response: ' + str(result))
def get(self, url, query={}, **kwargs):
try:
res = requests.get(self.url(url, query), timeout=self.timeout, **kwargs)
if res.status_code == 401:
raise NodeResponseError("Unauthorized. Do you need to set a token?")
elif not res.status_code in [200, 403, 206]:
raise NodeServerError("Unexpected status code: %s" % res.status_code)
if "Content-Type" in res.headers and "application/json" in res.headers['Content-Type']:
result = res.json()
if 'error' in result:
raise NodeResponseError(result['error'])
return result
else:
return res
except json.decoder.JSONDecodeError as e:
raise NodeServerError(str(e))
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
raise NodeConnectionError(str(e))
def post(self, url, data):
result = self.node.post(url, data)
if isinstance(result, dict) and 'error' in result:
raise NodeResponseError(result['error'])
return result
try:
file = task['file']
fields = {
'images': [(os.path.basename(file), read_file(file), (mimetypes.guess_type(file)[0] or "image/jpg"))]
}
e = MultipartEncoder(fields=fields)
result = self.post('/task/new/upload/{}'.format(uuid), data=e, headers={'Content-Type': e.content_type})
if isinstance(result, dict) and 'success' in result and result['success']:
uf = nonloc.uploaded_files.increment()
if progress_event is not None:
progress_event.set()
else:
if isinstance(result, dict) and 'error' in result:
raise NodeResponseError(result['error'])
else:
raise NodeServerError("Failed upload with unexpected result: %s" % str(result))
except OdmError as e:
if task['retries'] < max_retries:
# Put task back in queue
task['retries'] += 1
task['wait_until'] = datetime.datetime.now() + datetime.timedelta(seconds=task['retries'] * retry_timeout)
q.put(task)
else:
nonloc.error = e
except Exception as e:
nonloc.error = e
finally:
q.task_done()
def download_zip(self, destination, progress_callback=None, parallel_downloads=16, parallel_chunks_size=10):
"""Download this task's assets archive to a directory.
Args:
destination (str): directory where to download assets archive. If the directory does not exist, it will be created.
progress_callback (function): an optional callback with one parameter, the download progress percentage.
parallel_downloads (int): maximum number of parallel downloads if the node supports http range.
parallel_chunks_size (int): size in MB of chunks for parallel downloads
Returns:
str: path to archive file (.zip)
"""
info = self.info()
if info.status != TaskStatus.COMPLETED:
raise NodeResponseError("Cannot download task, task status is " + str(info.status))
if not os.path.exists(destination):
os.makedirs(destination, exist_ok=True)
try:
download_stream = self.get('/task/{}/download/all.zip'.format(self.uuid), stream=True)
headers = download_stream.headers
zip_path = os.path.join(destination, "{}_{}_all.zip".format(self.uuid, int(time.time())))
# Keep track of download progress (if possible)
content_length = download_stream.headers.get('content-length')
total_length = int(content_length) if content_length is not None else None
downloaded = 0
chunk_size = int(parallel_chunks_size * 1024 * 1024)
use_fallback = False