예제 #1
0
    def test_request_with_no_params(self, reqmock):
        reqmock.get('https://api/bar', status_code=200)
        request_raw('get', 'bar')

        req = reqmock.last_request
        assert req.url == 'https://api/bar'
        assert req.text is None
예제 #2
0
    def test_request_with_no_params(self, reqmock):
        reqmock.get("https://api/bar", status_code=200)
        request_raw("get", "bar")

        req = reqmock.last_request
        assert req.url == "https://api/bar"
        assert req.text is None
예제 #3
0
    def test_request_with_params(self, reqmock):
        reqmock.get("https://api/bar", status_code=200)
        request_raw("get", "bar", params={"baz": "qux"}, body="bazqux", headers={"foo": "bar"})

        req = reqmock.last_request
        assert req.query == "baz=qux"
        assert req.text == "bazqux"
        assert req.headers["foo"] == "bar"
예제 #4
0
    def test_authentication(self, reqmock):
        reqmock.get('https://api/bar', status_code=200)

        analyzere.username = '******'
        analyzere.password = '******'
        request_raw('get', 'bar')

        assert (reqmock.last_request.headers['Authorization'] == 'Basic %s' %
                base64.b64encode(b'user:pa55').decode('ascii'))
예제 #5
0
    def test_request_with_params(self, reqmock):
        reqmock.get('https://api/bar', status_code=200)
        request_raw('get', 'bar', params={'baz': 'qux'}, body='bazqux',
                    headers={'foo': 'bar'})

        req = reqmock.last_request
        assert req.query == 'baz=qux'
        assert req.text == 'bazqux'
        assert req.headers['foo'] == 'bar'
예제 #6
0
    def test_authentication(self, reqmock):
        reqmock.get("https://api/bar", status_code=200)

        analyzere.username = "******"
        analyzere.password = "******"
        request_raw("get", "bar")

        assert reqmock.last_request.headers["Authorization"] == "Basic %s" % base64.b64encode(b"user:pa55").decode(
            "ascii"
        )
예제 #7
0
 def test_request_retying(self, reqmock):
     reqmock.get(
         "https://api/bar",
         [{"status_code": 503, "headers": {"Retry-After": "1.0"}}, {"status_code": 200, "text": "foo"}],
     )
     with mock.patch("time.sleep") as sleep:
         resp = request_raw("get", "bar")
     assert resp.text == "foo"
     sleep.assert_called_once_with(1.0)
예제 #8
0
 def test_request_retying(self, reqmock):
     reqmock.get('https://api/bar', [
         {'status_code': 503, 'headers': {'Retry-After': '1.0'}},
         {'status_code': 200, 'text': 'foo'},
     ])
     with mock.patch('time.sleep') as sleep:
         resp = request_raw('get', 'bar')
     assert resp.text == 'foo'
     sleep.assert_called_once_with(1.0)
예제 #9
0
    def upload_data(self, file_or_str):
        """
        Accepts a file-like object or string and uploads it. Files are
        automatically uploaded in 4mb chunks. Implements the tus protocol.
        """
        file_obj = StringIO(file_or_str) if isinstance(file_or_str, six.string_types) else file_or_str
        chunk_size = 4 * 1024 * 1024
        length = utils.file_length(file_obj)

        # Initiate upload session
        request_raw("post", self._data_path, headers={"Entity-Length": str(length)})

        # Upload chunks
        for chunk, offset in utils.read_in_chunks(file_obj, chunk_size):
            headers = {"Offset": str(offset), "Content-Type": "application/offset+octet-stream"}
            request_raw("patch", self._data_path, headers=headers, body=chunk)

        # Commit the session
        request_raw("post", self._commit_path)

        # Block until data has finished processing
        while True:
            resp = self.upload_status
            if resp.status == "Processing Successful" or resp.status == "Processing Failed":
                return resp
            else:
                time.sleep(analyzere.upload_poll_interval)
예제 #10
0
 def download_ylt(self, auto_retry=True, **params):
     path = '%s/ylt' % self._get_path(self.id)
     return request_raw('get', path, params=params,
                        auto_retry=auto_retry).content
예제 #11
0
 def test_errors_handled(self, reqmock):
     reqmock.get("https://api/bar", status_code=400)
     with pytest.raises(InvalidRequestError):
         request_raw("get", "bar")
예제 #12
0
 def download_data(self):
     return request_raw("get", self._data_path).content
예제 #13
0
 def delete_data(self):
     request_raw("delete", self._data_path)
예제 #14
0
 def download_yelt(self, auto_retry=True, **params):
     path = '{}/yelt'.format(self._get_path(self.id))
     return request_raw('get', path, params=params,
                        auto_retry=auto_retry).content
예제 #15
0
 def delete_data(self):
     request_raw('delete', self._data_path)
예제 #16
0
 def download_data(self):
     return request_raw('get', self._data_path).content
예제 #17
0
    def upload_data(self,
                    file_or_str,
                    chunk_size=analyzere.upload_chunk_size,
                    poll_interval=analyzere.upload_poll_interval,
                    upload_callback=lambda x: None,
                    commit_callback=lambda x: None):
        """
        Accepts a file-like object or string and uploads it. Files are
        automatically uploaded in chunks. The default chunk size is 16MiB and
        can be overwritten by specifying the number of bytes in the
        ``chunk_size`` variable.
        Accepts an optional poll_interval for temporarily overriding the
        default value `analyzere.upload_poll_interval`.
        Implements the tus protocol.
        Takes optional callbacks that return the percentage complete for the
        given "phase" of upload: upload/commit.
        Callback values are returned as 10.0 for 10%
        """
        if not callable(upload_callback):
            raise Exception('provided upload_callback is not callable')
        if not callable(commit_callback):
            raise Exception('provided commit_callback is not callable')

        file_obj = StringIO(file_or_str) if isinstance(
            file_or_str, six.string_types) else file_or_str

        # Upload file with known entity size if file object supports random
        # access.
        length = None
        if hasattr(file_obj, 'seek'):
            length = utils.file_length(file_obj)

            # Initiate upload session
            request_raw('post',
                        self._data_path,
                        headers={'Entity-Length': str(length)})
        else:
            request_raw('post', self._data_path)

        # Upload chunks
        for chunk, offset in utils.read_in_chunks(file_obj, chunk_size):
            headers = {
                'Offset': str(offset),
                'Content-Type': 'application/offset+octet-stream'
            }
            request_raw('patch', self._data_path, headers=headers, body=chunk)
            # if there is a known size, and an upload callback, call it
            if length:
                upload_callback(offset * 100.0 / length)

        upload_callback(100.0)
        # Commit the session
        request_raw('post', self._commit_path)

        # Block until data has finished processing
        while True:
            resp = self.upload_status
            if (resp.status == 'Processing Successful'
                    or resp.status == 'Processing Failed'):
                commit_callback(100.0)
                return resp
            else:
                commit_callback(float(resp.commit_progress))
                time.sleep(poll_interval)
예제 #18
0
 def test_errors_handled(self, reqmock):
     reqmock.get('https://api/bar', status_code=400)
     with pytest.raises(InvalidRequestError):
         request_raw('get', 'bar')
예제 #19
0
 def download_yelt(self, auto_retry=True, **params):
     path = "%s/yelt" % self._get_path(self.id)
     return request_raw("get", path, params=params, auto_retry=auto_retry).content