I followed the example on the Video introducing DotNet API. I am familiar with API,S and dot net for years.
I wrote the following code and it loops forever on the last line. Can you help ? Give me a hint ?
Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()
Dim myLocationApi = mySquareClient.LocationsApi (Until here, it works)
Dim myLocations = myLocationApi.ListLocations().Locations (when I reach this line. Loop forever…)
I see that my request is logged on Square Api Log Page and that it generates a correct response.
Is that the entire snippet of code that your using? Also since your using the SDKs have you looked at using our API Explorer to build snippets of code to call our APIs?
try
{
var result = await client.LocationsApi.ListLocationsAsync();
}
catch (ApiException e)
{
Console.WriteLine("Failed to make the request");
Console.WriteLine($"Response Code: {e.ResponseCode}");
Console.WriteLine($"Exception: {e.Message}");
}
Yes, I went to the API explorer. When I make my requests, they go through your server and the API log is filled up and returns a 200 status level. The Response contains the expected info in the log.
I tested different API’s and they all work according to the log. Was even able to launch the terminal API and make a payment that is confirmed in my Dashboard and in the API log.
But the thing is I stay “stucked” on the request line in my code, awaiting the response.
I put a Try Except and no error is raised up.
I installed all the recommended Microsoft nuget packages.
No error in scripts with debugger on Firefox or Chrome.
Reading the response is my only missing part to celebrate Easter )
No, you don’t need snippets. The snippet that I’m referring to is the the code that your running in that’s causing the duplicates. For example this is what I’m running:
using System;
using Square;
using Square.Models;
using Square.Exceptions;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace SquareListLocations
{
public class Program
{
private static ISquareClient client;
private static IConfigurationRoot config;
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.AddJsonFile($"/Users/SDK/dot-net/SDK-update/SquareListLocations/appsettings.json", true, true);
config = builder.Build();
var accessToken = config["AppSettings:AccessToken"];
client = new SquareClient.Builder()
.Environment(Square.Environment.Sandbox)
.AccessToken(accessToken)
.Build();
RetrieveLocationsAsync().Wait();
}
static async Task RetrieveLocationsAsync()
{
try
{
ListLocationsResponse response;
response = await client.LocationsApi.ListLocationsAsync();
foreach (Location location in response.Locations)
{
Console.WriteLine("location:\n 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}:", 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
}
}
}
}
Protected Async Sub BtnTestClick()
Dim myAccessToken As String = “MY_TOKEN”
Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()
Try
lblInfo.Text = "Calling..."
Dim myLocation = Await mySquareClient.LocationsApi.RetrieveLocationAsync("JVW3XXCXD6R3M")
lblInfo.Text = myLocation.Location.BusinessName
Catch ex As ApiException
lblInfo.Text = ex.ResponseCode.ToString + " - " + ex.Message
End Try
I did some tests and with version 16 of Square SDK for .Net, I can use LocationApi.RetrieveLocation in real time (not async).
With latest release, I cannot. It freezes.
Same code. Very simple and basic code.
So maybe your team should look at it.
Private Sub RadButton4_Click(sender As Object, e As EventArgs) Handles RadButton4.Click
Dim myAccessToken As String = "MY_TOKEN"
Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()
Dim myLocationApi = mySquareClient.LocationsApi
Try
Label1.Text = "Calling..."
Dim myLocation = myLocationApi.RetrieveLocation("JVW3XXCXD6R3M")
Label1.Text = myLocation.Location.BusinessName
Catch ex As ApiException
Label1.Text = ex.ResponseCode.ToString + " - " + ex.Message
End Try
End Sub
The team was able to get it to work with the latest version of the SDK with a few changes.
Module Program
Sub Main(args As String())
Dim myAccessToken As String = "YOUR_ACCESS_TOKEN"
Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Sandbox).AccessToken(myAccessToken).Build()
Console.WriteLine("Square SDK version: " + mySquareClient.SdkVersion)
Dim myLocationApi = mySquareClient.LocationsApi
Try
Console.WriteLine("Calling...")
Dim myLocation = myLocationApi.RetrieveLocation("LOCATION_ID")
Console.WriteLine(myLocation.Location.BusinessName)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
I have 25.2. I do not know what in my environment could make this behavior.
You test under Visual studio ? Version Version 17.0.4 64 bit I have.
Only thing I can say is that in the same environment, version 16 of the API works without Async and that version 25.2 was not working also at my friend’s place.
So to me, the answer is in the API version.
I sent much time around that. And finally use only Async, which s not my first choice for everything, but at least, I can go forward in my project.