Skip to content

Commit

Permalink
Added fluent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaNecron committed Jun 30, 2021
1 parent 2eff604 commit 5be6a78
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
50 changes: 50 additions & 0 deletions Kutt.NET/Fluent/DomainCreationRequest.cs
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);
}
}
}
88 changes: 88 additions & 0 deletions Kutt.NET/Fluent/LinkCreationRequest.cs
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);
}
}
}
9 changes: 4 additions & 5 deletions Kutt.NET/Kutt.NET.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -10,14 +9,14 @@
<RepositoryUrl>https://github.com/AlphaNecron/Kutt.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>kutt url-shortener netstandard library</PackageTags>
<AssemblyVersion>1.0.3</AssemblyVersion>
<FileVersion>1.0.3</FileVersion>
<AssemblyVersion>1.0.4</AssemblyVersion>
<FileVersion>1.0.4</FileVersion>
<PackageIcon>kutt.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageVersion>1.0.3</PackageVersion>
<PackageVersion>1.0.4</PackageVersion>
</PropertyGroup>
<ItemGroup>
<None Include="kutt.png" Pack="true" PackagePath="$(PackageIcon)"/>
<None Include="kutt.png" Pack="true" PackagePath="$(PackageIcon)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Expand Down

0 comments on commit 5be6a78

Please sign in to comment.