From 2e396aa96f4c4be10a8b730ae2611c83d3c87219 Mon Sep 17 00:00:00 2001 From: Juan Pimentel Date: Thu, 16 Mar 2023 06:32:49 -0500 Subject: [PATCH] Fixed bug. Removed double json dump --- VERSION.py | 2 +- route4me/activity_feed.py | 5 +---- route4me/address.py | 8 +++----- route4me/address_book.py | 4 +--- route4me/api.py | 28 +++++++++++++++------------- route4me/avoidance_zones.py | 5 ++--- route4me/members.py | 6 ++---- route4me/optimization.py | 5 +---- route4me/orders.py | 7 +++---- route4me/route.py | 13 ++++--------- route4me/territory.py | 8 +++----- route4me/version.py | 2 +- 12 files changed, 37 insertions(+), 56 deletions(-) diff --git a/VERSION.py b/VERSION.py index b53c717..41ab7b6 100644 --- a/VERSION.py +++ b/VERSION.py @@ -6,7 +6,7 @@ # VERSION.py - MAINTAINER's. Don't edit, if you don't know what are you doing # ============================================================================== -VERSION = (0, 1, 4, 1) +VERSION = (0, 1, 4, 4) RELEASE_SUFFIX = '' VERSION_STRING = '.'.join([str(x) for x in VERSION]) diff --git a/route4me/activity_feed.py b/route4me/activity_feed.py index 8f518ff..b6a4e00 100644 --- a/route4me/activity_feed.py +++ b/route4me/activity_feed.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -import json - from .api_endpoints import ACTIVITY_FEED from .base import Base from .exceptions import ParamValueException @@ -106,9 +104,8 @@ def log_specific_message(self, **kwargs): if self.check_required_params(self.json_data, ['api_key', 'activity_message', 'route_id']): - data = json.dumps(self.json_data, ensure_ascii=False) self.response = self.api._request_post(ACTIVITY_FEED, - self.params, data=data) + self.params, json=self.json_data) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') diff --git a/route4me/address.py b/route4me/address.py index 9bac9a6..6b4e691 100644 --- a/route4me/address.py +++ b/route4me/address.py @@ -166,11 +166,9 @@ def update_address(self, data, route_id, route_destination_id): 'route_destination_id': route_destination_id } params.update({'api_key': self.api.key}) - data = json.dumps(data) - response = self.api._make_request(ADDRESS_HOST, - params, - data, - self.api._request_put) + response = self.api._request_put(ADDRESS_HOST, + params, + json=data) return response.json() def delete_address_from_route(self, route_id, route_destination_id): diff --git a/route4me/address_book.py b/route4me/address_book.py index dda3479..181448d 100644 --- a/route4me/address_book.py +++ b/route4me/address_book.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -import json - from .api_endpoints import ADDRESSBOOK from .base import Base from .exceptions import ParamValueException @@ -88,7 +86,7 @@ def delete_addressbook_contact(self, **kwargs): if self.check_required_params(kwargs, ['address_ids', ]): self.response = self.api._request_delete(ADDRESSBOOK, self.params, - data=json.dumps(kwargs)) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') diff --git a/route4me/api.py b/route4me/api.py index 2ca065f..f560c65 100644 --- a/route4me/api.py +++ b/route4me/api.py @@ -139,7 +139,7 @@ def _request_put(self, url, request_params, json=None, data=None): headers=self.headers, verify=self.verify_ssl) - def _request_delete(self, url, request_params, data=None): + def _request_delete(self, url, request_params, data=None, json=None): """ DELETE request :param url: @@ -149,6 +149,7 @@ def _request_delete(self, url, request_params, data=None): """ return requests.request('DELETE', url, params=request_params, data=data, + json=json, headers=self.headers, verify=self.verify_ssl) @@ -178,10 +179,9 @@ def re_optimization(self, optimization_id, data={}): self.optimization.optimization_problem_id(optimization_id) self.optimization.reoptimize(1) data = {'parameters': data} - self.response = self._make_request(API_HOST, - self.optimization.get_params(), - json.dumps(data), - self._request_put) + self.response = self._request_put(API_HOST, + self.optimization.get_params(), + json=data) try: return self.response.json() except ValueError: @@ -217,10 +217,11 @@ def export_result_to_json(self, file_name): if self.response: try: f = open(file_name, 'w') - f.write(json.dumps(self.response.content, - ensure_ascii=False, - sort_keys=True, - indent=4)) + json.dump(self.response.content, + f, + ensure_ascii=False, + sort_keys=True, + indent=4) f.close() except Exception: raise @@ -234,10 +235,11 @@ def export_request_to_json(self, file_name): if self.optimization.data: try: f = open(file_name, 'w') - f.write(json.dumps(self.optimization.data, - ensure_ascii=False, - sort_keys=True, - indent=4)) + json.dump(self.optimization.data, + f, + ensure_ascii=False, + sort_keys=True, + indent=4) f.close() except Exception: raise diff --git a/route4me/avoidance_zones.py b/route4me/avoidance_zones.py index 0b04073..471d0cc 100644 --- a/route4me/avoidance_zones.py +++ b/route4me/avoidance_zones.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # codebeat:disable[SIMILARITY, ABC] -import json from .api_endpoints import AVOIDANCE from .base import Base @@ -60,7 +59,7 @@ def add_avoidance_zone(self, **kwargs): 'territory']): self.response = self.api._request_post(AVOIDANCE, self.params, - data=json.dumps(kwargs)) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') @@ -91,7 +90,7 @@ def update_avoidance_zone(self, territory_id, **kwargs): 'territory']): self.response = self.api._request_put(AVOIDANCE, self.params, - data=json.dumps(kwargs)) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') diff --git a/route4me/members.py b/route4me/members.py index 05aa13f..bc9cef3 100644 --- a/route4me/members.py +++ b/route4me/members.py @@ -43,10 +43,9 @@ def app_purchase_user_license(self, **kwargs): 'token', 'payload', 'format', ]): - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_post(USER_LICENSE_HOST, self.params, - data=data) + json=kwargs) try: return json.loads(response.content) except ValueError: @@ -63,10 +62,9 @@ def verify_device_license(self, **kwargs): if self.check_required_params(kwargs, ['device_id', 'device_type', 'format', ]): - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_post(VERIFY_DEVICE_LICENSE, self.params, - data=data) + json=kwargs) try: return json.loads(response.content) except ValueError: diff --git a/route4me/optimization.py b/route4me/optimization.py index 96b0757..318093b 100644 --- a/route4me/optimization.py +++ b/route4me/optimization.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -import json - from .api_endpoints import ADDRESS_HOST, API_HOST from .base import Base from .exceptions import ParamValueException @@ -83,10 +81,9 @@ def delete_optimization(self, **kwargs): """ self.json_data = kwargs if self.check_required_params(kwargs, ['optimization_problem_ids', ]): - data = json.dumps(self.json_data, ensure_ascii=False) self.response = self.api._request_delete(API_HOST, self.params, - data=data) + json=kwargs) response = self.response.json() return response else: diff --git a/route4me/orders.py b/route4me/orders.py index f79d8b2..c6b700d 100644 --- a/route4me/orders.py +++ b/route4me/orders.py @@ -29,10 +29,9 @@ def create_order(self, **kwargs): :return: API response content """ if self.check_required_params(kwargs, self.REQUIRED_FIELDS): - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_post(ORDERS_HOST, self.params, - data=data) + json=kwargs) try: return response.json() except ValueError: @@ -66,7 +65,7 @@ def update_order(self, **kwargs): if self.check_required_params(kwargs, self.REQUIRED_FIELDS): response = self.api._request_put(ORDERS_HOST, self.params, - data=json.dumps(kwargs)) + json=kwargs) try: return json.loads(response.content) except ValueError: @@ -83,7 +82,7 @@ def delete_order(self, **kwargs): if self.check_required_params(kwargs, ['order_ids', ]): response = self.api._request_delete(ORDERS_HOST, self.params, - data=json.dumps(kwargs)) + jsom=kwargs) try: return json.loads(response.content) except ValueError: diff --git a/route4me/route.py b/route4me/route.py index c4664b6..cbfe2df 100644 --- a/route4me/route.py +++ b/route4me/route.py @@ -2,7 +2,6 @@ # codebeat:disable[TOO_MANY_FUNCTIONS] -import json from .api_endpoints import ROUTE_HOST, EXPORTER from .api_endpoints import ADDRESS_HOST, GET_ACTIVITIES_HOST, DUPLICATE_ROUTE, SHARE_ROUTE_HOST @@ -177,10 +176,9 @@ def insert_address_into_route_optimal_position(self, **kwargs): 'optimal_position']): params = {'api_key': self.params['api_key'], 'route_id': kwargs.pop('route_id')} - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_put(ROUTE_HOST, params, - data=data) + json=kwargs) return response.json() else: raise ParamValueException('params', 'Params are not complete') @@ -254,9 +252,8 @@ def resequence_route(self, **kwargs): 'route_id': kwargs.pop('route_id'), 'route_destination_id': kwargs.pop('route_destination_id'), } - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_put(ROUTE_HOST, - params, data=data) + params, json=kwargs) return response.json() else: @@ -289,10 +286,9 @@ def merge_routes(self, **kwargs): AttributeError if there is an error deleting a route """ if self.check_required_params(kwargs, ['route_ids']): - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_post(MERGE_ROUTES_HOST, self.params, - data=data) + json=kwargs) return response.json() else: @@ -330,9 +326,8 @@ def update_route_destination_custom_data(self, **kwargs): 'route_id': kwargs.pop('route_id'), 'route_destination_id': kwargs.pop('route_destination_id'), } - data = json.dumps(kwargs, ensure_ascii=False) response = self.api._request_put(ADDRESS_HOST, - params, data=data) + params, json=kwargs) return response.json() else: diff --git a/route4me/territory.py b/route4me/territory.py index e3cc433..194b32a 100644 --- a/route4me/territory.py +++ b/route4me/territory.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- # codebeat:disable[SIMILARITY, BLOCK_NESTING] -import json - from .api_endpoints import TERRITORY_HOST from .base import Base from .exceptions import ParamValueException @@ -60,7 +58,7 @@ def add_territory(self, **kwargs): 'territory']): self.response = self.api._request_post(TERRITORY_HOST, self.params, - data=json.dumps(kwargs)) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') @@ -74,7 +72,7 @@ def delete_territory(self, **kwargs): kwargs.update({'api_key': self.params['api_key'], }) if self.check_required_params(kwargs, ['territory_id']): self.response = self.api._request_delete(TERRITORY_HOST, - kwargs) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') @@ -91,7 +89,7 @@ def update_territory(self, territory_id, **kwargs): 'territory']): self.response = self.api._request_put(TERRITORY_HOST, self.params, - data=json.dumps(kwargs)) + json=kwargs) return self.response.json() else: raise ParamValueException('params', 'Params are not complete') diff --git a/route4me/version.py b/route4me/version.py index cfb805d..7a3cf7d 100644 --- a/route4me/version.py +++ b/route4me/version.py @@ -6,7 +6,7 @@ # VERSION.py - MAINTAINER's. Don't edit, if you don't know what are you doing # ============================================================================== -VERSION = (0, 1, 4, 3) +VERSION = (0, 1, 4, 4) RELEASE_SUFFIX = '' VERSION_STRING = '.'.join([str(x) for x in VERSION])