diff --git a/examples/single_depot_round_trip_orders_based.py b/examples/single_depot_round_trip_orders_based.py new file mode 100644 index 0000000..253101b --- /dev/null +++ b/examples/single_depot_round_trip_orders_based.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- +# codebeat:disable[SIMILARITY, LOC, ABC] + +import argparse + +from route4me import Route4Me + +from route4me.constants import ( + ALGORITHM_TYPE, + OPTIMIZE, + DEVICE_TYPE, + TRAVEL_MODE, + DISTANCE_UNIT, +) + + +def main(api_key): + route4me = Route4Me(api_key) + + optimization = route4me.optimization + address = route4me.address + optimization.algorithm_type(ALGORITHM_TYPE.CVRP_TW_SD) + optimization.share_route(0) + optimization.store_route(0) + optimization.route_time(0) + optimization.rt(1) + optimization.route_max_duration(86400) + optimization.vehicle_max_distance_mi(10000) + optimization.route_name('Single Depot Round Trip Orders Based') + optimization.optimize(OPTIMIZE.TIME) + optimization.distance_unit(DISTANCE_UNIT.MI) + optimization.device_type(DEVICE_TYPE.WEB) + optimization.travel_mode(TRAVEL_MODE.DRIVING) + + order_manager = route4me.order + + # Creating some Orders + + orders = [ + { + "address_1": "754 5th Ave New York, NY 10019, USA", + "address_stop_type": "DELIVERY", + "day_scheduled_for_YYMMDD": "2022-12-24", + "EXT_FIELD_cost": 5, + "EXT_FIELD_pieces": 1, + "EXT_FIELD_custom_data": + { + "attached_barcode": "ORD0002ABC12301", + } + }, + { + "address_1": "717 5th Ave New York, NY 10022", + "address_stop_type": "DELIVERY", + "day_scheduled_for_YYMMDD": "2022-12-24", + "EXT_FIELD_cost": 1, + "EXT_FIELD_pieces": 1, + "EXT_FIELD_custom_data": + { + "attached_barcode": "ORD0002ABC12302", + } + }, + { + "address_1": "888 Madison Ave New York, NY 10014", + "address_stop_type": "DELIVERY", + "day_scheduled_for_YYMMDD": "2022-12-24", + "EXT_FIELD_cost": 2, + "EXT_FIELD_pieces": 1, + "EXT_FIELD_custom_data": + { + "attached_barcode": "ORD0002ABC12303", + } + }, + { + "address_1": "1011 Madison Ave New York, NY 10075", + "address_stop_type": "DELIVERY", + "day_scheduled_for_YYMMDD": "2022-12-24", + "EXT_FIELD_cost": 6, + "EXT_FIELD_pieces": 1, + "EXT_FIELD_custom_data": + { + "attached_barcode": "ORD0002ABC12304", + } + }, + { + "address_1": "440 Columbus Ave New York, NY 10024", + "address_stop_type": "DELIVERY", + "day_scheduled_for_YYMMDD": "2022-12-24", + "EXT_FIELD_cost": 3, + "EXT_FIELD_pieces": 1, + "EXT_FIELD_custom_data": + { + "attached_barcode": "ORD0002ABC12305", + } + }, + + ] + + for o in orders: + new_order = order_manager.create_order(**o) + address.add_address(order_id=new_order.get('order_id')) + + # Adding Depot + + address.add_address( + address='57 W 57th St New York, NY 10019', + lat=40.7558695, + lng=-73.9862019, + alias='Verizon Wireless', + time=0, + is_depot=True + ) + + response = route4me.run_optimization() + print('Optimization Link: {}'.format(response['links']['view'])) + for i, route in enumerate(response['routes']): + print('\t{0}\tRoute Link: {1}'.format(i + 1, route['links']['route'])) + for address in route['addresses']: + print('\t\t\tAddress: {0}'.format(address['address'])) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Single Driver Round Trip') + parser.add_argument('--api_key', dest='api_key', help='Route4Me API KEY', + type=str, required=True) + args = parser.parse_args() + main(args.api_key) + +# codebeat:enable[SIMILARITY, LOC, ABC] diff --git a/route4me/address.py b/route4me/address.py index fa78d1e..9bac9a6 100644 --- a/route4me/address.py +++ b/route4me/address.py @@ -63,7 +63,9 @@ def add_address(self, **kwargs): :param kwargs: :return: """ - if self.check_required_params(kwargs, self.REQUIRED_FIELDS): + if self.check_required_params(kwargs, self.REQUIRED_FIELDS) or \ + self.check_required_params(kwargs, ["order_id"]) or \ + self.check_required_params(kwargs, ["contact_id"]): self.addresses.append(kwargs) self.api.optimization.data['addresses'] = self.addresses else: diff --git a/route4me/api.py b/route4me/api.py index 67e73e0..57b060c 100644 --- a/route4me/api.py +++ b/route4me/api.py @@ -4,10 +4,6 @@ import requests import json -try: - from urllib import urlencode -except ImportError: - from urllib.parse import urlencode from .activity_feed import ActivityFeed from .address import Address from .address_book import AddressBook @@ -76,21 +72,12 @@ def _make_request(self, url, params, data, request_method): :raise: APIException """ params['api_key'] = self.key - request_params = self._transform_params(params) - response = request_method(url, request_params, data) + response = request_method(url, params, data) if not 200 <= response.status_code < 400: raise APIException(response.status_code, response.text, response.url) return response - def _transform_params(self, params): - """ - Convert params dict to url params - :param params: - :return: - """ - return urlencode(params) - def get(self, request_method): """ Execute optimization diff --git a/route4me/orders.py b/route4me/orders.py index 73d9a03..f79d8b2 100644 --- a/route4me/orders.py +++ b/route4me/orders.py @@ -12,7 +12,7 @@ class Order(Base): Orders are transactional events. """ - REQUIRED_FIELDS = ('address_1', 'cached_lat', 'cached_lng',) + REQUIRED_FIELDS = ('address_1', ) def __init__(self, api): """