Beispiel #1
0
        public async Task <WebResponse> PostAsync(DeliveryEstimateRequest request, string url, string apiKey)
        {
            var headers = new Dictionary <string, string>
            {
                { "Authorization", $"Bearer {apiKey}" }
            };

            var requestJson = JsonConvert.SerializeObject(request);

            using (_client)
            {
                var webRequest = BuildWebRequest("POST", url, requestJson, headers);
                return(webRequest.GetResponseAsync().Result);
            }
        }
Beispiel #2
0
        public async void ValidateDelivery()
        {
            _request = GetDefaultRequest();

            Console.Write("\nEnter Api Key: ");
            _apiKey = Console.ReadLine();

            Console.Write("\nEnter External Business Name: ");
            _request.external_business_name = Console.ReadLine();

            Console.WriteLine("\nEnter Pickup Address:");
            Console.WriteLine("---------------------");
            _request.pickup_address = CreateAddress();

            Console.WriteLine("\nEnter Dropoff Address:");
            Console.WriteLine("---------------------");
            _request.dropoff_address = CreateAddress();

            var estimate     = new DeliveryEstimate();
            var executor     = new WebRequestExecutor();
            var errorMessage = string.Empty;

            try
            {
                var response = await executor.PostAsync(_request, _url, _apiKey);

                estimate = ProcessResponse(response);
            }
            catch (Exception exception)
            {
                var webException        = (WebException)exception.InnerException;
                var webRequestException = new WebRequestException(webException);
                errorMessage = webRequestException.Message;
                estimate     = null;
            }

            if (estimate != null && estimate.field_errors == null)
            {
                var timeEnroute = (estimate.delivery_time - estimate.pickup_time).TotalMinutes;
                Console.WriteLine(String.Format("\nDoorDash will deliver from {0} to {1}.\nApproximate time enroute is {2} minutes.", _request.pickup_address.street, _request.dropoff_address.street, timeEnroute));
            }
            else if (estimate != null)
            {
                Console.WriteLine(String.Format("\nDoorDash will NOT deliver from {0} to {1}.", _request.pickup_address.street, _request.dropoff_address.street));

                foreach (var error in estimate.field_errors)
                {
                    Console.WriteLine(String.Format("Problem: {0}: {1}", error["field"], error["error"]));
                }
            }
            else
            {
                Console.WriteLine("\nERROR -- Problem with credentials or formatting.\n");

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    Console.Write(errorMessage);
                }
            }
            Console.ReadLine();
        }