add bot commands

This commit is contained in:
Tommy Parnell
2017-09-23 16:08:16 -04:00
parent 10815576fc
commit cc452af65d
6 changed files with 110 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ namespace StreamElements.Net
{
private string JwtToken { get; }
private IAuthenticatedStreamElementsApi Client { get; }
private IGenericRestEndpoint<BotCommand,string> BotCommandClient { get; }
public AuthRestClient(string jwtToken)
{
this.JwtToken = jwtToken;
@@ -21,6 +22,7 @@ namespace StreamElements.Net
throw new ArgumentNullException(nameof(jwtToken));
}
this.Client = BuildHttpClient<IAuthenticatedStreamElementsApi>();
BotCommandClient = BuildHttpClient<IGenericRestEndpoint<BotCommand, string>>("bot/commands");
}
/// <summary>
/// Returns a list of activities sorted by createdAt.
@@ -78,7 +80,7 @@ namespace StreamElements.Net
/// Returns an array of users with levels in your channel.
/// </summary>
/// <returns></returns>
public Task<BotLevels> GetBotLevels() => Client.GetBotLevels();
public Task<BotLevels> GetBotLevels() => Client.GetBotLevelsAsync();
/// <summary>
/// Create a new permission for the current channel.
/// </summary>
@@ -89,11 +91,40 @@ namespace StreamElements.Net
{
throw new ArgumentNullException(nameof(userName));
}
return Client.PostBotLevel(new { username = userName, level = botEnum});
return Client.PostBotLevelAsync(new { username = userName, level = botEnum});
}
public override T BuildHttpClient<T>()
public Task<List<BotCommand>> GetBotCommandsAsync() => BotCommandClient.GetAllAsync();
public Task<BotCommand> GetBotCommandAsync(string id)
{
var httpClient = new HttpClient() { BaseAddress = new Uri("https://api.streamelements.com/kappa/v1") };
if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
return BotCommandClient.GetOneAsync(id);
}
public Task<BotCommand> PostBotCommand(BotCommand command)
{
if(command == null) throw new ArgumentNullException(nameof(command));
return BotCommandClient.CreateAsync(command);
}
public Task<BotCommand> PutBotCommand (string id, BotCommand command)
{
if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
if(command == null) throw new ArgumentNullException(nameof(command));
return BotCommandClient.UpdateAsync(id, command);
}
public Task DeleteCommand(string id)
{
if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
return BotCommandClient.DeleteAsync(id);
}
public override T BuildHttpClient<T>(string pathSegment = null)
{
var builder = new UriBuilder("https://api.streamelements.com/kappa/v1");
if(!string.IsNullOrEmpty(pathSegment)) builder.WithPathSegment(pathSegment);
var httpClient = new HttpClient() { BaseAddress = builder.Uri };
httpClient.DefaultRequestHeaders.Add("authorization", $"Bearer {this.JwtToken}");
return RestService.For<T>(httpClient);
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace StreamElements.Net.Models
{
public class Cooldown
{
[JsonProperty("global")]
public int Global { get; set; }
[JsonProperty("user")]
public int User { get; set; }
}
public class BotCommand
{
[JsonProperty("command")]
public string Command { get; set; }
[JsonProperty("reply")]
public string Reply { get; set; }
[JsonProperty("accessLevel")]
public BotActionEnum AccessLevel { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("cost")]
public int Cost { get; set; }
[JsonProperty("cooldown")]
public Cooldown Cooldown { get; set; }
[JsonProperty("enabled")]
public bool Enabled { get; set; }
[JsonProperty("aliases")]
public IList<string> Aliases { get; set; }
}
}

View File

@@ -5,7 +5,7 @@ using StreamElements.Net.Models;
namespace StreamElements.Net.Rest
{
public interface IAuthenticatedStreamElementsApi
internal interface IAuthenticatedStreamElementsApi
{
[Get("/activities")]
Task<IList<Activity>> GetActivitiesAsync();
@@ -26,9 +26,9 @@ namespace StreamElements.Net.Rest
Task<Result> PostBotSayAsync([Body] object message);
[Get("/bot/levels")]
Task<BotLevels> GetBotLevels();
Task<BotLevels> GetBotLevelsAsync();
[Post("/bot/levels")]
Task<SubmitBotLevelResponse> PostBotLevel([Body] object submit);
Task<SubmitBotLevelResponse> PostBotLevelAsync([Body] object submit);
}
}

View File

@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Refit;
namespace StreamElements.Net.Rest
{
public interface IGenericRestEndpoint<T, in TKey> where T : class
{
[Get("")]
Task<System.Collections.Generic.List<T>> GetAllAsync();
[Get("/{key}")]
Task<T> GetOneAsync(TKey key);
[Post("")]
Task<T> CreateAsync([Body] T paylod);
[Put("/{key}")]
Task<T> UpdateAsync(TKey key, [Body]T payload);
[Delete("/{key}")]
Task DeleteAsync(TKey key);
}
}

View File

@@ -7,9 +7,11 @@ namespace StreamElements.Net
{
public class RestClient
{
public virtual T BuildHttpClient<T>()
public virtual T BuildHttpClient<T>(string pathSegment = null)
{
var httpClient = new HttpClient() { BaseAddress = new Uri("https://api.streamelements.com/kappa/v1") };
var builder = new UriBuilder("https://api.streamelements.com/kappa/v1");
if(!string.IsNullOrEmpty(pathSegment)) builder.WithPathSegment(pathSegment);
var httpClient = new HttpClient() { BaseAddress = builder.Uri };
return RestService.For<T>(httpClient);
}
}

View File

@@ -4,5 +4,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="refit" Version="4.0.1" />
<PackageReference Include="UriBuilder.Fluent" Version="1.4.0" />
</ItemGroup>
</Project>