def test_finds_filename_in_headers(self, mocked_open, response): with mock.patch('github3.utils.open', mocked_open, create=True): stream_response_to_file(response) mocked_open.assert_called_once_with('a_file_name', 'wb') mocked_open().write.assert_called_once_with(b'fake data') mocked_open().close.assert_called_once_with()
def test_opens_a_new_file(self, mocked_open, response): with mock.patch('github3.utils.open', mocked_open, create=True): stream_response_to_file(response, 'some_file') mocked_open.assert_called_once_with('some_file', 'wb') mocked_open().write.assert_called_once_with(b'fake data') mocked_open().close.assert_called_once_with()
def test_finds_filename_in_headers(self, mocked_open, response): with mock.patch("github3.utils.open", mocked_open, create=True): stream_response_to_file(response) mocked_open.assert_called_once_with("a_file_name", "wb") mocked_open().write.assert_called_once_with(b"fake data") mocked_open().close.assert_called_once_with()
def test_opens_a_new_file(self, mocked_open, response): with mock.patch("github3.utils.open", mocked_open, create=True): stream_response_to_file(response, "some_file") mocked_open.assert_called_once_with("some_file", "wb") mocked_open().write.assert_called_once_with(b"fake data") mocked_open().close.assert_called_once_with()
def download(self, path=''): """Download the data for this asset. :param path: (optional), path where the file should be saved to, default is the filename provided in the headers and will be written in the current directory. it can take a file-like object as well :type path: str, file :returns: bool -- True if successful, False otherwise """ headers = { 'Accept': 'application/octet-stream' } resp = self._get(self._api, allow_redirects=False, stream=True, headers=headers) if resp.status_code == 302: # Amazon S3 will reject the redirected request unless we omit # certain request headers headers.update({ 'Authorization': None, 'Content-Type': None, }) resp = self._get(resp.headers['location'], stream=True, headers=headers) if self._boolean(resp, 200, 404): stream_response_to_file(resp, path) return True return False
def download(url, path=''): """Download the data for this asset. :param path: (optional), path where the file should be saved to, default is the filename provided in the headers and will be written in the current directory. it can take a file-like object as well :type path: str, file :returns: name of the file, if successful otherwise ``None`` :rtype: str """ # headers = { # 'Accept': 'application/zip, application/octet-stream' # } headers = {} resp = None status_code = 302 while status_code == 302: resp = requests.get(url, allow_redirects=False, stream=True, headers=headers) status_code = resp.status_code if status_code == 302: url = resp.headers['location'] # Amazon S3 will reject the redirected request unless we omit # certain request headers if 's3' in resp.headers['location']: headers.update({'Content-Type': None}) if resp and resp_check(resp, 200, 404): return utils.stream_response_to_file(resp, path) return None
def test_uses_existing_file(self, response): fd = OpenFile() stream_response_to_file(response, fd) assert fd.written_to is True assert fd.data == b'fake data'