Skip to content

Commit 4611935

Browse files
committed
fix(API): Use GET only when no body
1 parent aba0dc2 commit 4611935

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

test/unit/tinify_source_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ def test_from_url_should_raise_error_when_server_doesnt_return_a_success(
124124
def test_result_should_return_result(self):
125125
assert isinstance(Source.from_buffer(b"png file").result(), Result)
126126

127+
def test_result_should_use_get_when_commands_is_empty(self, mock_requests):
128+
source = Source(b"png file")
129+
source.url = "https://api.tinify.com/some/location"
130+
mock_requests.get(
131+
"https://api.tinify.com/some/location", content=b"compressed file"
132+
)
133+
source.result()
134+
assert mock_requests.call_count == 1
135+
assert mock_requests.last_request.method == "GET"
136+
137+
def test_result_should_use_post_when_commands_is_not_empty(self, mock_requests):
138+
source = Source(b"png file").resize(width=400)
139+
source.url = "https://api.tinify.com/some/location"
140+
mock_requests.post(
141+
"https://api.tinify.com/some/location", content=b"small file"
142+
)
143+
source.result()
144+
assert mock_requests.call_count == 1
145+
assert mock_requests.last_request.method == "POST"
146+
127147
def test_preserve_should_return_source(self, mock_requests):
128148
assert isinstance(
129149
Source.from_buffer(b"png file").preserve("copyright", "location"), Source

tinify/source.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ def store(self, **options): # type: (Any) -> ResultMeta
6565
return ResultMeta(response.headers)
6666

6767
def result(self): # type: () -> Result
68-
response = tinify.get_client().request('GET', self.url, self.commands)
68+
if not self.commands:
69+
response = tinify.get_client().request('GET', self.url, self.commands)
70+
else:
71+
response = tinify.get_client().request('POST', self.url, self.commands)
6972
return Result(response.headers, response.content)
7073

7174
def to_file(self, path): # type: (Union[str, IO]) -> None

0 commit comments

Comments
 (0)