Exemplo n.º 1
0
    def upload_file(self, file_data: str):
        """
        Upload and scan a package and returns the file_id

        :param file: Package file content to be uploaded and scanned
        """
        response = self.drf_api['organizations/{}/upload_app'.format(
            self.organization_id)]().get()
        url = response['url']
        requests.put(url, data=file_data)
        response2 = self.drf_api['organizations/{}/upload_app'.format(
            self.organization_id)]().post(
                data=dict(file_key=response['file_key'],
                          file_key_signed=response['file_key_signed'],
                          url=response['url']))
        file = None
        timeout = time.time() + 10
        submission_id = response2['submission_id']
        while (file is None):
            submission = self.drf_api.submissions(submission_id).get()
            if submission.get('detail') == 'Not found.':
                raise UploadError(
                    'Something went wrong, try uploading the file again.')
            submission_obj = mapper_drf_api(Submission, submission)
            file = submission_obj.file
            if submission_obj.reason:
                raise UploadError(submission_obj.reason)
            if time.time() > timeout:
                raise RuntimeError(
                    'Request timed out, try uploading the file again.')
        return file
Exemplo n.º 2
0
 def get_file(self, file_id: int) -> File:
     """
     Fetch file by file ID
     :param file_id: File ID
     """
     file = self.drf_api['v2/files/{}'.format(file_id)]().get()
     return mapper_drf_api(File, file)
Exemplo n.º 3
0
    def paginated_drf_data(self, response, mapper_class):
        initial_data = [
            mapper_drf_api(mapper_class, value)
            for value in response['results']
        ]

        if not response.get('next'):
            return initial_data
        nxt = response['next']
        while nxt is not None:
            resp = self.drf_api.direct_get(nxt)
            nxt = resp['next']
            initial_data += [
                mapper_drf_api(mapper_class, value)
                for value in resp['results']
            ]
        return initial_data
Exemplo n.º 4
0
    def get_project(self, project_id: int) -> Project:
        """
        Fetch project by project ID

        :param project_id: Project ID
        """
        project = self.drf_api['v2/projects/{}'.format(project_id)]().get()
        return mapper_drf_api(Project, project)
Exemplo n.º 5
0
 def get_whoami(self) -> Whoami:
     """
     Show session info
     """
     whoami = self.drf_api.me().get()
     return mapper_drf_api(Whoami, whoami)