Skip to content

Commit

Permalink
feat: Support for the Drafts workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Mar 28, 2024
1 parent 0dc99a8 commit 9e0c042
Show file tree
Hide file tree
Showing 14 changed files with 1,066 additions and 60 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Microsoft.Extensions.* Ver -->
<DotNet8Version>8.0.0</DotNet8Version>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Ben.Demystifier" Version="0.4.1" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.7" />
Expand All @@ -21,6 +21,7 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="$(DotNet8Version)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="$(DotNet8Version)" />
<PackageVersion Include="SlugGenerator" Version="2.0.2" />
<PackageVersion Include="System.Net.Http.Json" Version="$(DotNet8Version)" />
<PackageVersion Include="System.Text.Json" Version="$(DotNet8Version)" />
<PackageVersion Include="xunit" Version="2.6.5" />
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,26 @@
A .NET SDK built for the Issuu v2 API

[![.github/workflows/main.yml](https://github.com/IngeniumSE/IssuuSDK/actions/workflows/main.yml/badge.svg)](https://github.com/IngeniumSE/IssuuSDK/actions/workflows/main.yml) [![.github/workflows/release.yml](https://github.com/IngeniumSE/IssuuSDK/actions/workflows/release.yml/badge.svg)](https://github.com/IngeniumSE/IssuuSDK/actions/workflows/release.yml)

## Installation

The SDK is available as a NuGet package. You can install it using the following command:

```
dotnet add package IssuuSDK
```

## Usage

### .NET Core & .NET 5+
TBC

### .NET Framework

TBC

## Open Source

This SDK is open source and is available under the MIT license. Feel free to contribute to the project by submitting pull requests or issues.

- SlugGenerator by Artem Polishchuk - https://github.com/polischuk/SlugGenerator
4 changes: 4 additions & 0 deletions apps/IssuuSDK.ConsoleSample/IssuuSDK.ConsoleSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<Content Include="appsettings*.json" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\libs\IssuuSDK\IssuuSDK.csproj" />
</ItemGroup>

</Project>
212 changes: 212 additions & 0 deletions libs/IssuuSDK/Api/DraftOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

using SlugGenerator;

namespace IssuuSDK.Api;

partial interface IIssuuApiClient
{
/// <summary>
/// Gets the /draft operations.
/// </summary>
public IDraftOperations Drafts { get; }
}

partial class IssuuApiClient
{
Lazy<IDraftOperations>? _drafts;
public IDraftOperations Drafts => (_drafts ??= Defer<IDraftOperations>(
c => new DraftOperations(new("/drafts"), c))).Value;
}

/// <summary>
/// Providers operations for the /drafts endpoint.
/// </summary>
public partial interface IDraftOperations
{
/// <summary>
/// Creates a draft.
/// HTTP POST /drafts
/// </summary>
/// <param name="draft">The draft document.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<Document>> CreateDraftAsync(
Draft draft,
CancellationToken cancellationToken = default);

/// <summary>
/// Delete the draft with the given slug.
/// HTTP DELETE /drafts/{slug}
/// </summary>
/// <param name="slug">The document slug.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse> DeleteDraftAsync(
string slug,
CancellationToken cancellationToken = default);

/// <summary>
/// Gets the draft with the given slug.
/// HTTP GET /drafts/{slug}
/// </summary>
/// <param name="slug">The document slug.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<Document>> GetDraftAsync(
string slug,
CancellationToken cancellationToken = default);

/// <summary>
/// Gets the drafts.
/// HTTP GET /drafts
/// </summary>
/// <param name="page">The current page to fetch.</param>
/// <param name="size">The size of page to fetch.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<Document[]>> GetDraftsAsync(
int? page = null,
int? size = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Publishes the draft with the given slug.
/// </summary>
/// <param name="slug">The document slug.</param>
/// <param name="desiredName">The desired name for the public document.</param>
/// <param name="cancellationToken"></param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<PublishResult>> PublishDraftAsync(
string slug,
string? desiredName = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Updates a draft.
/// HTTP PATCH /drafts/{slug}
/// </summary>
/// <param name="slug">The document slug.</param>
/// <param name="draft">The draft document.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<Document>> UpdateDraftAsync(
string slug,
Draft draft,
CancellationToken cancellationToken = default);

