public void AddressApiResponse_VerifyCsvFactory()
        {
            var addressResponseString = "\"3\",\"216 Norfolk Street, Cambridge, MA, 02139\",\"Match\",\"Exact\",\"216 NORFOLK ST, CAMBRIDGE, MA, 02139\",\"-71.098366,42.369587\",\"86869427\",\"R\"";
            var addressResponse       = AddressApiResponse.ParseAddressApiResponseFromCsv(addressResponseString);

            Assert.IsTrue(addressResponse.Address.City == "Cambridge");
        }
        public async Task <AddressApiData> FetchAddressUsingApiAsync(string DeliverAddressId)
        {
            var    app           = Application.Current as App;
            string DeliveryBoyId = app.UserId;
            string AccessKey     = app.UserKey;
            string PhoneNumber   = app.UserPhoneNumber;
            Dictionary <string, dynamic> payload = new Dictionary <string, dynamic>();

            payload.Add("phone_number", PhoneNumber);
            payload.Add("access_key", AccessKey);
            payload.Add("deliveraddress_id", DeliverAddressId);
            AddressApiResponse ApiResponseObject = await this.Post <AddressApiResponse>(this.getAuthUrl("getDeliverAddress"), payload, null);

            Console.WriteLine("Response Status is : " + ApiResponseObject.Status);
            if (ApiResponseObject.Status.ToLower() != "success")
            {
                System.Diagnostics.Debug.WriteLine("[BuildRequestNDisplay] Received non-success " + ApiResponseObject.Status);
                System.Diagnostics.Debug.WriteLine("[BuildRequestNDisplay] Received Response: " + ApiResponseObject.Data);
                return(null);
            }
            ResponseData = ApiResponseObject.Data;
            return(ResponseData);
        }
Esempio n. 3
0
        /// <summary>
        /// Geocode a list of addresses using Census geocoding API
        /// </summary>
        /// <param name="addresses">List of addresses where length is less than or equal to 1000</param>
        /// <param name="returnType">Whether to hit Locations or Geographies API (only location is supported at present)</param>
        /// <returns></returns>
        public List <AddressApiResponse> BulkGeocode(List <Address> addresses, string returnType = DefaultReturnType)
        {
            if (addresses.Count() > 1000)
            {
                throw new Exception("BulkApiAgent cannot geocode more than 1000 addresses per request");
            }

            // Move elsewhere
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12
                                                   | SecurityProtocolType.Ssl3;

            string addressesCsv = "";

            addresses.ForEach(address => addressesCsv = String.Concat(addressesCsv, address.ToCsv()));

            // Convert addresses from list of strings to Byte array to bundle as "file" as required by API
            byte[] addressesAsBytes = Encoding.ASCII.GetBytes(addressesCsv);

            using (var client = new HttpClient())
            {
                try
                {
                    if (Uri.TryCreate(String.Format(EndPointRoot, returnType), UriKind.Absolute, out Uri endpointUrl))
                    {
                        client.BaseAddress = endpointUrl;

                        // Set up form arguments for POST request
                        var content = new MultipartFormDataContent();

                        // Fake a file to pass to endpoint
                        var fileContent = new ByteArrayContent(addressesAsBytes);
                        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                        {
                            Name     = "addressFile",
                            FileName = "addresses.csv"
                        };
                        fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        content.Add(fileContent);

                        // Not a FormUrlEncodedContent class due to an ostensible bug in census API that
                        // rejects key/value formatting and requires 'benchmark' in a 'name' field
                        var benchMarkContent = new StringContent(Benchmark);
                        benchMarkContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                        {
                            Name = "benchmark"
                        };
                        content.Add(benchMarkContent);

                        var    result        = client.PostAsync("", content).Result;
                        string resultContent = result.Content.ReadAsStringAsync().Result;
                        var    resultSplit   = resultContent.Split('\n');

                        // Results return with an extra newline after the last entry, drop the last item
                        resultSplit = resultSplit.Take(resultSplit.Count() - 1).ToArray();

                        var resultAddresses = new List <AddressApiResponse>();
                        resultSplit.ToList().ForEach(addressString => resultAddresses.Add(AddressApiResponse.ParseAddressApiResponseFromCsv(addressString)));

                        return(resultAddresses);
                    }
                    else
                    {
                        throw new Exception("Error forming Census endpoint URL");
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }