Skip to content

wayneyaoo/Omg.Lol.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Omg.Lol.Net

Codecov Nuget (with prereleases)

GitHub Supported framework

An unofficial .NET SDK for the lovely omg.lol API service! If you haven't checked it out, please do so : ) $20 a year brings so much fun!

Disclaimer:
I'm not affliated with the service in any way. I'm one of the customers and a random dev.

Warning:
The omg.lol API service is still working in progress, hence this downstream library can only be, at most, as stable as the upstream API. Also expect breaking change until the version reaches 1.0.0.

  • net6, net7, netstandard2.0 and netstandard2.1 support
  • Clean interface design with testability in mind
  • Lazy evaluation
  • Fully asynchronous
  • Extensibility in core components.

API Coverage

Gaps between the API and this library is tracked in this issue.

Installation

dotnet add package Omg.Lol.Net --prerelease # Only prerelease is available now.

Or see Omg.Lol.Net on nuget.org.

Usage

The main interface client code should interact with is IOmgLolClient. An instance of it can (only) be created via OmgLolClientBuilder. If you have specific needs (custom HttpClient, custom way to fetch your API key from different sources), see extensivility section for more details.

var client = OmgLolClient.Create("my-api-key");

With the client, you can dive into different sub-clients to consume the API.

// service statistics.
var serviceStatistics = await client.ServiceClient.GetServiceStatisticsAsync();

// post a PURL
var response = await client.PurlsClient
                    .CreatePurlAsync(
                        "my-address",
                        new PurlPost()
                        {
                            name = "example",
                            url = "https://example.com",
                        });

// ...

Extensibility

Custom HttpClient

If you need to inject your instance of HttpClient, you can achieve this via

  • Create a whole new implementation of interface IHttpClient, or inherit DefaultHttpClient then override GetHttpClient to return your instance. For example, this creates a custom HttpClient with proxy support.

    public class ProxyClient : DefaultHttpClient
    {
        protected override HttpClient GetHttpClient()
            => new HttpClient(new HttpClientHandler()
               {
                    Proxy = new WebProxy("http:https://127.0.0.1:8080"),
               })
    }
  • Create an implementation of interface IHttpClientFactory to return your implementation of IHttpClient.

    public class MyHttpClientFactory : IHttpClientFactory
    {
        public IHttpClient GetHttpClient() => new ProxyClient();
    
        // Or you can create a new implementation of IHttpClient as well.
        //public IHttpClient GetHttpClient() => new MyNewHttpClient();
    }
  • When creating an IOmgLolClient with the builder, use another overload:

    var client = OmgLolClientBuilder.Create("my-api-key", new MyHttpClientFactory());

As creating an instance with the builder is likely to happen at the early stage of an application initialization, " injecting" your factory implementation is a bit cumbersome so the library doesn't bother doing so. You might still want to new it there.

Because of lazy evaluation, the IHttpClient is only created when making a real network call.

Custom API key fetching from other sources

Your API key is sensitive for sure, and could be stored in different sources (environment variable, file, remote secret storage, another computer etc.) with special ways to fetch. To make things easier for you (and me), there're two overloads in the client builder:

// A simple overload to get key via a callback
var client = await OmgLolClientBuilder.Create(
            () => Task.FromResult("use-whatever-logic-to-fetch-the-key"));

// Or if the logic is complicated enough, create an implementation of interface IApiKeyProvider
public class TestApiKeyProvider : IApiKeyProvider
{
    public Task<string> GetApiKeyAsync() => Task.FromResult("my-api-key");
}

// and use this overload
var client = await OmgLolClientBuilder.Create(new TestApiKeyProvider());