/// <summary>
/// Uploads file content to attach to a draft.
/// HTTP PATH /drafts/{slug}/upload
/// </summary>
/// <param name="slug">The document slug.</param>
/// <param name="filePath">The file path.</param>
/// <param name="confirmCopyright">Has confirmation of copyright be applied?</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The Issuu response.</returns>
Task<IssuuResponse<Document>> UploadDocumentContentAsync(
string slug,
string filePath,
bool confirmCopyright = false,
CancellationToken cancellationToken = default);
}

public class DraftOperations(PathString path, ApiClient client) : IDraftOperations
{
public async Task<IssuuResponse<Document>> CreateDraftAsync(
Draft draft,
CancellationToken cancellationToken = default)
{
var request = new IssuuRequest<Draft>(HttpMethod.Post, path, draft);

return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken);
}

public async Task<IssuuResponse> DeleteDraftAsync(
string slug,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(slug, nameof(slug));

var request = new IssuuRequest(HttpMethod.Delete, path + $"/{slug}");

return await client.SendAsync(request, cancellationToken);
}

public async Task<IssuuResponse<Document>> GetDraftAsync(
string slug,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(slug, nameof(slug));

var request = new IssuuRequest(HttpMethod.Get, path + $"/{slug}");

return await client.FetchSingleAsync<Document>(request, cancellationToken);
}

public async Task<IssuuResponse<Document[]>> GetDraftsAsync(
int? page = null,
int? size = null,
CancellationToken cancellationToken = default)
{
var request = new IssuuRequest(HttpMethod.Get, path, page: page, size: size);

return await client.FetchManyAsync<Document[]>(request, cancellationToken);
}

public async Task<IssuuResponse<PublishResult>> PublishDraftAsync(
string slug,
string? desiredName = null,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(slug, nameof(slug));

if (desiredName is { Length: > 0})
{
desiredName = desiredName.GenerateSlug();
}

var request = new IssuuRequest<PublishRequest>(
HttpMethod.Post,
path + $"/{slug}/publish",
new PublishRequest
{
DesiredName = desiredName
});

return await client.FetchSingleAsync<PublishRequest, PublishResult>(request, cancellationToken);
}

public async Task<IssuuResponse<Document>> UpdateDraftAsync(
string slug,
Draft draft,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(slug, nameof(slug));

var request = new IssuuRequest<Draft>(new HttpMethod("PATCH"), path + $"/{slug}", draft);

return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken);
}

public async Task<IssuuResponse<Document>> UploadDocumentContentAsync(
string slug,
string filePath,
bool confirmCopyright = false,
CancellationToken cancellationToken = default)
{
Ensure.IsNotNullOrEmpty(slug, nameof(slug));

var request = new IssuuRequest(
new HttpMethod("PATCH"), path + $"/{slug}/upload",
useMultipartContent: true,
formData: new()
{
["confirmCopyright"] = confirmCopyright ? "true" : "false"
},
filePath: filePath);

return await client.FetchSingleAsync<Document>(request, cancellationToken);
}
}
15 changes: 15 additions & 0 deletions libs/IssuuSDK/Api/IssuuApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

namespace IssuuSDK.Api;

public partial interface IIssuuApiClient
{

}

public partial class IssuuApiClient : ApiClient, IIssuuApiClient
{
public IssuuApiClient(HttpClient http, IssuuSettings settings)
: base(http, settings, settings.BaseUrl) { }
}
Loading

0 comments on commit 9e0c042

Please sign in to comment.