CS C#


Installation

Add the NuGet package NekosBestApiNet to your project:

dotnet add package NekosBestApiNet

Usage

using System;
using System.Threading.Tasks;
using NekosBestApiNet;

public class ExampleClass
{
  private readonly NekosBestApi _nekosBestApi;

  public ExampleClass() 
    => _nekosBestApi = new NekosBestApi();

  public async Task Hug() 
  {
    var image = await _nekosBestApi.ActionsApi.Hug();

    Console.WriteLine(image.Results.FirstOrDefault().Url);
  }
}

Example using custom HttpClient

You can provide your own HttpClient instance, but you have to set the BaseAddress manually

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using NekosBestApiNet;

class ExampleClass
{
    private NekosBestApi _nekosBestApi;

    public ExampleClass()
    {
        var httpClient = new HttpClient
        {
            BaseAddress = new Uri("https://nekos.best/api/v2")
        };

        _nekosBestApi = new NekosBestApi(httpClient);
    }
}

Example using dependency injection

Create a ServiceCollection, then add an instance of the NekosBestApi class to it

using System;
using System.Threading.Tasks;
using NekosBestApiNet;
using Microsoft.Extensions.DependencyInjection;

public class Startup 
{
  private NekosBestApi _nekosBestApi;

  private IServiceProvider _serviceProvider;

  public void Init() 
  {
    var services = new ServiceCollection();

    _nekosBestApi = new NekosBestApi();

    ConfigureServices(services);
    _serviceProvider = services.BuildServiceProvider();
  }

  public async Task RunAsync() 
  {
    var exampleClass = _serviceProvider.GetService<ExampleClass>();

    var image = await _nekosBestApi.ActionsApi.Hug();

    Console.WriteLine(image.Results.FirstOrDefault().Url);
  }

  private void ConfigureServices(IServiceCollection services) 
  {
    services.AddSingleton(_nekosBestApi);
    services.AddSingleton<ExampleClass>();
  }
}

Using this in a class

using System.Threading.Tasks;
using NekosBestApiNet;
using NekosBestApiNet.Models.Images;

public class ExampleClass 
{
  private readonly NekosBestApi _nekosBestApi;

  public ExampleClass(NekosBestApi nekosBestApi) 
    => _nekosBestApi = nekosBestApi;

  public async Task<ActionResult> Hug()
    => await _nekosBestApi.Hug();
}

About

Example added by: SylveonDeko

The source code is available at GitHub

NekosBest NekosBest NekosBest