Base16.LoopiaApi 1.0.0

Simple wrapper for comunicating with LoopiaApi via .NET.

Official Api is documented at https://www.loopia.se/api/

Install-Package Base16.LoopiaApi -Version 1.0.0
dotnet add package Base16.LoopiaApi --version 1.0.0
<PackageReference Include="Base16.LoopiaApi" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Base16.LoopiaApi --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Base16.LoopiaApi

Introduction

Base16.LoopiaApi is a simple .NET Standard 2.0 XML-RPC wrapper for Loopias API.
The wrapper makes it easy to use the public customer-facing API described in the official documentation.
This library can for example very simply be used to update a DNS record to point to a specific IP.

Prerequisites

In order to use the functionality exposed by this library (and the Loopia API in general) you'll need to have at least a DNS account registered with Loopia. You'll also need to create an API-user ("API-användare") under your account and ensure that the user has the appropriate access rights.

Limitations & Known Issues

  • Loopias official limitations apply
  • Most partner API's have not been implemented sine they cannot be fully verified without being a partner
  • Encoding for IDN-domains seems to be wrong on invoice_item_obj - This is only an issue when viewing these domains on invoice-lines

Simple usage example

Note this example only serves to illustrate the usage of the API.

using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Base16.LoopiaApi;
using Base16.LoopiaApi.Models;

class Program
{
	private static readonly HttpClient HttpClient = new HttpClient();

	public async Task Main()
	{
		var externalIP = await GetExternalIPAsync();

		var result = await UpdateZoneRecordAsync(
			apiUsername: "--ENTER-YOUR-API-USERNAME-HERE--",
			apiPassword: "--ENTER-YOUR-API-PASSWORD-HERE--",
			domain: "--ENTER-YOUR-DOMAIN-NAME-HERE--(ex: google.se)--",
			subdomain: "@",
			externalIP: externalIP
		);

		Console.WriteLine($"{nameof(UpdateZoneRecordAsync)} returned {result}");
	}

	private async Task<ResponseStatus> UpdateZoneRecordAsync(
		String apiUsername,
		String apiPassword,
		String domain,
		String subdomain,
		String externalIP
	)
	{
		// Initialize LoopiaApiClient given a username and password.
		// 'customerNumber' is only valid for retailers and should otherwise be omitted.
		var loopiaApi = new LoopiaApiClient(
			username: apiUsername,
			password: apiPassword,
			customerNumber: null
		);

		// Get all zone-records for the given domain and subdomain
		var zoneRecords = await loopiaApi.GetZoneRecordsAsync(domain, subdomain);

		// Locate the record that we want to update
		var ipRecord = zoneRecords.Single(x => x.Type == "A");

		// Check if the record is already up-to-date
		if(ipRecord.RData == externalIP)
		{
			// IP already up-to-date, no action required
			return ResponseStatus.Ok;
		}

		// Update record with new IP
		ipRecord.RData = externalIP;

		// Send update-request to Loopia
		return await loopiaApi.UpdateZoneRecordAsync(domain, subdomain, ipRecord);
	}

	private async Task<String> GetExternalIPAsync()
	{
		var getResponse = await HttpClient.GetAsync("https://api.ipify.org/?format=txt");
		return await getResponse.Content.ReadAsStringAsync();
	}
}

Version history

1.0.0 - Initial release

Base16.LoopiaApi

Introduction

Base16.LoopiaApi is a simple .NET Standard 2.0 XML-RPC wrapper for Loopias API.
The wrapper makes it easy to use the public customer-facing API described in the official documentation.
This library can for example very simply be used to update a DNS record to point to a specific IP.

Prerequisites

In order to use the functionality exposed by this library (and the Loopia API in general) you'll need to have at least a DNS account registered with Loopia. You'll also need to create an API-user ("API-användare") under your account and ensure that the user has the appropriate access rights.

Limitations & Known Issues

  • Loopias official limitations apply
  • Most partner API's have not been implemented sine they cannot be fully verified without being a partner
  • Encoding for IDN-domains seems to be wrong on invoice_item_obj - This is only an issue when viewing these domains on invoice-lines

Simple usage example

Note this example only serves to illustrate the usage of the API.

using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Base16.LoopiaApi;
using Base16.LoopiaApi.Models;

class Program
{
	private static readonly HttpClient HttpClient = new HttpClient();

	public async Task Main()
	{
		var externalIP = await GetExternalIPAsync();

		var result = await UpdateZoneRecordAsync(
			apiUsername: "--ENTER-YOUR-API-USERNAME-HERE--",
			apiPassword: "--ENTER-YOUR-API-PASSWORD-HERE--",
			domain: "--ENTER-YOUR-DOMAIN-NAME-HERE--(ex: google.se)--",
			subdomain: "@",
			externalIP: externalIP
		);

		Console.WriteLine($"{nameof(UpdateZoneRecordAsync)} returned {result}");
	}

	private async Task<ResponseStatus> UpdateZoneRecordAsync(
		String apiUsername,
		String apiPassword,
		String domain,
		String subdomain,
		String externalIP
	)
	{
		// Initialize LoopiaApiClient given a username and password.
		// 'customerNumber' is only valid for retailers and should otherwise be omitted.
		var loopiaApi = new LoopiaApiClient(
			username: apiUsername,
			password: apiPassword,
			customerNumber: null
		);

		// Get all zone-records for the given domain and subdomain
		var zoneRecords = await loopiaApi.GetZoneRecordsAsync(domain, subdomain);

		// Locate the record that we want to update
		var ipRecord = zoneRecords.Single(x => x.Type == "A");

		// Check if the record is already up-to-date
		if(ipRecord.RData == externalIP)
		{
			// IP already up-to-date, no action required
			return ResponseStatus.Ok;
		}

		// Update record with new IP
		ipRecord.RData = externalIP;

		// Send update-request to Loopia
		return await loopiaApi.UpdateZoneRecordAsync(domain, subdomain, ipRecord);
	}

	private async Task<String> GetExternalIPAsync()
	{
		var getResponse = await HttpClient.GetAsync("https://api.ipify.org/?format=txt");
		return await getResponse.Content.ReadAsStringAsync();
	}
}

Version history

1.0.0 - Initial release

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 175 9/12/2019