-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eff604
commit 5be6a78
Showing
3 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Domain> 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<string>()}"); | ||
return JsonConvert.DeserializeObject<Domain>(content); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Link> 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<string>()}"); | ||
return JsonConvert.DeserializeObject<Link>(content); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters