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 - +