Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 61 additions & 76 deletions quickstart/Program.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
}
42 changes: 21 additions & 21 deletions quickstart/quickstart.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>