DiscuitSharp 0.1.0-rc2

This is a prerelease version of DiscuitSharp.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package DiscuitSharp --version 0.1.0-rc2
                    
NuGet\Install-Package DiscuitSharp -Version 0.1.0-rc2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DiscuitSharp" Version="0.1.0-rc2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DiscuitSharp" Version="0.1.0-rc2" />
                    
Directory.Packages.props
<PackageReference Include="DiscuitSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DiscuitSharp --version 0.1.0-rc2
                    
#r "nuget: DiscuitSharp, 0.1.0-rc2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DiscuitSharp@0.1.0-rc2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DiscuitSharp&version=0.1.0-rc2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DiscuitSharp&version=0.1.0-rc2&prerelease
                    
Install as a Cake Tool

DiscuitSharp

Discuit Sharp (D#) is a .NET Standard library that provides easy access to the Discuit API with no low-level network or HTTP config required.

Buy me a coffee PayPal Donate License: LGPL v3

Overview

Installation

Add the following package to your project using NuGet:

dotnet add package DiscuitSharp

Before you can use DiscuitClient, you need to set up an HttpClient with a cookie storage container, and initialize the session:

var handler = new HttpClientHandler() { CookieContainer = new CookieContainer() };
var httpClient = new HttpClient(handler)
{
    BaseAddress = new Uri("https://discuit.net/api/")
};
var discClient = new DiscuitClient(httpClient);
_ = discClient.GetInitial();

Note: It is important to manage the Discuit client as you would an HttpClient, since it is a wrapper. Otherwise, you may be at risk for socket exhaustion and DNS changes not being respected. Reuse the Discuit Client Instances and configure with an HttpClientFactory.

Usage

User Authentication and Management

Log in a user using their credentials, necessary for any actions that require an authenticated DiscuitUser context.

var credentials = new Credentials("mmstiver", "password123");
var user = await client.Authenticate(credentials);

Invalidate the current user session, effectively logging the user out of the application.

await client.InvalidateAuth();

Additional Actions:

-GetAuthenticatedUser(): Retrieves details about the currently authenticated user. -GetUser(string UserName): Fetches information about a specific user by their username, useful for viewing profiles.

Communities

Search for communities with a query string or Set ( All|Default|Subscribed), Re

var communitiesByName = await client.GetCommunities("Discuit");
var communities = await client.GetCommunities(QueryParams.Subscribed);

Or get a Community by Id

//Id for 'general'
CommunityId Id = new CommunityId("176ef2e09e28701c14c0c148");
Community? community = await client.Get(Id);

Posts

Fetching Posts with Specific Feed and Sorting

Fetch posts from a specific feed, such as 'Featured' or 'New', and apply sorting criteria like 'Recent' or 'Popular'..

var posts = await client.GetPosts(feed: Feed.Featured, sort: Sort.Recent, limit: 10);

foreach (var post in posts.Items)
{
    Console.WriteLine($"{post.Title} - {post.Content}");
}

Fetching Posts with Pagination

To fetch posts in a paginated manner, you can pass a CursorIndex to the GetPosts method. This cursor typically represents a pointer to a specific place in the list of posts, allowing you to retrieve the next set of posts from that point. Here is how you can use it:

// Assuming 'lastCursor' is retrieved from the previous fetch operation
var lastCursor = new CursorIndex("cursor_value_here");
var nextPosts = await client.GetPosts(cursor: lastCursor);

foreach (var post in nextPosts.Items)
{
    Console.WriteLine($"{post.Title} - {post.Content}");
}

Fetching Posts by Id

// posts for general
CommunityId Id = new CommunityId("176ef2e09e28701c14c0c148");
var post = await GetPosts(Id);

Create, Update and Delete accept a Post model (TextPost | LinkPost | ImagePost). Changes to a post does not have to be tracked, as each model will track it's own changes. Any modifications will be persisted when the model is passed into the Update.

TextPost newPost = new("First Post", "general", "Hi Mom!");
newPost = await client.Create(newPost);
newPost!.Title = "updated title"
newPost = await client.Update(newPost);
newPost = await client.Delete(newPost, deleteContent: true);

Comments

Comment has the same actions as Posts:

PublicId publicId = new PublicPostId("d3viYYpm")
CommentId parentId = new CommentId("54135hfg43")
newComment =  await client.Create(publicId, parentId, newComment);
newComment!.Body = "updated Comment"
newComment = await client.Update(publicId, newComment.Id);
_ = await client.Delete(publicId, newComment.Id);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-rc3 93 6/4/2024
0.1.0-rc2 74 6/4/2024

Initial release of DiscuitSharp.