Skip to content

Commit

Permalink
Implement linkding API
Browse files Browse the repository at this point in the history
fixes #45
  • Loading branch information
prplecake committed Jul 6, 2023
1 parent c9adf86 commit 34b511d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .run/BookmarkSync.CLI.run.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="BookmarkSync.CLI" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/BookmarkSync.CLI/bin/Debug/net7.0/mastodon-bookmark-sync" />
<option name="EXE_PATH" value="$PROJECT_DIR$/src/BookmarkSync.CLI/bin/Debug/net7.0/mastodon-bookmark-sync" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/BookmarkSync.CLI/bin/Debug/net7.0" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/BookmarkSync.CLI/bin/Debug/net7.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<envs>
<env name="MBS_ENVIRONMENT" value="Development" />
</envs>
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/BookmarkSync.CLI/BookmarkSync.CLI.csproj" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/src/BookmarkSync.CLI/BookmarkSync.CLI.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
Expand Down
1 change: 1 addition & 0 deletions mastodon-bookmark-sync.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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/=Linkding/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pinboard/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Raindropio/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unbookmark/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using BookmarkSync.Core.Entities.Config;
using BookmarkSync.Infrastructure.Services.Mastodon;
using Microsoft.Extensions.Hosting;

namespace BookmarkSync.Infrastructure.Services.Bookmarking;

public class BookmarkSyncService : IHostedService
{
private static readonly ILogger _logger = Log.ForContext<BookmarkSyncService>();
private readonly IBookmarkingService _bookmarkingService;
private readonly IHostApplicationLifetime _host;
private readonly List<Instance>? _instances;
private readonly IBookmarkingService _bookmarkingService;
private static readonly ILogger _logger = Log.ForContext<BookmarkSyncService>();
public BookmarkSyncService(IHostApplicationLifetime host, IConfigManager configManager)
{
_bookmarkingService = BookmarkingService.GetBookmarkingService(configManager);
Expand All @@ -27,7 +28,7 @@ public async Task StartAsync(CancellationToken stoppingToken)
{
_logger.Information("Processing {Instance}", instance);
_logger.Debug("Setting up Mastodon API client");
var client = new Mastodon.ApiClient(instance);
var client = new ApiClient(instance);
// Get bookmarks from mastodon account
List<Bookmark>? bookmarks = null;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Mime;
using BookmarkSync.Core;
using BookmarkSync.Infrastructure.Services.Bookmarking.LinkAce;
using BookmarkSync.Infrastructure.Services.Bookmarking.Linkding;
using BookmarkSync.Infrastructure.Services.Bookmarking.Pinboard;

namespace BookmarkSync.Infrastructure.Services.Bookmarking;
Expand Down Expand Up @@ -32,6 +33,7 @@ public static IBookmarkingService GetBookmarkingService(IConfigManager configMan
{
"LinkAce" => new LinkAceBookmarkingService(configManager),
"Pinboard" => new PinboardBookmarkingService(configManager),
"linkding" => new LinkdingBookmarkingService(configManager),
_ => throw new InvalidOperationException("Bookmark service either not provided or unknown")
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using Newtonsoft.Json;

namespace BookmarkSync.Infrastructure.Services.Bookmarking.Linkding;

public class LinkdingBookmarkingService : BookmarkingService, IBookmarkingService
{
private static readonly ILogger _logger = Log.ForContext<LinkdingBookmarkingService>();
public LinkdingBookmarkingService(IConfigManager configManager)
{
ApiToken = configManager.App.Bookmarking.ApiToken ?? throw new InvalidOperationException("Missing API token");
string linkAceUri = configManager.GetConfigValue("App:Bookmarking:LinkdingUri") ??
throw new InvalidOperationException("Missing linkding Uri");
ApiUri = $"{linkAceUri}/api/bookmarks/";
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", ApiToken);
}
/// <inheritdoc />
public async Task<HttpResponseMessage> Save(Bookmark bookmark)
{
// Prep payload
Dictionary<string, object?> payload = new()
{
{
"url", bookmark.Uri
},
{
"title", bookmark.Content
},
{
"tag_names", bookmark.DefaultTags
},
{
"unread", false
},
{
"shared", false
}
};
var stringContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8,
MediaTypeNames.Application.Json);
var response = await Client.PostAsync(ApiUri, stringContent);
response.EnsureSuccessStatusCode();
_logger.Debug("Response status: {StatusCode}", response.StatusCode);
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BookmarkSync.Infrastructure.Services.Bookmarking.Linkding;

namespace BookmarkSync.Infrastructure.Tests.Services;

[TestClass]
Expand Down Expand Up @@ -55,6 +57,35 @@ public void GetBookmarkingService_LinkAce()
Assert.IsInstanceOfType(obj, typeof(LinkAceBookmarkingService));
}
[TestMethod]
public void GetBookmarkingService_Linkding()
{
// Arrange
var config = new Dictionary<string, string?>
{
{
"App:Bookmarking:Service", "linkding"
},
{
"App:Bookmarking:ApiToken", "3781368521fd4fae365dac100f28dbce533a4f9a"
},
{
"App:Bookmarking:LinkdingUri", "https://your-linkace-url.com"
}
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(config)
.Build();

IConfigManager configManager = new ConfigManager(configuration);

// Act
var obj = BookmarkingService.GetBookmarkingService(configManager);

// Assert
Assert.AreEqual(typeof(LinkdingBookmarkingService), obj.GetType());
Assert.IsInstanceOfType(obj, typeof(LinkdingBookmarkingService));
}
[TestMethod]
public void GetBookmarkingService_Pinboard()
{
// Arrange
Expand Down

0 comments on commit 34b511d

Please sign in to comment.