You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Adding the package to a Visual Studio project

  1. From within Visual Studio, open the Nuget Package Manager
  2. Search for "dimension data"
  3. Click Install
  4. The package will install its dependencies, the Newtonsoft.Json for serializing JSON and Microsoft's ASP.NET WebApi client for sending XML requests over HTTP

 

Connecting to the API and returning data

The following example shows an example class using the .NET 4.5 async/await patterns. All of the API call methods within the ComputeApiClient are async.

using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace DD.Cloud.Examples.ApiClient
{
	using DD.CBU.Compute.Api.Client;
	using DD.CBU.Compute.Api.Client.Interfaces;
	using DD.CBU.Compute.Api.Contracts.Directory;

	public class TestClient
	{
		private IComputeApiClient _apiClient;
		private ICredentials _credentials = new NetworkCredential("myuser", "mypassword");
		private KnownApiRegion _targetRegion = KnownApiRegion.Australia_AU;
		private KnownApiVendor _targetVendor = KnownApiVendor.DimensionData;

		public TestClient()
		{
			_apiClient = ComputeApiClient.GetComputeApiClient(_targetVendor, _targetRegion, _credentials);
		}

		public async Task<string[]> GetServerNames()
		{
			// Login 
			IAccount myAccount = await _apiClient.Login();
			// Get all of the servers
			var servers = await _apiClient.GetDeployedServers();
			// Return all of the server names
			return servers.Select(server => server.name).ToArray();
		}
	}
}
  • No labels