Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with to open file attachments #342

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions quickbooks/client.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved the open() call closer to where it's used

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def make_request(self, request_type, url, request_body=None, content_type='appli
}

if file_path:
attachment = open(file_path, 'rb')
url = url.replace('attachable', 'upload')
boundary = '-------------PythonMultipartPost'
headers.update({
Expand All @@ -173,7 +172,8 @@ def make_request(self, request_type, url, request_body=None, content_type='appli
'Connection': 'close'
})

binary_data = str(base64.b64encode(attachment.read()).decode('ascii'))
with open(file_path, 'rb') as attachment:
binary_data = str(base64.b64encode(attachment.read()).decode('ascii'))

content_type = json.loads(request_body)['ContentType']

Expand Down
20 changes: 18 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from tests.integration.test_base import QuickbooksUnitTestCase

try:
from mock import patch
from mock import patch, mock_open
except ImportError:
from unittest.mock import patch
from unittest.mock import patch, mock_open

from quickbooks.exceptions import QuickbooksException, SevereException, AuthorizationException
from quickbooks import client
Expand Down Expand Up @@ -217,6 +217,22 @@ def test_download_pdf_not_authorized(self, process_request):

self.assertRaises(AuthorizationException, receipt.download_pdf, self.qb_client)

@patch('quickbooks.client.QuickBooks.process_request')
def test_make_request_file_closed(self, process_request):
file_path = '/path/to/file.txt'
process_request.return_value = MockResponse()
with patch('builtins.open', mock_open(read_data=b'file content')) as mock_file:
qb_client = client.QuickBooks(auth_client=self.auth_client)
qb_client.make_request('POST',
'https://sandbox-quickbooks.api.intuit.com/v3/company/COMPANY_ID/attachable',
request_body='{"ContentType": "text/plain"}',
file_path=file_path)

mock_file.assert_called_once_with(file_path, 'rb')
mock_file.return_value.__enter__.return_value.read.assert_called_once()
mock_file.return_value.__exit__.assert_called_once()
process_request.assert_called_once()


class MockResponse(object):
@property
Expand Down
Loading