I have used previous versions of the Square SDK but now I am trying to transition to the Square API V2. I am able to make the following call successfully but my issue is that I need to wait for the call to finish. Being and asynch call my code continues and I have no way of knowing when the query has returned the results. How do I know when the query has returned a result so that I can use it?
{
client = new SquareClient.Builder()
.Environment(Square.Environment.Production)
.AccessToken(“xxxx-xxxxxxxxxxxxxxxx-x”)
.Build();
var locationIds = new List<string>();
locationIds.Add("xxxxxxxxxxxx");
var states = new List<string>();
states.Add("COMPLETED");
var stateFilter = new SearchOrdersStateFilter.Builder(states: states)
.Build();
DateTime fromTime = DateTime.Now.AddHours(-36);
DateTime toTime = DateTime.Now;
var closedAt = new TimeRange.Builder()
.StartAt(fromTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", DateTimeFormatInfo.InvariantInfo))
.EndAt(toTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", DateTimeFormatInfo.InvariantInfo))
.Build();
var dateTimeFilter = new SearchOrdersDateTimeFilter.Builder()
.ClosedAt(closedAt)
.Build();
var filter = new SearchOrdersFilter.Builder()
.StateFilter(stateFilter)
.DateTimeFilter(dateTimeFilter)
.Build();
var sort = new SearchOrdersSort.Builder(sortField: "CLOSED_AT")
.SortOrder("DESC")
.Build();
var query = new SearchOrdersQuery.Builder()
.Filter(filter)
.Sort(sort)
.Build();
var body = new SearchOrdersRequest.Builder()
.LocationIds(locationIds)
.Query(query)
.Limit(6)
.ReturnEntries(false)
.Build();
SearchOrdersResponse result = null; ;
try
{
result = await client.OrdersApi.SearchOrdersAsync(body: body);
return result; //reakpointhere
}
catch (ApiException e)
{
Console.WriteLine("Failed to make the request");
Console.WriteLine($"Response Code: {e.ResponseCode}");
Console.WriteLine($"Exception: {e.Message}");
return result;
}
catch(Exception e2)
{
return result;
}
}
I put a breakpoint " return result; //reakpointhere" when I call this task it returns immediately and the breakpoint gets hit a few seconds late. I need to call SearchOrdersAsync synchronously.