Patreon 0.0.3

dotnet add package Patreon --version 0.0.3                
NuGet\Install-Package Patreon -Version 0.0.3                
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="Patreon" Version="0.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Patreon --version 0.0.3                
#r "nuget: Patreon, 0.0.3"                
#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.
// Install Patreon as a Cake Addin
#addin nuget:?package=Patreon&version=0.0.3

// Install Patreon as a Cake Tool
#tool nuget:?package=Patreon&version=0.0.3                

Non-Official C# Patreon Client

Quick Start

// create client for testing
var patreonClient = new Patreon(new(), new AccessTokenOnly("personal_access_token"));

// build request - save for later
var request = PatreonRequestBuilder.Identity(b => b.SelectFields());

// execute request
var response = await patreonClient.GetAsync(request);

HttpClient for Production

Patreon doesn't allow client credentials flow and there are no API keys, so the client comes with token management in mind.

Implementation Example

public class EFCorePatreonTokens : PatreonTokens, IPatreonTokens
{
    public EFCorePatreonTokens(
        HttpClient client,
        PatreonClientConfig config,
        MyDbContext ctx
    ) : base(client, config)
    {
        _ctx = ctx;
    }
    
    protected override Task<Tokens> GetTokens() {
        // your reading tokens logic
    }

    protected override Task SaveTokensAsync(Tokens response)
    {
        // your saving token logic
    }
}

var patreon = new Patreon(new(), new EFCorePatreonTokens(...));

Example with Marten

public class PatreonTokensInMarten : PatreonTokens
{
    private readonly Settings _settings;
    private readonly IDocumentSession _session;

    public PatreonTokensInMarten(
        HttpClient client,
        PatreonClientConfig config,
        Settings settings,
        IDocumentSession session
    ) : base(client, config)
    {
        _settings = settings;
        _session = session;
    }

    // todo: replace me with GetUserId in current scope service
    private const string PersonalTokens = nameof(PersonalTokens);

    private Tokens? Tokens { get; set; }

    public override async Task<Tokens> GetTokens()
    {
        // try load from db
        if (Tokens == null)
        {
            Tokens = await _session.LoadAsync<TokensDoc>(PersonalTokens);
        }

        // try load from appsettings
        if (Tokens == null)
        {
            Tokens = new TokensDoc()
            {
                AccessToken = _settings.PatreonAccessToken,
                RefreshToken = _settings.PatreonRefreshToken,
            };
        }

        return Tokens;
    }

    protected override async Task SaveTokensAsync(Tokens response)
    {
        var doc = new TokensDoc()
        {
            Id = PersonalTokens,
            AccessToken = response.AccessToken,
            RefreshToken = response.RefreshToken,
            ExpiresIn = response.ExpiresIn,
            Scope = response.Scope,
            TokenType = response.TokenType,
        };
        Tokens = doc;
        _session.Store(doc);
        await _session.SaveChangesAsync();
    }
}

public class TokensDoc : Tokens
{
    public string Id { get; set; }
}

How to use

Requests

// get identity
PatreonRequestBuilder.Identity(builder => ...);
// get campaign by id
PatreonRequestBuilder.Campaign(builder => ...);
// get campaigns
PatreonRequestBuilder.Campaigns(builder => ...);
// get member by id
PatreonRequestBuilder.Member(builder => ...);
// get campaign members by campaign id
PatreonRequestBuilder.CampaignMembers(builder => ...);
// get post by id
PatreonRequestBuilder.Post(builder => ...);
// get posts by campaign id
PatreonRequestBuilder.CampaignPosts(builder => ...);

Parameters

var request = PatreonRequestBuilder.CampaignMembers(b => b.SelectFields());

var response = await patreonClient.GetAsync(request.For("campaign_id"));

Selecting Specific Fields

var request = PatreonRequestBuilder.CampaignMembers(
    b => b.SelectFields(x => new
    {
        x.FullName,
        x.LastChargeDate,
        x.LifetimeSupportCents
    })
);
var campaignMembersRequest = PatreonRequestBuilder.CampaignMembers(
    b => b.SelectFields()
          .Include(x => x.Tiers)
);
var campaignMembersRequest = PatreonRequestBuilder.CampaignMembers(
    b => b.SelectFields()
          .Include(x => x.Tiers, x => new
          {
              x.Title
          })
);
var campaignMembersRequest = PatreonRequestBuilder.CampaignMembers(
    b => b.SelectFields()
          .Include(x => x.Tiers, x => new
          {
              x.Title
          })
          .ThenInclude(x => x.Benefits)
);
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 is compatible.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.0.3 551 5/20/2023
0.0.2 245 2/14/2023
0.0.1 316 9/11/2022