Skip to content

Commit 49cc5e2

Browse files
committed
Handle file upload
* this allow ticket attachements and other misc upload
1 parent 0748912 commit 49cc5e2

6 files changed

+101
-20
lines changed

tests/http_utils_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ def test_with_bools(self):
6666
'true': True,
6767
'false': False
6868
}, result)
69+
70+
def test_filtering_files(self):
71+
result = _http_utils.form_encode_without_files(dict(true=True, files=dict(attach='some_binary_data')))
72+
self.assertDictEqual({
73+
'true': True,
74+
}, result)

tests/ubersmith_request_get_test.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# limitations under the License.
1414

1515
import unittest
16-
1716
from hamcrest import assert_that, equal_to
1817
from mock import patch, MagicMock
19-
import ubersmith_client
2018

19+
import ubersmith_client
2120
from tests.ubersmith_json.response_data_structure import a_response_data
2221

2322

@@ -66,6 +65,26 @@ def test_api_get_method_returns_with_arguments(self, request_mock):
6665

6766
expected_call()
6867

68+
@patch('ubersmith_client.ubersmith_request_get.requests')
69+
def test_api_get_support_ticket_submit_allow_file_upload(self, request_mock):
70+
expected_files = {'attach[0]': ('filename.pdf', b'filecontent')}
71+
expected_call = self.expect_a_ubersmith_call_with_files(requests_mock=request_mock,
72+
method='support.ticket_submit',
73+
subject='that I used to know',
74+
body='some body',
75+
returning=a_response_data(data='42'),
76+
files=expected_files)
77+
78+
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password, use_http_get=True)
79+
80+
response = ubersmith_api.support.ticket_submit(subject='that I used to know',
81+
body='some body',
82+
files=expected_files)
83+
84+
assert_that(response, equal_to('42'))
85+
86+
expected_call()
87+
6988
def expect_a_ubersmith_call(self, requests_mock, returning=None, **kwargs):
7089
response = MagicMock(status_code=200, headers={'content-type': 'application/json'})
7190
requests_mock.get = MagicMock(return_value=response)
@@ -77,3 +96,16 @@ def assert_called_with():
7796
response.json.assert_called_with()
7897

7998
return assert_called_with
99+
100+
def expect_a_ubersmith_call_with_files(self, requests_mock, returning=None, files=None, **kwargs):
101+
response = MagicMock(status_code=200, headers={'content-type': 'application/json'})
102+
requests_mock.get = MagicMock(return_value=response)
103+
response.json = MagicMock(return_value=returning)
104+
105+
def assert_called_with():
106+
requests_mock.get.assert_called_with(auth=self.auth, params=kwargs, timeout=self.timeout, url=self.url,
107+
files=files,
108+
headers={'user-agent': 'python-ubersmithclient'})
109+
response.json.assert_called_with()
110+
111+
return assert_called_with

tests/ubersmith_request_post_test.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# limitations under the License.
1414

1515
import unittest
16-
1716
from hamcrest import assert_that, equal_to, calling, raises
1817
from mock import patch, MagicMock
19-
import ubersmith_client
2018

19+
import ubersmith_client
2120
from tests.ubersmith_json.response_data_structure import a_response_data
2221

2322

@@ -77,13 +76,46 @@ def test_api_raises_exception_with_if_data_status_is_false(self, requests_mock):
7776
self.expect_a_ubersmith_call_post(requests_mock, method='client.miss', returning=data)
7877
assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException))
7978

