def get(self, job_id=None): if job_id is None: job_id = self.job_id url = '{}/{}'.format(self.url, job_id) response = self.session.get(url, headers={'content-type': 'application/json'}) Util.raise_detailed_error(response) return response.json()
def _login(self, email: str, password: str, token: str = None): self.session.cookies.clear() self.session = Util.mount_standard_session(self.session) headers = {} body = { 'email': email, 'password': password, } if token: body['token'] = token response = self.session.post( url='{}/api/auth'.format(self.url), json=body, headers=headers, ) Util.raise_detailed_error(response) self.user = response.json() # Persist so can quickly run again without needing to enter 2FA token. self.save_session_to_disk()
def download_original_file(self, entity_id: int, destination_filename: str) -> str: """ Download the originally uploaded file corresponding to a PipeBio document. Two requests are made: 1. Request a signed url for this document (GET /api/v2/entities/:id/original) 2. Download the data from that signed url (GET <result-from-step-1>) """ # First request a signed url from PipeBio. signed_url_response = self.session.get( '{}/api/v2/entities/{}/original'.format(self.url, entity_id), ) # Did the signed-url request work ok? Util.raise_detailed_error(signed_url_response) # Parse the results to get the signed url. download_url = signed_url_response.json()['url'] # Download the original file. download_response = requests.get(download_url) # Did the download request work ok? Util.raise_detailed_error(download_response) # Write the result to disk in chunks. with open(destination_filename, 'wb') as f: for chunk in download_response.iter_content(chunk_size=8192): f.write(chunk) return destination_filename
def create_file(self, project_id: str, parent_id: int, name: str, entity_type: EntityTypes = EntityTypes.SEQUENCE_DOCUMENT, visible=False) -> dict: print('create_file for parent_id:' + str(parent_id) + ' name:' + str(name)) payload = { 'name': name, 'type': entity_type.value, 'visible': visible, 'shareableId': project_id, } if parent_id is not None: payload['parentId'] = int(str(parent_id)) response = self.session.post( '{}/api/v2/entities'.format(self.url), headers={'Content-type': 'Application/json'}, data=json.dumps(payload), ) print('create_file response:' + str(response.status_code)) Util.raise_detailed_error(response) return response.json()
def list(self): response = self.session.get(self.url + '?filter=', headers={ 'content-type': 'application/json' }) Util.raise_detailed_error(response) return response
def create(self, entity_id: int, type: AttachmentType, data: Union[dict, List]): print('Creating attachment: entity_id={},kind={}'.format(entity_id, type.value)) url = '{}/{}/attachments'.format(self.url, entity_id) json = {"data": data, "type": type.value} response = self.session.post(url, json=json) Util.raise_detailed_error(response) print('Creating attachment: response', response.status_code) return response.json()
def get(self, entity_id): response = self.session.get( '{}/api/v2/entities/{}'.format(self.url, entity_id), headers={'Content-type': 'Application/json'}, ) print('get response:' + str(response.status_code)) Util.raise_detailed_error(response) return response.json()
def list(self) -> List[dict]: url = '{}/api/v2/shareables'.format(self.url) response = self.session.get(url) print('ShareablesService:list - response:' + str(response.status_code)) Util.raise_detailed_error(response) return response.json()['data']
def start_import_job(self): """ Enable the cloud-function to trigger a job run via the kubernetes job processing engine. :return: """ response = self.session.patch( '{}/{}/import'.format(self.url, self.job_id), headers={'content-type': 'application/json'} ) Util.raise_detailed_error(response) return response
def mark_file_visible(self, entity_summary: UploadSummary): print('marking visible:', entity_summary) response = self.session.patch( '{}/api/v2/entities/{}'.format(self.url, entity_summary.id), headers={'Content-type': 'Application/json'}, data=json.dumps(entity_summary.to_json()), ) print('mark_file_visible response:' + str(response.status_code)) print('mark_file_visible text :' + str(response.text)) Util.raise_detailed_error(response) return response.json()
def upsert(self, entity_id: int, type: AttachmentType, data: Union[dict, List], version: int = 1, ignore_version=True): """ Create or update if exists. """ print('Upserting attachment: entity_id={},type={},version={},ignore_version={}'.format(entity_id, type.value, version, ignore_version)) url = '{}/{}/attachments'.format(self.url, entity_id) json = {"data": data, "version": version, "type": type.value, "ignoreVersion": ignore_version} response = self.session.put(url, json=json) Util.raise_detailed_error(response) print('Creating attachment: response', response.status_code)
def create(self, name: str) -> dict: url = '{}/api/v2/shareables'.format(self.url) response = self.session.post( url, data=json.dumps({'name': name}), ) print('ShareablesService:list - response:' + str(response.status_code)) Util.raise_detailed_error(response) return response.json()
def upload_data_to_signed_url(self, absolute_file_location: str, signed_url: str, signed_headers): # 1. Start the signed-upload. # NOTE: Url and headers cannot be modified or the upload will fail. create_upload_response = self.session.post(signed_url, headers=signed_headers) Util.raise_detailed_error(create_upload_response) response_headers = create_upload_response.headers location = response_headers['Location'] # 2. Upload bytes. with open(absolute_file_location, 'rb') as file: upload_response = self.session.put(location, data=file) Util.raise_detailed_error(upload_response) print('Upload response: ', upload_response.status_code) print('Upload response:', upload_response.text)
def list_entities(self, shareable_id: str): url = '{}/api/v2/shareables/{}/entities'.format(self.url, shareable_id) response = self.session.get(url) print('ShareablesService:list_entities - response:' + str(response.status_code)) Util.raise_detailed_error(response) file = StringIO(response.text) reader = csv.DictReader(file, dialect='excel-tab') rows = [] for row in reader: rows.append(row) return rows
def create_signed_upload(self, file_name: str, parent_id: int, project_id: str, organization_id: str) -> dict: data = { 'name': file_name, 'type': EntityTypes.SEQUENCE_DOCUMENT.value, 'targetFolderId': parent_id, 'shareableId': project_id, 'ownerId': organization_id, } # if details is not None: # # Details should be an # data['details'] = [] # for detail in details: # data['details'].append(detail.to_json()) response = self.session.post('{}/api/v2/signed-url'.format(self.base_url), json=data) Util.raise_detailed_error(response) return response.json()
def create(self, owner_id: str, shareable_id: str, job_type: JobType, name: str, input_entity_ids: List[int], params=None) -> str: """ :param owner_id: - organization id owning this job :param shareable_id: - project in which the docuemnts are :param job_type: :param name: - helpful user facing name :param input_entity_ids: - document ids :param params: - specific to this job_type :return: """ if params is None: params = {} response = self.session.post(self.url, headers={ 'content-type': 'application/json' }, data=json.dumps({ 'name': name, 'params': params, 'shareableId': shareable_id, 'ownerId': owner_id, 'inputEntities': input_entity_ids, 'type': job_type.value })) Util.raise_detailed_error(response) data = response.json() job_id = data['id'] self.job_id = job_id return job_id
def get_fields(self, entity_id: int, ignore_id=False) -> List[Column]: """ Returns the fields for a document or 404 if there are no fields (e.g. it's a folder). :return: """ response = self.session.get( '{}/api/v2/entities/{}/fields'.format(self.url, entity_id), ) Util.raise_detailed_error(response) columns = [] for field in response.json(): if ignore_id and field == 'id': continue else: # Not all columns have field so we need to check it's set. description = field[ 'description'] if 'description' in field else None columns.append( Column(field['name'], TableColumnType[field['type']], description)) return columns
def get(self, entity_id: str, type: AttachmentType, version: int = 1): url = '{}/{}/attachments/{}'.format(self.url, entity_id, type.value, json={'version': version}) response = self.session.get(url) Util.raise_detailed_error(response) return response.json()