Skip to content

Commit

Permalink
Custom server support & link expiration support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaNecron committed Jun 29, 2021
1 parent e902bca commit 2eff604
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Kutt.NET/DomainsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<Domain> CreateDomainAsync(string address, string homepage = ""
};
var request = new RestRequest(DOMAINS_ENDPOINT, DataFormat.Json);
request.AddJsonBody(body);
var response = await Client.ExecutePostAsync(request);
var response = await _client.ExecutePostAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand All @@ -40,7 +40,7 @@ public async Task DeleteDomainAsync(string uuid)
{
var request = new RestRequest(DOMAINS_ENDPOINT_WITH_ID, Method.DELETE, DataFormat.Json);
request.AddUrlSegment("id", uuid ?? throw new ArgumentNullException(nameof(uuid)));
var response = await Client.ExecuteAsync(request);
var response = await _client.ExecuteAsync(request);
if (!response.IsSuccessful)
throw new KuttException
($"{(int) response.StatusCode}: {(JObject.Parse(response.Content)["error"] ?? "").Value<string>()}");
Expand Down
6 changes: 3 additions & 3 deletions Kutt.NET/Kutt.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<RepositoryUrl>https://github.com/AlphaNecron/Kutt.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>kutt url-shortener netstandard library</PackageTags>
<AssemblyVersion>1.0.2</AssemblyVersion>
<FileVersion>1.0.2</FileVersion>
<AssemblyVersion>1.0.3</AssemblyVersion>
<FileVersion>1.0.3</FileVersion>
<PackageIcon>kutt.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageVersion>1.0.2</PackageVersion>
<PackageVersion>1.0.3</PackageVersion>
</PropertyGroup>
<ItemGroup>
<None Include="kutt.png" Pack="true" PackagePath="$(PackageIcon)"/>
Expand Down
17 changes: 14 additions & 3 deletions Kutt.NET/KuttApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ namespace Kutt.NET
{
public partial class KuttApi
{
private readonly RestClient Client;
private readonly RestClient _client;

public KuttApi(string apiKey)
{
ApiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
Client = new RestClient(API_URL);
Client.AddDefaultHeader("X-API-KEY", ApiKey);
_client = new RestClient(API_URL);
_client.AddDefaultHeader("X-API-KEY", ApiKey);
}


public KuttApi(string apiKey, string customServer)
{
ApiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_client = new RestClient(Uri.IsWellFormedUriString(customServer,
UriKind.Absolute)
? customServer
: throw new ArgumentException("Please provide a valid server."));
_client.AddDefaultHeader("X-API-KEY", ApiKey);
}

private string ApiKey { get; }
Expand Down
22 changes: 13 additions & 9 deletions Kutt.NET/LinksEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<ListOfLinks> GetListOfLinksAsync(
request.AddQueryParameter("limit", limit.ToString());
request.AddQueryParameter("skip", skip.ToString());
request.AddQueryParameter("all", all.ToString().ToLower());
var response = await Client.ExecuteGetAsync(request);
var response = await _client.ExecuteGetAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand All @@ -48,7 +48,7 @@ public async Task DeleteLinkAsync(string uuid)
{
var request = new RestRequest(LINKS_ENDPOINT_WITH_ID, Method.DELETE, DataFormat.Json);
request.AddUrlSegment("id", uuid ?? throw new ArgumentNullException(nameof(uuid)));
var response = await Client.ExecuteAsync(request);
var response = await _client.ExecuteAsync(request);
if (!response.IsSuccessful)
throw new KuttException
($"{(int) response.StatusCode}: {(JObject.Parse(response.Content)["error"] ?? "").Value<string>()}");
Expand All @@ -60,25 +60,26 @@ public async Task DeleteLinkAsync(string uuid)
/// <param name="uuid">UUID</param>
/// <param name="target">Target</param>
/// <param name="slug">Slug</param>
/// <param name="expireIn">2 minutes/hours/days</param>
/// <param name="description">The Description</param>
/// <returns>Updated link as <see cref="Link" /></returns>
public async Task<Link> UpdateLinkAsync(
string uuid,
public async Task<Link> UpdateLinkAsync(string uuid,
string target,
string slug,
string description = ""
)
string expireIn = "",
string description = "")
{
var body = new
{
target = target ?? throw new ArgumentNullException(nameof(target)),
address = slug ?? throw new ArgumentNullException(nameof(slug)),
expire_in = expireIn,
description
};
var request = new RestRequest(LINKS_ENDPOINT_WITH_ID, Method.PATCH, DataFormat.Json);
request.AddUrlSegment("id", uuid ?? throw new ArgumentNullException(nameof(uuid)));
request.AddJsonBody(body);
var response = await Client.ExecuteAsync(request);
var response = await _client.ExecuteAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand All @@ -97,7 +98,7 @@ public async Task<LinkStats> GetLinkStatsAsync(string uuid)
{
var request = new RestRequest(LINK_STATS_ENDPOINT, Method.PATCH, DataFormat.Json);
request.AddUrlSegment("id", uuid ?? throw new ArgumentNullException(nameof(uuid)));
var response = await Client.ExecuteAsync(request);
var response = await _client.ExecuteAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand All @@ -114,11 +115,13 @@ public async Task<LinkStats> GetLinkStatsAsync(string uuid)
/// <param name="slug">Slug</param>
/// <param name="reuse">Reuse</param>
/// <param name="password">Password</param>
/// <param name="expireIn">2 minutes/hours/days</param>
/// <returns>Shortened link as <see cref="Link" /></returns>
public async Task<Link> CreateLinkAsync(
string longUrl,
string domain = "",
string description = "",
string expireIn = "",
string slug = "",
bool reuse = false,
string password = "")
Expand All @@ -129,12 +132,13 @@ public async Task<Link> CreateLinkAsync(
domain,
customurl = slug,
description,
expire_in = expireIn,
password,
reuse
};
var request = new RestRequest(LINKS_ENDPOINT, DataFormat.Json);
request.AddJsonBody(body);
var response = await Client.ExecutePostAsync(request);
var response = await _client.ExecutePostAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand Down
2 changes: 1 addition & 1 deletion Kutt.NET/UsersEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class KuttApi
public async Task<UserInfo> GetUserInfoAsync()
{
var request = new RestRequest(USERS_ENDPOINT, DataFormat.Json);
var response = await Client.ExecuteGetAsync(request);
var response = await _client.ExecuteGetAsync(request);
var content = response.Content;
if (!response.IsSuccessful)
throw new KuttException
Expand Down

0 comments on commit 2eff604

Please sign in to comment.