-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from prplecake/19-add-support-for-linkace-api
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
.idea/.idea.mastodon-bookmark-sync/.idea/inspectionProfiles/Project_Default.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using BookmarkSync.Core.Utilities; | ||
|
||
namespace BookmarkSync.Core.Tests.Utilities; | ||
|
||
[TestClass] | ||
public class UriUtilitiesTests | ||
{ | ||
[DataTestMethod] | ||
[DataRow("https://example.com")] | ||
[DataRow("https://example.com/")] | ||
public void UriUtilities_HttpsProto(string uri) | ||
{ | ||
string actual = uri.RemoveProto(); | ||
Assert.AreEqual("example.com", actual); | ||
} | ||
[DataTestMethod] | ||
[DataRow("http://example.com")] | ||
[DataRow("http://example.com/")] | ||
public void UriUtilities_HttpProto(string uri) | ||
{ | ||
string actual = uri.RemoveProto(); | ||
Assert.AreEqual("example.com", actual); | ||
} | ||
[DataTestMethod] | ||
[DataRow("example.com")] | ||
[DataRow("example.com/")] | ||
public void UriUtilities_NoProto(string uri) | ||
{ | ||
string actual = uri.RemoveProto(); | ||
Assert.AreEqual("example.com", actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Serilog; | ||
|
||
namespace BookmarkSync.Core.Utilities; | ||
|
||
public static class UriUtilities | ||
{ | ||
private static readonly ILogger _logger = Log.ForContext(typeof(UriUtilities)); | ||
/// <summary> | ||
/// Returns a URI without a (/https?/) protocol. | ||
/// </summary> | ||
/// <param name="uri">The URI to process.</param> | ||
/// <returns>A URI without a protocol or trailing slash.</returns> | ||
public static string RemoveProto(this string uri) | ||
{ | ||
_logger.Debug("Running {Method} for {Uri}", "RemoveProto", uri); | ||
// https://stackoverflow.com/questions/10306690/what-is-a-regular-expression-which-will-match-a-valid-domain-name-without-a-subd/26987741#26987741 | ||
const string pattern = | ||
@"(?:https?:\/\/)?((((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]{1,1}\.)*(xn--)?([a-z0-9][a-z0-9\-]{0,60}|[a-z0-9-]{1,30}\.[a-z]{2,}))"; | ||
var m = Regex.Match(uri, pattern, RegexOptions.IgnoreCase); | ||
if (m.Success) return m.Groups[1].Value; | ||
_logger.Error("Could not process {Uri}", uri); | ||
throw new ArgumentException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
BookmarkSync.Infrastructure/Services/Bookmarking/LinkAce/LinkAceBookmarkingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using BookmarkSync.Core.Configuration; | ||
using BookmarkSync.Core.Entities; | ||
using BookmarkSync.Core.Interfaces; | ||
using BookmarkSync.Core.Utilities; | ||
using Newtonsoft.Json; | ||
using Serilog; | ||
|
||
namespace BookmarkSync.Infrastructure.Services.Bookmarking.LinkAce; | ||
|
||
public class LinkAceBookmarkingService : BookmarkingService, IBookmarkingService | ||
{ | ||
private static readonly ILogger _logger = Log.ForContext<LinkAceBookmarkingService>(); | ||
public LinkAceBookmarkingService(IConfigManager configManager) | ||
{ | ||
ApiToken = configManager.App.Bookmarking.ApiToken ?? throw new InvalidOperationException("Missing API token"); | ||
string linkAceUri = configManager.GetConfigValue("App:Bookmarking:LinkAceUri") ?? | ||
throw new InvalidOperationException("Missing LinkAce Uri"); | ||
ApiUri = $"{linkAceUri}/api/v1/links"; | ||
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiToken); | ||
} | ||
/// <inheritdoc /> | ||
public async Task<HttpResponseMessage> Save(Bookmark bookmark) | ||
{ | ||
// Prep payload | ||
Dictionary<string, object> payload = new() | ||
{ | ||
{ | ||
"url", bookmark.Uri | ||
}, | ||
{ | ||
"title", bookmark.Content | ||
}, | ||
{ | ||
"tags", bookmark.DefaultTags | ||
}, | ||
{ | ||
"is_private", true | ||
}, | ||
{ | ||
"check_disabled", true | ||
} | ||
}; | ||
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; | ||
} | ||
} |