Cloud Jasmin Common Use Cases
Common Use Cases
Common Use Cases
Back | List of Articles

How to get a list of orders?

Last changed in 26/08/2022

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.

Concepts

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.

internal class RoseConstants
{
    internal const string AccountKey = "HXDID1P";
    internal const string SubscriptionKey = "HXDID7M-HXDIDGJ";
}

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.

// Get the authorization access token
string accessToken = await GetAccessTokenAsync();
Console.WriteLine(accessToken != null ? "Authorization token found." : "Authorization token not found.");

HttpClient

Para fazer chamadas a uma Web API REST em C# pode utilizar-se a classe:

System.Net.Http.HttpClient

Such as, for example:

using (HttpClient client = new HttpClient())
{
    (…)
}

Configuration of the access token

Web API requests always require the previous access token. This must be included in the request headers.

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Order data

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.

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

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.

// 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))
{
    (…)
}
Bookmark or share this article
Esta página foi útil?
Obrigado pelo seu voto.

login para deixar a sua opinião.

Obrigado pelo seu feedback. Iremos analisá-lo para continuarmos a melhorar!
Artigos Relacionados
Como obter uma lista de encomendas? Como criar uma encomenda de vendas? Como obter o pdf de um documento? Como obter uma lista de encomendas? Como criar uma encomenda de vendas?