Skip to content

Latest commit

 

History

History
210 lines (164 loc) · 9.84 KB

File metadata and controls

210 lines (164 loc) · 9.84 KB

AdGuard.ApiClient - the C# library for the AdGuard DNS API

DNS API documentation

This C# SDK is automatically generated by the OpenAPI Generator project:

  • API version: 1.11
  • SDK version: 1.0.0
  • Generator version: 7.16.0
  • Build package: org.openapitools.codegen.languages.CSharpClientCodegen

Updating the API Client

To update this API client from the latest OpenAPI specification:

  • Download OpenAPI Spec: See api/README.md for instructions on obtaining the latest spec
  • Regenerate Client: Use ./regenerate-client.sh (Linux/macOS) or .\Regenerate-Client.ps1 (Windows)
  • Complete Guide: See OPENAPI_UPDATE_GUIDE.md for detailed instructions

Frameworks supported

Dependencies

The DLLs included in the package may not be the latest version. We recommend using NuGet to obtain the latest version of the packages:

Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
Install-Package System.ComponentModel.Annotations

Installation

Run the following command to generate the DLL

  • [Mac/Linux] /bin/sh build.sh
  • [Windows] build.bat

Then include the DLL (under the bin folder) in the C# project, and use the namespaces:

using AdGuard.ApiClient.Api;
using AdGuard.ApiClient.Client;
using AdGuard.ApiClient.Model;

Packaging

A .nuspec is included with the project. You can follow the Nuget quickstart to create and publish packages.

This .nuspec uses placeholders from the .csproj, so build the .csproj directly:

nuget pack -Build -OutputDirectory out AdGuard.ApiClient.csproj

Then, publish to a local feed or other host and consume the new package via Nuget as usual.

Usage

To use the API client with a HTTP proxy, setup a System.Net.WebProxy

Configuration c = new Configuration();
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;

Connections

Each ApiClass (properly the ApiClient inside it) will create an instance of HttpClient. It will use that for the entire lifecycle and dispose it when called the Dispose method.

To better manager the connections it's a common practice to reuse the HttpClient and HttpClientHandler (see here for details). To use your own HttpClient instance just pass it to the ApiClass constructor.

HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);

If you want to use an HttpClient and don't have access to the handler, for example in a DI context in Asp.net Core when using IHttpClientFactory.

HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient);

You'll loose some configuration settings, the features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. You need to either manually handle those in your setup of the HttpClient or they won't be available.

Here an example of DI setup in a sample web project:

services.AddHttpClient<YourApiClass>(httpClient =>
   new PetApi(httpClient));

Getting Started

using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using AdGuard.ApiClient.Api;
using AdGuard.ApiClient.Client;
using AdGuard.ApiClient.Model;

namespace Example
{
    public class Example
    {
        public static void Main()
        {

            Configuration config = new Configuration();
            config.BasePath = "https://api.adguard-dns.io";
            // Configure API key authorization: ApiKey
            config.ApiKey.Add("Authorization", "YOUR_API_KEY");
            // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
            // config.ApiKeyPrefix.Add("Authorization", "Bearer");
            // Configure Bearer token for authorization: AuthToken
            config.AccessToken = "YOUR_BEARER_TOKEN";

            // create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
            HttpClient httpClient = new HttpClient();
            HttpClientHandler httpClientHandler = new HttpClientHandler();
            var apiInstance = new AccountApi(httpClient, config, httpClientHandler);

            try
            {
                // Gets account limits
                AccountLimits result = apiInstance.GetAccountLimits();
                Debug.WriteLine(result);
            }
            catch (ApiException e)
            {
                Debug.Print("Exception when calling AccountApi.GetAccountLimits: " + e.Message );
                Debug.Print("Status Code: "+ e.ErrorCode);
                Debug.Print(e.StackTrace);
            }

        }
    }
}

Documentation for API Endpoints

All URIs are relative to https://api.adguard-dns.io

Class Method HTTP request Description
AccountApi GetAccountLimits GET /oapi/v1/account/limits Gets account limits
AuthenticationApi AccessToken POST /oapi/v1/oauth_token Generates Access and Refresh token
DNSServersApi CreateDNSServer POST /oapi/v1/dns_servers Creates a new DNS server
DNSServersApi ListDNSServers GET /oapi/v1/dns_servers Lists DNS servers that belong to the user.
DedicatedIPAddressesApi AllocateDedicatedIPv4Address POST /oapi/v1/dedicated_addresses/ipv4 Allocates new dedicated IPv4
DedicatedIPAddressesApi ListDedicatedIPv4Addresses GET /oapi/v1/dedicated_addresses/ipv4 Lists allocated dedicated IPv4 addresses
DevicesApi CreateDevice POST /oapi/v1/devices Creates a new device
DevicesApi GetDevice GET /oapi/v1/devices/{device_id} Gets an existing device by ID
DevicesApi ListDevices GET /oapi/v1/devices Lists devices
DevicesApi RemoveDevice DELETE /oapi/v1/devices/{device_id} Removes a device
DevicesApi UpdateDevice PUT /oapi/v1/devices/{device_id} Updates an existing device
FilterListsApi ListFilterLists GET /oapi/v1/filter_lists Gets filter lists
QueryLogApi ClearQueryLog DELETE /oapi/v1/query_log Clears query log
QueryLogApi GetQueryLog GET /oapi/v1/query_log Gets query log
StatisticsApi GetTimeQueriesStats GET /oapi/v1/stats/time Gets time statistics
WebServicesApi ListWebServices GET /oapi/v1/web_services Lists web services

Documentation for Models

Documentation for Authorization

Authentication schemes defined for the API:

ApiKey

  • Type: API key
  • API key parameter name: Authorization
  • Location: HTTP header

AuthToken

  • Type: Bearer Authentication