diff --git a/src/StreamElements.Net/AuthRestClient.cs b/src/StreamElements.Net/AuthRestClient.cs index 24aa55b..bb7c81f 100644 --- a/src/StreamElements.Net/AuthRestClient.cs +++ b/src/StreamElements.Net/AuthRestClient.cs @@ -13,6 +13,7 @@ namespace StreamElements.Net { private string JwtToken { get; } private IAuthenticatedStreamElementsApi Client { get; } + private IGenericRestEndpoint BotCommandClient { get; } public AuthRestClient(string jwtToken) { this.JwtToken = jwtToken; @@ -21,6 +22,7 @@ namespace StreamElements.Net throw new ArgumentNullException(nameof(jwtToken)); } this.Client = BuildHttpClient(); + BotCommandClient = BuildHttpClient>("bot/commands"); } /// /// 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. /// /// - public Task GetBotLevels() => Client.GetBotLevels(); + public Task GetBotLevels() => Client.GetBotLevelsAsync(); /// /// Create a new permission for the current channel. /// @@ -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() + + public Task> GetBotCommandsAsync() => BotCommandClient.GetAllAsync(); + + public Task 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 PostBotCommand(BotCommand command) + { + if(command == null) throw new ArgumentNullException(nameof(command)); + return BotCommandClient.CreateAsync(command); + } + + public Task 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(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(httpClient); } diff --git a/src/StreamElements.Net/Models/BotCommand.cs b/src/StreamElements.Net/Models/BotCommand.cs new file mode 100644 index 0000000..a3ae158 --- /dev/null +++ b/src/StreamElements.Net/Models/BotCommand.cs @@ -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 Aliases { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs b/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs index eab2c35..69fd2c5 100644 --- a/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs +++ b/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs @@ -5,7 +5,7 @@ using StreamElements.Net.Models; namespace StreamElements.Net.Rest { - public interface IAuthenticatedStreamElementsApi + internal interface IAuthenticatedStreamElementsApi { [Get("/activities")] Task> GetActivitiesAsync(); @@ -26,9 +26,9 @@ namespace StreamElements.Net.Rest Task PostBotSayAsync([Body] object message); [Get("/bot/levels")] - Task GetBotLevels(); + Task GetBotLevelsAsync(); [Post("/bot/levels")] - Task PostBotLevel([Body] object submit); + Task PostBotLevelAsync([Body] object submit); } } \ No newline at end of file diff --git a/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs b/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs new file mode 100644 index 0000000..aa93d48 --- /dev/null +++ b/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs @@ -0,0 +1,23 @@ +using System.Threading.Tasks; +using Refit; + +namespace StreamElements.Net.Rest +{ + public interface IGenericRestEndpoint where T : class + { + [Get("")] + Task> GetAllAsync(); + + [Get("/{key}")] + Task GetOneAsync(TKey key); + + [Post("")] + Task CreateAsync([Body] T paylod); + + [Put("/{key}")] + Task UpdateAsync(TKey key, [Body]T payload); + + [Delete("/{key}")] + Task DeleteAsync(TKey key); + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/RestClient.cs b/src/StreamElements.Net/RestClient.cs index 0d51827..ff64c8b 100644 --- a/src/StreamElements.Net/RestClient.cs +++ b/src/StreamElements.Net/RestClient.cs @@ -7,9 +7,11 @@ namespace StreamElements.Net { public class RestClient { - public virtual T BuildHttpClient() + public virtual T BuildHttpClient(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(httpClient); } } diff --git a/src/StreamElements.Net/StreamElements.Net.csproj b/src/StreamElements.Net/StreamElements.Net.csproj index 3389d77..3db7baa 100644 --- a/src/StreamElements.Net/StreamElements.Net.csproj +++ b/src/StreamElements.Net/StreamElements.Net.csproj @@ -4,5 +4,6 @@ + \ No newline at end of file