GithubHelp home page GithubHelp logo

mmstiver / discuitsharp Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 207 KB

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.

License: GNU Lesser General Public License v3.0

C# 100.00%

discuitsharp's Introduction

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);

discuitsharp's People

Contributors

mmstiver avatar

Stargazers

FeelinLucky avatar Insidious Fiddler avatar

Watchers

 avatar

discuitsharp's Issues

Clear out warnings

For first version release, Warnings will be configured to be treated as errors. Clear out any build warnings. Likely null references, or unused variables.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.