C#
In this article we discuss some examples for API interaction. It should be noted that these are indeed examples and not production-grade code.
Add a service reference
Visual Studio can generate classes to interact with WCF services. Follow the following steps in Visual Studio to generate the code.
- Go to: Project -> Add Service Reference…
- Enter the following information:
Address:https://api.cloudbilling.nl/V1_3_2.svc
Namespace:CB
- Click OK
Example: upload a customer, get all customers and upload a purchase
The code sample below shows a complete C# console application that retrieves all customers. It expects the service reference as shown above.
using System.ServiceModel;
using System.ServiceModel.Channels;
using DemoClient.CB;
var client = new APIServiceEndpointClient();
// Add the API token to outbound message headers.
var token = "...";
client.Endpoint.EndpointBehaviors.Add(new CloudBillingTokenHeaderBehaviour(token));
// Upload a customer to CloudBilling.
var uploadCustomerResult = await client.UploadCustomersAsync([
new()
{
CustomerCode = "100000",
CustomerName = "Foo Bar ltd",
ImportTagName = "Your Customer Name Here",
TimezoneId = "W. Europe Standard Time",
InvoicePeriodSize = 1,
InvoicePeriodUOM = BillingEnumsUOM.Month,
InvoicePeriodAlignToUOM = true,
}
]);
if (uploadCustomerResult.ResultCode != ResultCodeEnum.Success)
throw new Exception(uploadCustomerResult.ResultMessage);
Console.WriteLine($"Uploaded customer to CloudBilling");
// Get all customers.
var getCustomerResult = await client.CustomersGetAllAsync();
if (getCustomerResult.ResultCode != ResultCodeEnum.Success)
throw new Exception(getCustomerResult.ResultMessage);
Console.WriteLine($"There are {getCustomerResult.Result.Length} customers in CloudBilling");
// Upload a purchase to CloudBilling.
var uploadPurchaseResult = await client.UploadPurchasesAsync([
new()
{
PurchaseReference = "100000-0001",
CustomerCode = "100000",
ProductLabel = "Your Product Name Here",
Quantity = 1,
PurchaseDate = DateTime.Now.ToString("O"),
}
]);
if (uploadPurchaseResult.ResultCode != ResultCodeEnum.Success)
throw new Exception(uploadPurchaseResult.ResultMessage);
Console.WriteLine($"Uploaded purchase to CloudBilling");
public class CloudBillingTokenHeaderBehaviour(string token) : IEndpointBehavior, IClientMessageInspector
{
#region IEndpointBehavior
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(this);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
throw new NotImplementedException();
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion IEndpointBehavior
#region IClientMessageInspector
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object? BeforeSendRequest(ref Message request, IClientChannel channel)
{
request.Headers.Add(MessageHeader.CreateHeader("ApiToken",
"http://api.cloudbilling.nl/2018/11/auth/token", token));
return null;
}
#endregion IClientMessageInspector
}