Skip to content

Commit

Permalink
Use LinkAce.NET packages
Browse files Browse the repository at this point in the history
  • Loading branch information
prplecake committed Nov 4, 2023
1 parent 6c95c2f commit 655550b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 71 deletions.
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
- name: Build
run: |
dotnet nuget add source --username prplecake --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/CompostInTraining/index.json"
dotnet nuget add source --username prplecake --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name prplecake "https://nuget.pkg.github.com/prplecake/index.json"
dotnet build
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- name: Restore dependencies
run: |
dotnet nuget add source --username prplecake --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/CompostInTraining/index.json"
dotnet nuget add source --username prplecake --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name prplecake "https://nuget.pkg.github.com/prplecake/index.json"
dotnet restore
- name: Build
run: dotnet build --no-restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<ItemGroup>
<PackageReference Include="cit.common" Version="0.0.1" />
<PackageReference Include="LinkAce" Version="0.0.4" />
<PackageReference Include="LinkAce.Api" Version="0.0.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.0.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,51 @@
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text;
using System.Web;
using BookmarkSync.Core.Json;
using Newtonsoft.Json;
using LinkAce.Api;
using LinkAce.Entites;

namespace BookmarkSync.Infrastructure.Services.Bookmarking.LinkAce;

public class LinkAceBookmarkingService : BookmarkingService, IBookmarkingService
{
private static readonly ILogger _logger = Log.ForContext<LinkAceBookmarkingService>();
private readonly string _linkAceUri;
private readonly LinkAceClient _linkAceClient;
public LinkAceBookmarkingService(IConfigManager configManager)
{
ApiToken = configManager.App.Bookmarking.ApiToken ?? throw new InvalidOperationException("Missing API token");
_linkAceUri = configManager.GetConfigValue("App:Bookmarking:LinkAceUri") ??
throw new InvalidOperationException("Missing LinkAce Uri");
ApiUri = $"{_linkAceUri}/api/v1/links";
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiToken);
_linkAceClient = new LinkAceClient(_linkAceUri, Client);
}
/// <inheritdoc/>
public async Task<HttpResponseMessage> Save(Bookmark bookmark)
{
// Prep payload
Dictionary<string, object?> payload = new()
Link payload = new()
{
{
"url", bookmark.Uri
},
{
"title", bookmark.Content
},
{
"tags", bookmark.DefaultTags
},
{
"is_private", true
},
{
"check_disabled", true
}
Url = bookmark.Uri,
Title = bookmark.Content,
IsPrivate = true,
CheckDisabled = true,
Tags = bookmark.DefaultTags
};
var stringContent = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8,
MediaTypeNames.Application.Json);

// Check for existing bookmarks with the same URL.
var uriBuilder = new UriBuilder($"{_linkAceUri}/api/v1/search/links");
var query = HttpUtility.ParseQueryString(string.Empty);
query["query"] = bookmark.Uri;
uriBuilder.Query = query.ToString();
var linksResponse = await Client.GetAsync(uriBuilder.ToString());
linksResponse.EnsureSuccessStatusCode();
string responseContent = await linksResponse.Content.ReadAsStringAsync();
var responseObj = JsonConvert.DeserializeObject<LinkAceApiSearchResponse>(responseContent,
new JsonSerializerSettings
{
ContractResolver = SnakeCaseContractResolver.Instance
});

HttpResponseMessage? response;
var existingLink = responseObj?.Data?.Where(b => b.Url == bookmark.Uri).FirstOrDefault();
var existingLinks = await _linkAceClient.SearchLinksByUrl(bookmark.Uri);
var existingLink = existingLinks?.Data?.Where(b => b.Url == bookmark.Uri).FirstOrDefault();
if (existingLink != null)
{
// Bookmark exists in LinkAce, try to update.
_logger.Information("Bookmark {Uri} exists in LinkAce, updating...", bookmark.Uri);
response = await Client.PatchAsync($"{ApiUri}/{existingLink.Id}", stringContent);
response = await _linkAceClient.UpdateLinkById(existingLink.Id, payload);
response.EnsureSuccessStatusCode();
_logger.Debug("Response status: {StatusCode}", response.StatusCode);
return response;
}

response = await Client.PostAsync(ApiUri, stringContent);
response = await _linkAceClient.CreateLink(payload);
response.EnsureSuccessStatusCode();
_logger.Debug("Response status: {StatusCode}", response.StatusCode);
return response;
Expand Down

This file was deleted.

0 comments on commit 655550b

Please sign in to comment.