Table of Contents

Overview

This project gives support to the public API V2 endpoints.

The SDK contains a list of classes (clients). Each client corresponds to a controller in the API. For example, the OfficesClient class contains all the methods necessary to call the endpoints in the OfficesController.

How to use the SDK

The NfieldHttpClientFactory class has two methods to create an HttpClient ready to be used with minimal configuration

  • CreateNativeBearerClient: supports the native authentication. It expects a Bearer token
  • CreateEntraIdClient: supports the Entra ID authentication. It expects a bearer token and the domain name

Pass this HttpClient to the SDK client you want to use. See the following examples:

Example: Native Bearer authentication

// Sign in to get a bearer token
const string userName = "Enter user name";
const string password = "Enter password";
const string publicApiBaseUrl = "https://public-api-host-name"; //Token Uri does not contain Version
const string nfieldIdentityBaseUrl = "https://nfield-identity-host-name";
var tokenRequestModel = new Nfield.SDK.TokenRequestModel
{
	DomainName = Domain.Name,
	UserName = userName,
	Password = password
};
var bearerHttpClient = await NfieldHttpClientFactory.CreateNativeBearerClient(publicApiBaseUrl, nfieldIdentityBaseUrl, tokenRequestModel);

var officesClient = new Nfield.SDK.DomainOfficesClient(publicApiBaseUrl, bearerHttpClient);
var offices = await officesClient.GetAllAsync();
var office = await officesClient.GetAsync(offices.First().Id);
office = await officesClient.PostAsync(new OfficesRequestModel
{
	Name = "Test SDK",
	Description = "Test SDK",
});

Example: Entra ID authentication

const string bearerToken = "Enter your token here";
const string publicApiBaseUrl = "https://public-api-host-name"; //Token Uri does not contain Version

var bearerHttpClient = NfieldHttpClientFactory.CreateEntraIdClient(bearerToken, "domainName");

var officesClient = new Nfield.SDK.DomainOfficesClient(publicApiBaseUrl, bearerHttpClient);
var offices = await officesClient.GetAllAsync();
var office = await officesClient.GetAsync(offices.First().Id);
office = await officesClient.PostAsync(new OfficesRequestModel
{
	Name = "Test SDK",
	Description = "Test SDK",
});

Testing the API with the SDK

It is recommended to test the API with the SDK. The SDK is generated from the Swagger Documentation, so by testing the API through the SDK we are also testing that the documentation is correct.

The SDK takes into account the expected exceptions, models and response codes. So the request and responses must be exactly the expected ones. This allows us to create different tests to tests all the possible scenarios.

POST and PATCH methods eith response should return a Location header with a URL pointing to the created/updated resource. The NfieldHttpClientFactory has a property ResponseLocationHeader that returns the value in the Location header from the response.

Example: To obtain the URI to get and validate the created resource

const string bearerToken = "Enter your token here";
const string publicApiBaseUrl = "https://public-api-host-name"; //Token Uri does not contain Version

var bearerHttpClient = NfieldHttpClientFactory.CreateEntraIdClient(bearerToken, "domainName");
var _target = new Nfield.SDK.DomainCapiInterviewersClient(publicApiBaseUrl, bearerHttpClient);

//Act
await _target.PatchOfficeAssignmentAsync(createdInterviewer.InterviewerId, createdOffice.Id, CancellationToken.None);

//assert Location heather
NfieldHttpClientFactory.ResponseLocationHeader.Should().Be($"{TestsParams.Instance.PublicApiBaseUri}/v2/capiInterviewers/{createdInterviewer.InterviewerId}/offices/{createdOffice.Id}");