示例#1
0
 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()
示例#2
0
    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()
示例#3
0
    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
示例#4
0
    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()
示例#5
0
 def list(self):
     response = self.session.get(self.url + '?filter=',
                                 headers={
                                     'content-type': 'application/json'
                                 })
     Util.raise_detailed_error(response)
     return response
示例#6
0
 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()
示例#7
0
 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()
示例#8
0
    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']
示例#9
0
 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
示例#10
0
 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()
示例#11
0
 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)
示例#12
0
    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()
示例#13
0
    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)
示例#14
0
    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
示例#15
0
    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()
示例#16
0
    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
示例#17
0
    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
示例#18
0
 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()