From 5be6a78d5adcf371d1887fd0d8a76498c858f4ae Mon Sep 17 00:00:00 2001 From: Nguyen Thanh Quang Date: Wed, 30 Jun 2021 13:05:55 +0700 Subject: [PATCH] Added fluent methods --- Kutt.NET/Fluent/DomainCreationRequest.cs | 50 ++++++++++++++ Kutt.NET/Fluent/LinkCreationRequest.cs | 88 ++++++++++++++++++++++++ Kutt.NET/Kutt.NET.csproj | 9 ++- 3 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 Kutt.NET/Fluent/DomainCreationRequest.cs create mode 100644 Kutt.NET/Fluent/LinkCreationRequest.cs diff --git a/Kutt.NET/Fluent/DomainCreationRequest.cs b/Kutt.NET/Fluent/DomainCreationRequest.cs new file mode 100644 index 0000000..c0f446c --- /dev/null +++ b/Kutt.NET/Fluent/DomainCreationRequest.cs @@ -0,0 +1,50 @@ +using System; +using System.Threading.Tasks; +using Kutt.NET.Domains; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using RestSharp; +using static Kutt.NET.Constants; + +namespace Kutt.NET.Fluent +{ + public class DomainCreationRequest + { + public DomainCreationRequest(string apiKey, string domain) + { + ApiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); + Domain = domain ?? throw new ArgumentNullException(nameof(domain)); + } + + internal string ApiKey { get; } + + public string Domain { get; private set; } + + public string Homepage { get; private set; } + + public DomainCreationRequest WithCustomHomepage(string homepage) + { + Homepage = homepage; + return this; + } + + public async Task SubmitAsync(string server = API_URL) + { + var body = new + { + address = Domain ?? throw new ArgumentNullException(nameof(Domain)), + homepage = Homepage + }; + var client = new RestClient(server); + var request = new RestRequest(DOMAINS_ENDPOINT, DataFormat.Json); + request.AddHeader("X-API-KEY", ApiKey); + request.AddJsonBody(body); + var response = await client.ExecutePostAsync(request); + var content = response.Content; + if (!response.IsSuccessful) + throw new KuttException + ($"{(int) response.StatusCode}: {(JObject.Parse(content)["error"] ?? "").Value()}"); + return JsonConvert.DeserializeObject(content); + } + } +} \ No newline at end of file diff --git a/Kutt.NET/Fluent/LinkCreationRequest.cs b/Kutt.NET/Fluent/LinkCreationRequest.cs new file mode 100644 index 0000000..7290c07 --- /dev/null +++ b/Kutt.NET/Fluent/LinkCreationRequest.cs @@ -0,0 +1,88 @@ +using System; +using System.Threading.Tasks; +using Kutt.NET.Links; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using RestSharp; +using static Kutt.NET.Constants; + +namespace Kutt.NET.Fluent +{ + public class LinkCreationRequest + { + public LinkCreationRequest(string apiKey, string target) + { + ApiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); + Target = target ?? throw new ArgumentNullException(nameof(target)); + } + + internal string ApiKey { get; } + public string Target { get; private set; } + public string Domain { get; private set; } + public string Expiration { get; private set; } + public string Password { get; private set; } + public bool Reuse { get; private set; } + public string Slug { get; private set; } + public string Description { get; private set; } + + public LinkCreationRequest WithPassword(string password) + { + Password = password; + return this; + } + + public LinkCreationRequest ToggleReuse() + { + Reuse = true; + return this; + } + + public LinkCreationRequest WithDescription(string description) + { + Description = description; + return this; + } + + public LinkCreationRequest WithCustomSlug(string slug) + { + Slug = slug; + return this; + } + + public LinkCreationRequest WithExpiration(string expiration) + { + Expiration = expiration; + return this; + } + + public LinkCreationRequest WithCustomDomain(string domain) + { + Domain = domain; + return this; + } + + public async Task SubmitAsync(string server = API_URL) + { + var body = new + { + target = Target ?? throw new ArgumentNullException(nameof(Target)), + domain = Domain, + customurl = Slug, + description = Description, + expire_in = Expiration, + password = Password, + reuse = Reuse + }; + var client = new RestClient(server); + var request = new RestRequest(LINKS_ENDPOINT, DataFormat.Json); + request.AddHeader("X-API-KEY", ApiKey); + request.AddJsonBody(body); + var response = await client.ExecutePostAsync(request); + var content = response.Content; + if (!response.IsSuccessful) + throw new KuttException + ($"{(int) response.StatusCode}: {(JObject.Parse(content)["error"] ?? "").Value()}"); + return JsonConvert.DeserializeObject(content); + } + } +} \ No newline at end of file diff --git a/Kutt.NET/Kutt.NET.csproj b/Kutt.NET/Kutt.NET.csproj index 2d235f8..670dc7f 100644 --- a/Kutt.NET/Kutt.NET.csproj +++ b/Kutt.NET/Kutt.NET.csproj @@ -1,5 +1,4 @@ - netstandard2.0 true @@ -10,14 +9,14 @@ https://github.com/AlphaNecron/Kutt.NET git kutt url-shortener netstandard library - 1.0.3 - 1.0.3 + 1.0.4 + 1.0.4 kutt.png MIT - 1.0.3 + 1.0.4 - +