MartineAPI is a an API made by the owner of Martine Bot
If you find any errors/issues or want any features added, create an issue
Add the NuGet package MartineApiNet
to your project:
dotnet add package MartineApiNet
using System;
using System.Threading.Tasks;
using MartineApiNet;
public class ExampleClass
{
private readonly MartineApi _martineApi;
public ExampleClass()
=> _martineApi = new MartineApi();
public async Task GetRandomMeme()
{
var image = await _martineApi.RedditApi.GetRandomMeme();
Console.WriteLine(image.Data.PostUrl);
}
}
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 MartineApiNet;
public ExampleClass()
{
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.martinebot.com/v1")
};
_martineApi = new MartineApi(httpClient);
}
Create a ServiceCollection, then add an instance of the MartineApi class to it
using System;
using System.Threading.Tasks;
using MartineApiNet;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
private MartineApi _martineApi;
private IServiceProvider _serviceProvider;
public void Init()
{
var services = new ServiceCollection();
_martineApi = new MartineApi();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
}
public async Task RunAsync()
{
var exampleClass = _serviceProvider.GetService<ExampleClass>();
var image = await exampleClass.GetRandomMeme();
Console.WriteLine(image.Data.PostUrl);
}
private void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(_martineApi);
services.AddSingleton<ExampleClass>();
}
}
Using this in a class:
using System.Threading.Tasks;
using MartineApiNet;
using MartineApiNet.Models.Images;
public class ExampleClass
{
private readonly MartineApi _martineApi;
public ExampleClass(MartineApi martineApi)
=> _martineApi = martineApi;
public async Task<RedditPost> GetRandomMeme()
=> await _martineApi.GetRandomMeme();
}