From f9192bcff24dcdaf0635b19ce39968fd84e32732 Mon Sep 17 00:00:00 2001 From: Matthew Jorgensen Date: Fri, 4 Oct 2024 14:30:17 -0500 Subject: [PATCH 1/3] Create new object CreateLinkRequest to correctly handle the Tag type situation --- src/LinkAce.NET/Entites/CreateLinkRequest.cs | 83 ++++++++++++++++++++ src/LinkAce.NET/LinkAceClient.cs | 11 +-- 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/LinkAce.NET/Entites/CreateLinkRequest.cs diff --git a/src/LinkAce.NET/Entites/CreateLinkRequest.cs b/src/LinkAce.NET/Entites/CreateLinkRequest.cs new file mode 100644 index 0000000..fb24461 --- /dev/null +++ b/src/LinkAce.NET/Entites/CreateLinkRequest.cs @@ -0,0 +1,83 @@ +using JetBrains.Annotations; + +namespace LinkAce.NET.Entites; + +/// +/// Represents a link entity with various properties such as URL, title, description, and status. +/// +[PublicAPI] +public class CreateLinkRequest +{ + /// + /// Gets or sets a value indicating whether the link check is disabled. + /// + public bool CheckDisabled { get; set; } + /// + /// Gets or sets the date and time when the link was created. + /// + public DateTime CreatedAt { get; set; } + /// + /// Gets or sets the date and time when the link was deleted, if applicable. + /// + public DateTime? DeletedAt { get; set; } + /// + /// Gets or sets the description of the link. + /// + public string? Description { get; set; } + /// + /// Gets or sets the icon associated with the link. + /// + public string? Icon { get; set; } + /// + /// Gets or sets the unique identifier for the link. + /// + public int Id { get; set; } + /// + /// Gets or sets a value indicating whether the link is private. + /// + public bool IsPrivate { get; set; } + /// + /// Gets or sets the status of the link. + /// + public int Status { get; set; } + /// + /// Gets or sets the tags associated with the link. + /// + public string[]? Tags { get; set; } + /// + /// Gets or sets the title of the link. + /// + public string? Title { get; set; } + /// + /// Gets or sets the date and time when the link was last updated. + /// + public DateTime UpdatedAt { get; set; } + /// + /// Gets or sets the URL of the link. + /// + public string Url { get; set; } + /// + /// Gets or sets the user identifier associated with the link. + /// + public int UserId { get; set; } + + public static implicit operator CreateLinkRequest(Link link) + { + return new CreateLinkRequest + { + CheckDisabled = link.CheckDisabled, + CreatedAt = link.CreatedAt, + DeletedAt = link.DeletedAt, + Description = link.Description, + Icon = link.Icon, + Id = link.Id, + IsPrivate = link.IsPrivate, + Status = link.Status, + Tags = link.Tags?.Select(tag => tag.Name).ToArray(), + Title = link.Title, + UpdatedAt = link.UpdatedAt, + Url = link.Url, + UserId = link.UserId + }; + } +} diff --git a/src/LinkAce.NET/LinkAceClient.cs b/src/LinkAce.NET/LinkAceClient.cs index 4a9473c..3852a59 100644 --- a/src/LinkAce.NET/LinkAceClient.cs +++ b/src/LinkAce.NET/LinkAceClient.cs @@ -46,13 +46,14 @@ public LinkAceClient(string linkAceUrl, string apiToken, HttpClient? client = nu /// /// Creates a new link. /// - /// The link to create. + /// The request to create a link. /// The HTTP response message. - public async Task CreateLink(Link link) + public async Task CreateLink(CreateLinkRequest createLinkRequest) { - var response = await _client.PostAsync($"{_apiUrl}/links", - new StringContent(JsonConvert.SerializeObject(link), Encoding.UTF8, - MediaTypeNames.Application.Json)); + var json = JsonConvert.SerializeObject(createLinkRequest, _serializationSettings); + var payload = new StringContent(json, Encoding.UTF8, + MediaTypeNames.Application.Json); + var response = await _client.PostAsync($"{_apiUrl}/links", payload); return response; } /// From dd9be1a6af6b44b6911761eec0582cf17102dfeb Mon Sep 17 00:00:00 2001 From: Matthew Jorgensen Date: Fri, 4 Oct 2024 15:15:56 -0500 Subject: [PATCH 2/3] Target .NET 8 --- src/LinkAce.NET/LinkAce.NET.csproj | 2 +- src/TestApp/TestApp.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LinkAce.NET/LinkAce.NET.csproj b/src/LinkAce.NET/LinkAce.NET.csproj index 48c25b5..a64470b 100644 --- a/src/LinkAce.NET/LinkAce.NET.csproj +++ b/src/LinkAce.NET/LinkAce.NET.csproj @@ -1,7 +1,7 @@ - net7.0;net8.0 + net8.0 enable enable 0.0.3 diff --git a/src/TestApp/TestApp.csproj b/src/TestApp/TestApp.csproj index 84b7392..2f5f7ed 100644 --- a/src/TestApp/TestApp.csproj +++ b/src/TestApp/TestApp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable enable False From 4fb3b41f186d34bfcbfc107af4a4be7df665915b Mon Sep 17 00:00:00 2001 From: Matthew Jorgensen Date: Fri, 4 Oct 2024 15:17:08 -0500 Subject: [PATCH 3/3] Set serialization options --- src/LinkAce.NET/LinkAceClient.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/LinkAce.NET/LinkAceClient.cs b/src/LinkAce.NET/LinkAceClient.cs index fd466a7..956a534 100644 --- a/src/LinkAce.NET/LinkAceClient.cs +++ b/src/LinkAce.NET/LinkAceClient.cs @@ -17,6 +17,9 @@ public class LinkAceClient { private static string? _apiUrl; private static HttpClient _client; + private JsonSerializerOptions _serializationOptions = new(JsonSerializerDefaults.Web){ + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower + }; /// /// Initializes a new instance of the class with the specified URL and HTTP client. /// @@ -51,7 +54,7 @@ public LinkAceClient(string linkAceUrl, string apiToken, HttpClient? client = nu public async Task CreateLink(Link link) { var response = await _client.PostAsync($"{_apiUrl}/links", - new StringContent(JsonSerializer.Serialize(link), Encoding.UTF8, + new StringContent(JsonSerializer.Serialize(link, _serializationOptions), Encoding.UTF8, MediaTypeNames.Application.Json)); return response; } @@ -80,7 +83,7 @@ public LinkAceClient(string linkAceUrl, string apiToken, HttpClient? client = nu public async Task UpdateLinkById(int id, Link link) { var response = await _client.PatchAsync($"{_apiUrl}/links/{id}", - new StringContent(JsonSerializer.Serialize(link), Encoding.UTF8, + new StringContent(JsonSerializer.Serialize(link, _serializationOptions), Encoding.UTF8, MediaTypeNames.Application.Json)); return response; }