80-
def expect_a_ubersmith_call_post(self, requests_mock, returning=None, status_code=200, **kwargs):
81-
response = MagicMock(status_code=status_code, headers={'content-type': 'application/json'})
79+
@patch('ubersmith_client.ubersmith_request_post.requests')
80+
def test_api_post_support_ticket_submit_allow_file_upload(self, request_mock):
81+
expected_files = {'attach[0]': ('filename.pdf', b'filecontent')}
82+
expected_call = self.expect_a_ubersmith_call_post_with_files(requests_mock=request_mock,
83+
method='support.ticket_submit',
84+
subject='that I used to know',
85+
body='some body',
86+
returning=a_response_data(data='42'),
87+
files=expected_files)
88+
89+
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
90+
91+
response = ubersmith_api.support.ticket_submit(subject='that I used to know',
92+
body='some body',
93+
files=expected_files)
94+
95+
assert_that(response, equal_to('42'))
96+
97+
expected_call()
98+
99+
def expect_a_ubersmith_call_post(self, requests_mock, returning=None, **kwargs):
100+
response = MagicMock(status_code=200, headers={'content-type': 'application/json'})
101+
requests_mock.post = MagicMock(return_value=response)
102+
response.json = MagicMock(return_value=returning)
103+
104+
def assert_called_with():
105+
requests_mock.post.assert_called_with(auth=self.auth, timeout=self.timeout, url=self.url, data=kwargs,
106+
headers={'user-agent': 'python-ubersmithclient'})
107+
response.json.assert_called_with()
108+
109+
return assert_called_with
110+
111+
def expect_a_ubersmith_call_post_with_files(self, requests_mock, returning=None, files=None, **kwargs):
112+
response = MagicMock(status_code=200, headers={'content-type': 'application/json'})
82113
requests_mock.post = MagicMock(return_value=response)
83114
response.json = MagicMock(return_value=returning)
84115

85116
def assert_called_with():
86117
requests_mock.post.assert_called_with(auth=self.auth, timeout=self.timeout, url=self.url, data=kwargs,
118+
files=files,
87119
headers={'user-agent': 'python-ubersmithclient'})
88120
response.json.assert_called_with()
89121

ubersmith_client/_http_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def form_encode(data):
2222
return exploded_data
2323

2424

25+
def form_encode_without_files(data):
26+
return form_encode({k: v for k, v in data.items() if k is not 'files'})
27+
28+
2529
def _explode_enumerable(k, v):
2630
exploded_items = []
2731
if isinstance(v, list) or isinstance(v, tuple):

ubersmith_client/ubersmith_request_get.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
class UbersmithRequestGet(UbersmithRequest):
2222
def __call__(self, **kwargs):
2323
self._build_request_params(kwargs)
24-
params = _http_utils.form_encode(kwargs)
24+
params = _http_utils.form_encode_without_files(kwargs)
25+
requests_get_args = dict(method=requests.get,
26+
url=self.url,
27+
auth=(self.user, self.password),
28+
timeout=self.timeout,
29+
headers={'user-agent': 'python-ubersmithclient'},
30+
params=params)
31+
if 'files' in kwargs:
32+
requests_get_args['files'] = kwargs['files']
2533

26-
response = self._process_request(method=requests.get,
27-
url=self.url,
28-
auth=(self.user, self.password),
29-
timeout=self.timeout,
30-
headers={'user-agent': 'python-ubersmithclient'},
31-
params=params)
34+
response = self._process_request(**requests_get_args)
3235

3336
return UbersmithRequest.process_ubersmith_response(response)

ubersmith_client/ubersmith_request_post.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
class UbersmithRequestPost(UbersmithRequest):
2222
def __call__(self, **kwargs):
2323
self._build_request_params(kwargs)
24-
params = _http_utils.form_encode(kwargs)
24+
params = _http_utils.form_encode_without_files(kwargs)
2525

26-
response = self._process_request(method=requests.post,
27-
url=self.url,
28-
auth=(self.user, self.password),
29-
timeout=self.timeout,
30-
headers={'user-agent': 'python-ubersmithclient'},
31-
data=params)
26+
requests_post_args = dict(method=requests.post,
27+
url=self.url,
28+
auth=(self.user, self.password),
29+
timeout=self.timeout,
30+
headers={'user-agent': 'python-ubersmithclient'},
31+
data=params)
32+
if 'files' in kwargs:
33+
requests_post_args['files'] = kwargs['files']
34+
35+
response = self._process_request(**requests_post_args)
3236

3337
return UbersmithRequest.process_ubersmith_response(response)

0 commit comments

Comments
 (0)