How to get a list of orders?
This example of using the Jasmin Web API illustrates how you can use the API to retrieve the list of orders available in the system. This code was developed in C# for a console application and the source code of this example is available in the repository of Jasmin or Rose examples. AccountKey and SubscriptionKey First, these two data are required by the Web API and must be defined in the code. To do this, we use two constants that will be used in the request URLs. Access token To access the Web API you also need the authorization token associated with the user and the subscription.In the Constants.cs file, there are the constants defined as clientId and clientSecret (in this case we are using the Client Credentias authentication flow) and a request to obtain the token by calling the GetAccessTokenAsync method available in the AuthenticationProvider.cs class is exemplified. HttpClient Para fazer chamadas a uma Web API REST em C# pode utilizar-se a classe: Such as, for example: Configuration of the access token Web API requests always require the previous access token. This must be included in the request headers. For the order query endpoint, order data will be returned in the body of the result message in JSON format. To do this, we created a very simple C# class that only includes the order attributes that are required for the example. At this point, the call to the Web API can be executed from an HTTP Client. Note that the request construction must always contain the base URL of the product followed by the word "api", the AccountKey and SubscriptionKey, ending with the module and the entity on which the operation will be performed.Concepts
internal class RoseConstants
{
internal const string AccountKey = "HXDID1P";
internal const string SubscriptionKey = "HXDID7M-HXDIDGJ";
}
// Get the authorization access token
string accessToken = await GetAccessTokenAsync();
Console.WriteLine(accessToken != null ? "Authorization token found." : "Authorization token not found.");
System.Net.Http.HttpClient
using (HttpClient client = new HttpClient())
{
(…)
}
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
Order data
internal class OrderResource
{
#region Internal Properties
public Guid Id
{
get;
set;
}
public string DocumentType
{
get;
set;
}
public string Serie
{
get;
set;
}
public int SeriesNumber
{
get;
set;
}
public string Company
{
get;
set;
}
#endregion
}
Running the request
// URI of the endpoint
string endpoint = string.Format(CultureInfo.CurrentCulture, "{0}/{1}/{2}/sales/orders", ApiBaseAddress, AccountKey, SubscriptionKey);
// Send the request (POST)
Uri endpointUri = new Uri(endpoint);
using (HttpResponseMessage response = await client.PostAsync(endpointUri, content))
{
(…)
}
login para deixar a sua opinião.