mirror of
https://github.com/ckaczor/ChrisKaczor.MinifluxClient.git
synced 2026-01-13 17:22:25 -05:00
Initial commit
This commit is contained in:
27
Library/EnumExtensions.cs
Normal file
27
Library/EnumExtensions.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
internal static class EnumExtensions
|
||||
{
|
||||
internal static TAttribute? GetAttribute<TAttribute>(this Enum enumValue) where TAttribute : Attribute
|
||||
{
|
||||
var type = enumValue.GetType();
|
||||
|
||||
var name = Enum.GetName(type, enumValue);
|
||||
|
||||
if (name == null)
|
||||
return null;
|
||||
|
||||
var field = type.GetField(name);
|
||||
|
||||
var attribute = field?.GetCustomAttribute<TAttribute>();
|
||||
|
||||
return attribute;
|
||||
}
|
||||
|
||||
extension<T>(T value) where T : Enum
|
||||
{
|
||||
internal string QueryPropertyName => value.GetAttribute<QueryParameterNameAttribute>()?.Name ?? string.Empty;
|
||||
}
|
||||
}
|
||||
12
Library/FeedEntryStatus.cs
Normal file
12
Library/FeedEntryStatus.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
public enum FeedEntryStatus
|
||||
{
|
||||
[JsonStringEnumMemberName("read")]
|
||||
Read,
|
||||
|
||||
[JsonStringEnumMemberName("unread")]
|
||||
Unread
|
||||
}
|
||||
101
Library/MinifluxClient.cs
Normal file
101
Library/MinifluxClient.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using ChrisKaczor.MinifluxClient.Models;
|
||||
using RestSharp;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
public class MinifluxClient
|
||||
{
|
||||
private readonly JsonSerializerOptions _jsonSerializerOptions;
|
||||
private readonly RestClient _restClient;
|
||||
|
||||
public MinifluxClient(string url, string apiKey)
|
||||
{
|
||||
_jsonSerializerOptions = new JsonSerializerOptions();
|
||||
|
||||
_restClient = new RestClient(url);
|
||||
_restClient.AddDefaultHeader("X-Auth-Token", apiKey);
|
||||
}
|
||||
|
||||
private async Task<T?> ExecuteRestRequest<T>(string api, Method method, IEnumerable<Parameter>? parameters, object? body)
|
||||
{
|
||||
var request = new RestRequest($"/v1/{api}", method);
|
||||
|
||||
if (parameters != null)
|
||||
{
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
request.AddParameter(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
if (body != null)
|
||||
{
|
||||
var bodyJson = JsonSerializer.Serialize(body, _jsonSerializerOptions);
|
||||
request.AddStringBody(bodyJson, DataFormat.Json);
|
||||
}
|
||||
|
||||
var response = await _restClient.ExecuteAsync(request);
|
||||
|
||||
if (response == null)
|
||||
throw new Exception("Failed to get response from API");
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception($"Error: {response.StatusCode} - {response.Content}");
|
||||
|
||||
if (response.Content == null)
|
||||
throw new Exception("Response content is null");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(response.Content))
|
||||
return default;
|
||||
|
||||
try
|
||||
{
|
||||
var responseObject = JsonSerializer.Deserialize<T>(response.Content, _jsonSerializerOptions);
|
||||
|
||||
return responseObject ?? throw new Exception($"Failed to deserialize response content - {response.Content}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Failed to deserialize response content - {response.Content}", e);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Category>> GetCategories()
|
||||
{
|
||||
var response = await ExecuteRestRequest<IEnumerable<Category>>("categories", Method.Get, null, null);
|
||||
|
||||
return response ?? [];
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Feed>> GetFeeds()
|
||||
{
|
||||
var response = await ExecuteRestRequest<IEnumerable<Feed>>("feeds", Method.Get, null, null);
|
||||
|
||||
return response ?? [];
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Entry>> GetFeedEntries(long feedId, SortField sortField, SortDirection sortDirection)
|
||||
{
|
||||
var parameters = new List<Parameter>
|
||||
{
|
||||
new QueryParameter(SortField.QueryParameterName, sortField.QueryPropertyName),
|
||||
new QueryParameter(SortDirection.QueryParameterName, sortDirection.QueryPropertyName)
|
||||
};
|
||||
|
||||
var response = await ExecuteRestRequest<EntriesResponse>($"feeds/{feedId}/entries", Method.Get, parameters, null);
|
||||
|
||||
return response?.Entries ?? [];
|
||||
}
|
||||
|
||||
public async Task MarkFeedEntries(IEnumerable<long> entryIds, FeedEntryStatus status)
|
||||
{
|
||||
var updateEntryRequest = new Requests.UpdateEntryRequest
|
||||
{
|
||||
EntryIds = entryIds,
|
||||
Status = status
|
||||
};
|
||||
|
||||
await ExecuteRestRequest<object>("entries", Method.Put, null, updateEntryRequest);
|
||||
}
|
||||
}
|
||||
34
Library/MinifluxClient.csproj
Normal file
34
Library/MinifluxClient.csproj
Normal file
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>ChrisKaczor.MinifluxClient</RootNamespace>
|
||||
<Title>ChrisKaczor.MinifluxClient</Title>
|
||||
<Authors>Chris Kaczor</Authors>
|
||||
<Product>ChrisKaczor.MinifluxClient</Product>
|
||||
<RepositoryUrl>https://github.com/ckaczor/ChrisKaczor.MinifluxClient</RepositoryUrl>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<Description>This library can be used to read RSS feeds via the Miniflux API.</Description>
|
||||
<PackageId>ChrisKaczor.MinifluxClient</PackageId>
|
||||
<AssemblyName>ChrisKaczor.MinifluxClient</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="MinifluxClient.sln.DotSettings" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
|
||||
<PackageReference Include="RestSharp" Version="112.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="README.md">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath>\</PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
3
Library/MinifluxClient.sln.DotSettings
Normal file
3
Library/MinifluxClient.sln.DotSettings
Normal file
@@ -0,0 +1,3 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kaczor/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Miniflux/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
20
Library/Models/Category.cs
Normal file
20
Library/Models/Category.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class Category
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public required long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; set; }
|
||||
|
||||
[JsonPropertyName("user_id")]
|
||||
public required long UserId { get; set; }
|
||||
|
||||
[JsonPropertyName("hide_globally")]
|
||||
public required bool HideGlobally { get; set; }
|
||||
}
|
||||
26
Library/Models/Enclosure.cs
Normal file
26
Library/Models/Enclosure.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class Enclosure
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public required long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("user_id")]
|
||||
public required long UserId { get; set; }
|
||||
|
||||
[JsonPropertyName("entry_id")]
|
||||
public required long EntryId { get; set; }
|
||||
|
||||
[JsonPropertyName("url")]
|
||||
public required string Url { get; set; }
|
||||
|
||||
[JsonPropertyName("mime_type")]
|
||||
public required string MimeType { get; set; }
|
||||
|
||||
[JsonPropertyName("size")]
|
||||
public required long Size { get; set; }
|
||||
}
|
||||
14
Library/Models/EntriesResponse.cs
Normal file
14
Library/Models/EntriesResponse.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class EntriesResponse
|
||||
{
|
||||
[JsonPropertyName("total")]
|
||||
public required int Total { get; set; }
|
||||
|
||||
[JsonPropertyName("entries")]
|
||||
public required List<Entry> Entries { get; set; }
|
||||
}
|
||||
65
Library/Models/Entry.cs
Normal file
65
Library/Models/Entry.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class Entry
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public required long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("user_id")]
|
||||
public required long UserId { get; set; }
|
||||
|
||||
[JsonPropertyName("feed_id")]
|
||||
public required long FeedId { get; set; }
|
||||
|
||||
[JsonPropertyName("status")]
|
||||
public required string Status { get; set; }
|
||||
|
||||
[JsonPropertyName("hash")]
|
||||
public required string Hash { get; set; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; set; }
|
||||
|
||||
[JsonPropertyName("url")]
|
||||
public required string Url { get; set; }
|
||||
|
||||
[JsonPropertyName("comments_url")]
|
||||
public required string CommentsUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("published_at")]
|
||||
public required DateTimeOffset PublishedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("created_at")]
|
||||
public required DateTimeOffset CreatedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("changed_at")]
|
||||
public required DateTimeOffset ChangedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("content")]
|
||||
public required string Content { get; set; }
|
||||
|
||||
[JsonPropertyName("author")]
|
||||
public required string Author { get; set; }
|
||||
|
||||
[JsonPropertyName("share_code")]
|
||||
public required string ShareCode { get; set; }
|
||||
|
||||
[JsonPropertyName("starred")]
|
||||
public required bool Starred { get; set; }
|
||||
|
||||
[JsonPropertyName("reading_time")]
|
||||
public required int ReadingTime { get; set; }
|
||||
|
||||
[JsonPropertyName("enclosures")]
|
||||
public required List<Enclosure> Enclosures { get; set; }
|
||||
|
||||
[JsonPropertyName("feed")]
|
||||
public required Feed Feed { get; set; }
|
||||
|
||||
[JsonPropertyName("tags")]
|
||||
public required List<string> Tags { get; set; }
|
||||
}
|
||||
131
Library/Models/Feed.cs
Normal file
131
Library/Models/Feed.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class Feed
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public required long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("user_id")]
|
||||
public required long UserId { get; set; }
|
||||
|
||||
[JsonPropertyName("feed_url")]
|
||||
public required string FeedUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("site_url")]
|
||||
public required string SiteUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; set; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public required string Description { get; set; }
|
||||
|
||||
[JsonPropertyName("checked_at")]
|
||||
public required DateTimeOffset CheckedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("next_check_at")]
|
||||
public required DateTimeOffset NextCheckAt { get; set; }
|
||||
|
||||
[JsonPropertyName("etag_header")]
|
||||
public required string EtagHeader { get; set; }
|
||||
|
||||
[JsonPropertyName("last_modified_header")]
|
||||
public required string LastModifiedHeader { get; set; }
|
||||
|
||||
[JsonPropertyName("parsing_error_message")]
|
||||
public required string ParsingErrorMessage { get; set; }
|
||||
|
||||
[JsonPropertyName("parsing_error_count")]
|
||||
public required int ParsingErrorCount { get; set; }
|
||||
|
||||
[JsonPropertyName("scraper_rules")]
|
||||
public required string ScraperRules { get; set; }
|
||||
|
||||
[JsonPropertyName("rewrite_rules")]
|
||||
public required string RewriteRules { get; set; }
|
||||
|
||||
[JsonPropertyName("blocklist_rules")]
|
||||
public required string BlocklistRules { get; set; }
|
||||
|
||||
[JsonPropertyName("keeplist_rules")]
|
||||
public required string KeeplistRules { get; set; }
|
||||
|
||||
[JsonPropertyName("block_filter_entry_rules")]
|
||||
public required string BlockFilterEntryRules { get; set; }
|
||||
|
||||
[JsonPropertyName("keep_filter_entry_rules")]
|
||||
public required string KeepFilterEntryRules { get; set; }
|
||||
|
||||
[JsonPropertyName("urlrewrite_rules")]
|
||||
public required string UrlrewriteRules { get; set; }
|
||||
|
||||
[JsonPropertyName("user_agent")]
|
||||
public required string UserAgent { get; set; }
|
||||
|
||||
[JsonPropertyName("cookie")]
|
||||
public required string Cookie { get; set; }
|
||||
|
||||
[JsonPropertyName("username")]
|
||||
public required string Username { get; set; }
|
||||
|
||||
[JsonPropertyName("password")]
|
||||
public required string Password { get; set; }
|
||||
|
||||
[JsonPropertyName("disabled")]
|
||||
public required bool Disabled { get; set; }
|
||||
|
||||
[JsonPropertyName("no_media_player")]
|
||||
public required bool NoMediaPlayer { get; set; }
|
||||
|
||||
[JsonPropertyName("ignore_http_cache")]
|
||||
public required bool IgnoreHttpCache { get; set; }
|
||||
|
||||
[JsonPropertyName("allow_self_signed_certificates")]
|
||||
public required bool AllowSelfSignedCertificates { get; set; }
|
||||
|
||||
[JsonPropertyName("fetch_via_proxy")]
|
||||
public required bool FetchViaProxy { get; set; }
|
||||
|
||||
[JsonPropertyName("hide_globally")]
|
||||
public required bool HideGlobally { get; set; }
|
||||
|
||||
[JsonPropertyName("disable_http2")]
|
||||
public required bool DisableHttp2 { get; set; }
|
||||
|
||||
[JsonPropertyName("pushover_enabled")]
|
||||
public required bool PushoverEnabled { get; set; }
|
||||
|
||||
[JsonPropertyName("ntfy_enabled")]
|
||||
public required bool NtfyEnabled { get; set; }
|
||||
|
||||
[JsonPropertyName("crawler")]
|
||||
public required bool Crawler { get; set; }
|
||||
|
||||
[JsonPropertyName("apprise_service_urls")]
|
||||
public required string AppriseServiceUrls { get; set; }
|
||||
|
||||
[JsonPropertyName("webhook_url")]
|
||||
public required string WebhookUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("ntfy_priority")]
|
||||
public required int NtfyPriority { get; set; }
|
||||
|
||||
[JsonPropertyName("ntfy_topic")]
|
||||
public required string NtfyTopic { get; set; }
|
||||
|
||||
[JsonPropertyName("pushover_priority")]
|
||||
public required int PushoverPriority { get; set; }
|
||||
|
||||
[JsonPropertyName("proxy_url")]
|
||||
public required string ProxyUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("category")]
|
||||
public required Category Category { get; set; }
|
||||
|
||||
[JsonPropertyName("icon")]
|
||||
public required Icon Icon { get; set; }
|
||||
}
|
||||
17
Library/Models/Icon.cs
Normal file
17
Library/Models/Icon.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Models;
|
||||
|
||||
[PublicAPI]
|
||||
public class Icon
|
||||
{
|
||||
[JsonPropertyName("feed_id")]
|
||||
public required long FeedId { get; set; }
|
||||
|
||||
[JsonPropertyName("icon_id")]
|
||||
public required long IconId { get; set; }
|
||||
|
||||
[JsonPropertyName("external_icon_id")]
|
||||
public required string ExternalIconId { get; set; }
|
||||
}
|
||||
7
Library/QueryParameterNameAttribute.cs
Normal file
7
Library/QueryParameterNameAttribute.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
|
||||
internal class QueryParameterNameAttribute(string name) : Attribute
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
}
|
||||
13
Library/Requests/UpdateEntryRequest.cs
Normal file
13
Library/Requests/UpdateEntryRequest.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient.Requests;
|
||||
|
||||
public class UpdateEntryRequest
|
||||
{
|
||||
[JsonPropertyName("entry_ids")]
|
||||
public required IEnumerable<long> EntryIds { get; set; }
|
||||
|
||||
[JsonPropertyName("status")]
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public required FeedEntryStatus Status { get; set; }
|
||||
}
|
||||
21
Library/SortDirection.cs
Normal file
21
Library/SortDirection.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
[QueryParameterName("direction")]
|
||||
public enum SortDirection
|
||||
{
|
||||
[QueryParameterName("asc")]
|
||||
Ascending,
|
||||
|
||||
[QueryParameterName("desc")]
|
||||
Descending
|
||||
}
|
||||
|
||||
internal static class SortDirectionExtensions
|
||||
{
|
||||
extension(SortDirection)
|
||||
{
|
||||
internal static string QueryParameterName => typeof(SortDirection).GetCustomAttribute<QueryParameterNameAttribute>()?.Name ?? throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
30
Library/SortField.cs
Normal file
30
Library/SortField.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace ChrisKaczor.MinifluxClient;
|
||||
|
||||
[QueryParameterName("order")]
|
||||
public enum SortField
|
||||
{
|
||||
[QueryParameterName("id")]
|
||||
Id,
|
||||
|
||||
[QueryParameterName("status")]
|
||||
Status,
|
||||
|
||||
[QueryParameterName("published_at")]
|
||||
PublishedAt,
|
||||
|
||||
[QueryParameterName("category_title")]
|
||||
CategoryTitle,
|
||||
|
||||
[QueryParameterName("category_id")]
|
||||
CategoryId
|
||||
}
|
||||
|
||||
internal static class SortFieldExtensions
|
||||
{
|
||||
extension(SortField)
|
||||
{
|
||||
internal static string QueryParameterName => typeof(SortField).GetCustomAttribute<QueryParameterNameAttribute>()?.Name ?? throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user