Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

EG-BRS/Xena.Contracts

Repository files navigation

How to use the Xena API

You can use this function to GET data from the API:

public async Task<T> GetData<T>(string apiUrl, string route = "Api")
{
    apiUrl = _apiSetting.Url + $"/{route}/" + apiUrl;

    using (var handler = new HttpClientHandler { UseCookies = false })
    {
        using (var client = new HttpClient(handler) { BaseAddress = new Uri(apiUrl) })
        {
            var message = new HttpRequestMessage();

            client.SetBearerFromUser(_httpContextAccessor.HttpContext.User);

            var result = await client.SendAsync(message);
            var resultBody = await result.Content.ReadAsStringAsync();

            return JsonConvert.DeserializeObject<T>(resultBody);
        }
    }
}

Use this the POST data to the API:

public async Task<T> PostData<T>(object data, string apiUrl, string route = "Api")
{
    apiUrl = _apiSetting.Url + $"/{route}/" + apiUrl;
    var json = JsonConvert.SerializeObject(data);

    using (var handler = new HttpClientHandler { UseCookies = false })
    {
        using (var client = new HttpClient(handler) { BaseAddress = new Uri(apiUrl) })
        {
            var message = new HttpRequestMessage(HttpMethod.Post, "");
            message.Content = new StringContent(json, Encoding.UTF8, "application/json");
            message.Headers.Add("Accept", "application/json");

            client.SetBearerFromUser(_httpContextAccessor.HttpContext.User);

            var result = await client.SendAsync(message);

            var statusCodeHandler = new StatusCodeHandler();
            statusCodeHandler.HandleStatusCode(result);

            var resultBody = await result.Content.ReadAsStringAsync();

            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };
            return JsonConvert.DeserializeObject<T>(resultBody, settings);
        }
    }
}

The functions are used like this:

var result = await _xena.GetData<PagedResultSet<PartnerResourceContextDto>>($"Fiscal/{FiscalId}/Resource");

Remember to use PagedResultSet when you request a list of stuff from the API.