Skip to content

Fix memory leaks, support "default_background" color and "custom_emoji" page icon #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Src/Notion.Client/Models/Blocks/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public enum Color
[EnumMember(Value = "gray_background")]
GrayBackground,

[EnumMember(Value = "default_background")]
DefaultBackground,

[EnumMember(Value = "brown_background")]
BrownBackground,

Expand Down
25 changes: 25 additions & 0 deletions Src/Notion.Client/Models/CustomEmojiObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class CustomEmojiObject : IPageIcon
{
[JsonProperty("type")]
public virtual string Type { get; set; }

[JsonProperty("custom_emoji")]
public Info Emoji { get; set; }

public class Info
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("url")]
public string Url { get; set; }
}
}
}
1 change: 1 addition & 0 deletions Src/Notion.Client/Models/Page/IPageIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Notion.Client
[JsonSubtypes.KnownSubTypeAttribute(typeof(EmojiObject), "emoji")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "file")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(FileObject), "external")]
[JsonSubtypes.KnownSubTypeAttribute(typeof(CustomEmojiObject), "custom_emoji")]
public interface IPageIcon
{
[JsonProperty("type")]
Expand Down
10 changes: 5 additions & 5 deletions Src/Notion.Client/Notion.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 10 additions & 16 deletions Src/Notion.Client/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class RestClient : IRestClient
ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }
};

private HttpClient _httpClient;

public RestClient(ClientOptions options)
{
_options = MergeOptions(options);
Expand All @@ -36,7 +34,7 @@ public async Task<T> GetAsync<T>(
JsonSerializerSettings serializerSettings = null,
CancellationToken cancellationToken = default)
{
var response = await SendAsync(uri, HttpMethod.Get, queryParams, headers,
using var response = await SendAsync(uri, HttpMethod.Get, queryParams, headers,
cancellationToken: cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -56,7 +54,7 @@ void AttachContent(HttpRequestMessage httpRequest)
Encoding.UTF8, "application/json");
}

var response = await SendAsync(uri, HttpMethod.Post, queryParams, headers, AttachContent,
using var response = await SendAsync(uri, HttpMethod.Post, queryParams, headers, AttachContent,
cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -76,7 +74,7 @@ void AttachContent(HttpRequestMessage httpRequest)
httpRequest.Content = new StringContent(serializedBody, Encoding.UTF8, "application/json");
}

var response = await SendAsync(uri, new HttpMethod("PATCH"), queryParams, headers, AttachContent,
using var response = await SendAsync(uri, new HttpMethod("PATCH"), queryParams, headers, AttachContent,
cancellationToken);

return await response.ParseStreamAsync<T>(serializerSettings);
Expand All @@ -88,7 +86,7 @@ public async Task DeleteAsync(
IDictionary<string, string> headers = null,
CancellationToken cancellationToken = default)
{
await SendAsync(uri, HttpMethod.Delete, queryParams, headers, null, cancellationToken);
using var _ = await SendAsync(uri, HttpMethod.Delete, queryParams, headers, null, cancellationToken);
}

private static ClientOptions MergeOptions(ClientOptions options)
Expand Down Expand Up @@ -141,7 +139,7 @@ private async Task<HttpResponseMessage> SendAsync(
Action<HttpRequestMessage> attachContent = null,
CancellationToken cancellationToken = default)
{
EnsureHttpClient();
using var client = GetHttpClient();

requestUri = AddQueryString(requestUri, queryParams);

Expand All @@ -156,7 +154,7 @@ private async Task<HttpResponseMessage> SendAsync(

attachContent?.Invoke(httpRequest);

var response = await _httpClient.SendAsync(httpRequest, cancellationToken);
var response = await client.SendAsync(httpRequest, cancellationToken);

if (!response.IsSuccessStatusCode)
{
Expand All @@ -174,17 +172,13 @@ private static void AddHeaders(HttpRequestMessage request, IDictionary<string, s
}
}

private void EnsureHttpClient()
private HttpClient GetHttpClient()
{
if (_httpClient != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HermanSchoenfeld This will create a new instance of HttpClient for each request. HttpClient instances are meant to be long-lived, and reused throughout the lifetime of the application

{
return;
}

var pipeline = new LoggingHandler { InnerHandler = new HttpClientHandler() };

_httpClient = new HttpClient(pipeline);
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
var httpClient = new HttpClient(pipeline);
httpClient.BaseAddress = new Uri(_options.BaseUrl);
return httpClient;
}

private static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, string>> queryParams)
Expand Down
Loading