Skip to content

Commit 73ed197

Browse files
v2.0.11: improve delete return handling (fixes #12)
1 parent 3e66f8d commit 73ed197

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

farmbot/functions/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ def request(self, method, endpoint, database_id, payload=None):
186186
self.state.error = None
187187
description = "Successfully fetched request contents."
188188
self.state.print_status(description=description)
189-
return response.json()
189+
try:
190+
result = response.json()
191+
except (json.JSONDecodeError, requests.exceptions.RequestException):
192+
result = self.parse_text(response.text)
193+
return result
194+
190195
description = "There was an error processing the request..."
191196
self.state.print_status(description=description)
192197
return self.state.error

farmbot/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .functions.resources import Resources
1616
from .functions.tools import ToolControls
1717

18-
VERSION = "2.0.10"
18+
VERSION = "2.0.11"
1919

2020

2121
class Farmbot():

tests/tests_main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,23 @@ def test_api_delete(self, mock_request):
439439
)
440440
self.assertEqual(result, {'name': 'deleted'})
441441

442+
@patch('requests.request')
443+
def test_api_delete_empty_text_return_value(self, mock_request):
444+
'''test api_delete function with empty_text return value'''
445+
mock_response = Mock()
446+
mock_response.status_code = 200
447+
mock_response.text = ''
448+
mock_response.json.side_effect = json.JSONDecodeError(
449+
'error', doc='', pos=0)
450+
mock_request.return_value = mock_response
451+
result = self.fb.api_delete('points', 12345)
452+
mock_request.assert_called_once_with(
453+
method='DELETE',
454+
url='https://my.farm.bot/api/points/12345',
455+
**REQUEST_KWARGS,
456+
)
457+
self.assertEqual(result, '')
458+
442459
@patch('requests.request')
443460
def test_api_delete_with_payload(self, mock_request):
444461
'''test api_delete: with payload'''

0 commit comments

Comments
 (0)