KSoft-Si / KSoft.Net

Wrapper for the KSoft API written in C#

Home Page:https://api.ksoft.si/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KSoft.si API Wrapper

nuget version downloads

http://api.ksoft.si

Overview

KSoft.Si API is a professional, fast, reliable and easy to use multi-function service for Discord bot developers or websites. It provides easy, no-nonsense endpoints with an extensive library of images, user data, weather, geo-location and songs. We help you build awesome services and keep Discord community safe and sound.

If you find any errors/issues or want any features added, create an issue

Getting started

Installing the package

Add the NuGet package KSoftNet to your project:

dotnet add package KSoftNet

Simple usage

A minimal example to get a random image by tag

A complete example is available in the examples/SimpleUsage project

using System;
using System.Threading.Tasks;
using KSoftNet;

public class ExampleClass {
  private readonly KSoftApi _kSoftApi;

  public ExampleClass(string token) {
    _kSoftApi = new KSoftApi(token);
  }

  public async Task GetRandomImage(string tag) {
    var image = await _kSoftApi.ImagesApi.GetRandomImage(tag: tag);

    Console.WriteLine(image.Url);
  }
}

Example using custom HttpClient

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

A complete example is available in the examples/CustomHttpclient project

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

public ExampleClass(string token) {
  var httpClient = new HttpClient {
    BaseAddress = new Uri("https://api.ksoft.si"),
    DefaultRequestHeaders = {
      Authorization = new AuthenticationHeaderValue("Bearer", token)
    }
  };

  _kSoftApi = new KSoftApi(httpClient);
}

Example using dependency injection

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

A complete example is available in the examples/DependencyInjection project

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

public class Startup {
  private const string Token = "{token}";
  private KSoftApi _kSoftApi;

  private IServiceProvider _serviceProvider;

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

    _kSoftApi = new KSoftApi(Token);

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

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

    var image = await exampleClass.GetRandomImage("birb");

    Console.WriteLine(image.Url);
  }

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

Using this in a class:

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

public class ExampleClass {
  private readonly KSoftApi _kSoftApi;

  public ExampleClass(KSoftApi kSoftApi) {
    _kSoftApi = kSoftApi;
  }

  public async Task<Image> GetRandomImage(string tag) {
    var image = await _kSoftApi.ImagesApi.GetRandomImage(tag: tag);

    return image;
  }
}

About

Wrapper for the KSoft API written in C#

https://api.ksoft.si/

License:GNU General Public License v3.0


Languages

Language:C# 100.0%