From 0daeda90ba9d990defcff5f8f2e17920871982df Mon Sep 17 00:00:00 2001 From: Mike Konopelski Date: Wed, 19 Mar 2025 10:08:40 -0400 Subject: [PATCH] upgrade dotnet quickstart to use new sdk --- quickstart/Program.cs | 137 ++++++++++++++++------------------- quickstart/quickstart.csproj | 42 +++++------ 2 files changed, 82 insertions(+), 97 deletions(-) diff --git a/quickstart/Program.cs b/quickstart/Program.cs index 669576d..03e7518 100644 --- a/quickstart/Program.cs +++ b/quickstart/Program.cs @@ -1,76 +1,61 @@ -using Square; -using Square.Models; -using Square.Exceptions; -using Square.Authentication; - -using Microsoft.Extensions.Configuration; - -namespace ExploreLocationsAPI -{ - public class Program - { - private static ISquareClient client; - private static IConfigurationRoot config; - - static void Main(string[] args) - { - var builder = new ConfigurationBuilder() - .AddJsonFile($"appsettings.json", true, true); - - config = builder.Build(); - var accessToken = config["AppSettings:AccessToken"]; - - client = new SquareClient.Builder() - .BearerAuthCredentials( - new BearerAuthModel.Builder( - accessToken - ) - .Build()) - .Environment(Square.Environment.Sandbox) - .Build(); - - RetrieveLocationsAsync().Wait(); - } - - static async Task RetrieveLocationsAsync() - { - try - { - ListLocationsResponse response = await client.LocationsApi.ListLocationsAsync(); - foreach (Location location in response.Locations) - { - Console.WriteLine("location:\n country = {0} name = {1}", - location.Country, location.Name); - } - } - catch (ApiException e) - { - var errors = e.Errors; - var statusCode = e.ResponseCode; - var httpContext = e.HttpContext; - Console.WriteLine("ApiException occurred:"); - Console.WriteLine("Headers:"); - foreach (var item in httpContext.Request.Headers) - { - //Display all the headers except Authorization - if (item.Key != "Authorization") - { - Console.WriteLine("\t{0}: \t{1}", item.Key, item.Value); - } - } - Console.WriteLine("Status Code: \t{0}", statusCode); - foreach (Error error in errors) - { - Console.WriteLine("Error Category:{0} Code:{1} Detail:{2}", error.Category, error.Code, error.Detail); - } - - // Your error handling code - } - catch (Exception e) - { - Console.WriteLine("Exception occurred"); - // Your error handling code - } - } - } -} +using Square; +using Square.Locations; + +using Microsoft.Extensions.Configuration; + +namespace ExploreLocationsAPI +{ + public class Program + { + private static SquareClient client = null!; + private static IConfigurationRoot config = null!; + + static async Task Main(string[] args) + { + var builder = new ConfigurationBuilder() + .AddJsonFile($"appsettings.json", true, true); + + config = builder.Build(); + var accessToken = config["AppSettings:AccessToken"]; + + client = new SquareClient( + accessToken, + new ClientOptions + { + BaseUrl = SquareEnvironment.Sandbox + } + ); + + await RetrieveLocationsAsync(); + } + + static async Task RetrieveLocationsAsync() + { + try + { + var response = await client.Locations.ListAsync(); + if (response.Locations != null) + { + foreach (var location in response.Locations) + { + Console.WriteLine($"location: country = {location.Country} name = {location.Name}"); + } + } + else + { + Console.WriteLine("No locations found."); + } + } + catch (SquareApiException e) + { + Console.WriteLine("SquareApiException occurred:"); + Console.WriteLine("Status Code: {0}", e.StatusCode); + Console.WriteLine("Error: {0}", e.Message); + } + catch (Exception e) + { + Console.WriteLine($"Exception occurred: {e.Message}"); + } + } + } +} diff --git a/quickstart/quickstart.csproj b/quickstart/quickstart.csproj index a092fbe..bcb09ce 100644 --- a/quickstart/quickstart.csproj +++ b/quickstart/quickstart.csproj @@ -1,21 +1,21 @@ - - - - Exe - net7.0 - enable - enable - - - - - - - - - - Always - - - - + + + + Exe + net8.0 + enable + enable + + + + + + + + + + Always + + + + \ No newline at end of file