Beispiel #1
0
        /// <summary>
        /// Determine PCode for a location
        /// </summary>
        /// <remarks>Converts the given request data to a PCode.
        /// Only one of the fields should be used for the conversion: ZipAddress, FipsCode or NPANXX.</remarks>
        /// <param name="request">Object containing ZipAddress, FipsCode or NPANXX to be converted.</param>
        /// <returns>PCode for specified ZipAddress, FipsCode or NPANXX.</returns>
        public async Task <uint> GetPCode(RequestPCodeDetail request)
        {
            HttpResponseMessage response = await httpClient.PostAsJsonAsync($"api/v1/Location/PCode", request);

            if (!response.IsSuccessStatusCode)
            {
                string error = response.Headers.Contains(ErrorHeader) ?
                               response.Headers.GetValues(ErrorHeader).FirstOrDefault() :
                               null;

                throw new AfcRestException(response.StatusCode, error);
            }

            string content = await response.Content.ReadAsStringAsync();

            return(Convert.ToUInt32(content));
        }
Beispiel #2
0
        /// <summary>
        /// Example showing how to obtain the PCode for a location.
        /// </summary>
        private static void GetPCode()
        {
            PrintSeparator();
            Console.WriteLine($"API: POST {BaseAddress}/api/v1/Location/PCode");
            Console.WriteLine();

            using (var client = new AfcRestClient(BaseAddress, UserName, Password, ClientId, ClientProfileId))
            {
                try
                {
                    // Create a RequestPCodeDetail object for the request body
                    // NOTE: only one of the following properties must be populated: FipsCode, NpaNxx, or ZipAddress
                    var request = new RequestPCodeDetail
                    {
                        FipsCode   = null,
                        NpaNxx     = null,
                        ZipAddress = new ZipAddress
                        {
                            CountryIso = "USA",
                            State      = "WA",
                            County     = "King",
                            Locality   = "Seattle",
                            ZipCode    = "98101"
                        }
                    };

                    Console.WriteLine($"REQUEST BODY:");
                    Console.WriteLine(request.ToJson());
                    Console.WriteLine();

                    // Invoke REST API to obtain the PCode for the location
                    uint pcode = client.GetPCode(request).Result;

                    Console.WriteLine($"RESPONSE:");
                    Console.WriteLine(pcode);
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ERROR: {ex.Message}");
                }
            }
        }