|
| 1 | +namespace Examples |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.IO; |
| 5 | + using SmartyStreets; |
| 6 | + using SmartyStreets.InternationalPostalCodeApi; |
| 7 | + |
| 8 | + internal static class InternationalPostalCodeExample |
| 9 | + { |
| 10 | + public static void Run() |
| 11 | + { |
| 12 | + |
| 13 | + // You don't have to store your keys in environment variables, but we recommend it. |
| 14 | + var authId = Environment.GetEnvironmentVariable("SMARTY_AUTH_ID"); |
| 15 | + var authToken = Environment.GetEnvironmentVariable("SMARTY_AUTH_TOKEN"); |
| 16 | + |
| 17 | + using var client = new ClientBuilder(authId, authToken).BuildInternationalPostalCodeApiClient(); |
| 18 | + |
| 19 | + // Documentation for input fields can be found at: |
| 20 | + // https://smartystreets.com/docs/cloud/international-postal-code-api |
| 21 | + |
| 22 | + var lookup = new Lookup |
| 23 | + { |
| 24 | + InputId = "ID-8675309", // Optional ID from your system |
| 25 | + Locality = "Sao Paulo", |
| 26 | + AdministrativeArea = "SP", |
| 27 | + Country = "Brazil", |
| 28 | + PostalCode = "02516" |
| 29 | + }; |
| 30 | + |
| 31 | + //uncomment the line below to add a custom parameter |
| 32 | + //lookup.AddCustomParameter("postal_code", "02516"); |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + client.Send(lookup); |
| 37 | + } |
| 38 | + catch (SmartyException ex) |
| 39 | + { |
| 40 | + Console.WriteLine(ex.Message); |
| 41 | + Console.WriteLine(ex.StackTrace); |
| 42 | + return; |
| 43 | + } |
| 44 | + catch (IOException ex) |
| 45 | + { |
| 46 | + Console.WriteLine(ex.StackTrace); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + var results = lookup.Result; |
| 51 | + |
| 52 | + if (results == null || results.Count == 0) |
| 53 | + { |
| 54 | + Console.WriteLine("No results."); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + Console.WriteLine("Results:"); |
| 59 | + Console.WriteLine(); |
| 60 | + |
| 61 | + for (var c = 0; c < results.Count; c++) |
| 62 | + { |
| 63 | + var candidate = results[c]; |
| 64 | + Console.WriteLine("Candidate: " + c); |
| 65 | + Display(candidate.InputId); |
| 66 | + Display(candidate.CountryIso3); |
| 67 | + Display(candidate.Locality); |
| 68 | + Display(candidate.DependentLocality); |
| 69 | + Display(candidate.DoubleDependentLocality); |
| 70 | + Display(candidate.SubAdministrativeArea); |
| 71 | + Display(candidate.AdministrativeArea); |
| 72 | + Display(candidate.SuperAdministrativeArea); |
| 73 | + Display(candidate.PostalCode); |
| 74 | + Console.WriteLine(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void Display(string value) |
| 79 | + { |
| 80 | + if (!string.IsNullOrEmpty(value)) |
| 81 | + { |
| 82 | + Console.WriteLine(" " + value); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments