-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to .net7, waiting for .net8 to fix dotnet/runtime#68895
- Loading branch information
Showing
43 changed files
with
754 additions
and
614 deletions.
There are no files selected for viewing
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
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
12 changes: 6 additions & 6 deletions
12
NotionSharp.ApiClient.Tests/NotionSharp.ApiClient.Tests.csproj
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
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 |
---|---|---|
@@ -1,107 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Flurl.Http; | ||
using Flurl.Http.Configuration; | ||
using FluentRest.Http; | ||
using FluentRest.Http.Configuration; | ||
using Polly; | ||
using Polly.Retry; | ||
|
||
namespace NotionSharp.ApiClient.Lib | ||
{ | ||
/// <summary> | ||
/// Flurl depends on newtonsoft json. Use System.Text.Json instead. | ||
/// </summary> | ||
internal class TextJsonSerializer : ISerializer | ||
{ | ||
private readonly JsonSerializerOptions? options; | ||
|
||
public TextJsonSerializer(JsonSerializerOptions? options = null) => this.options = options; | ||
public T Deserialize<T>(string s) => JsonSerializer.Deserialize<T>(s, options)!; | ||
public string Serialize(object obj) => JsonSerializer.Serialize(obj, options); | ||
namespace NotionSharp.ApiClient.Lib; | ||
|
||
public T Deserialize<T>(Stream stream) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
return Deserialize<T>(reader.ReadToEnd()); | ||
} | ||
} | ||
|
||
internal class JsonLowerCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
public override string ConvertName(string name) => name.ToLowerInvariant(); | ||
} | ||
internal class JsonLowerCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
public override string ConvertName(string name) => name.ToLowerInvariant(); | ||
} | ||
|
||
public class HttpNotionSession | ||
{ | ||
private readonly FlurlClient flurlClient; | ||
public class HttpNotionSession | ||
{ | ||
private readonly FluentRestClient flurlClient; | ||
|
||
public static readonly JsonSerializerOptions NotionJsonSerializationOptions = new JsonSerializerOptions() | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, | ||
PropertyNameCaseInsensitive = true, | ||
PropertyNamingPolicy = new JsonLowerCaseNamingPolicy() | ||
}; | ||
public static JsonSerializerOptions NotionJsonSerializationOptions { get; } = new (JsonSerializerDefaults.Web) | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
PropertyNameCaseInsensitive = true, | ||
PropertyNamingPolicy = new JsonLowerCaseNamingPolicy() | ||
}; | ||
|
||
public HttpNotionSession(Action<FlurlClient> configure) | ||
public HttpNotionSession(Action<FluentRestClient> configure) | ||
{ | ||
flurlClient = new (new HttpClient(new PolicyHandler())) | ||
{ | ||
flurlClient = new FlurlClient(new HttpClient(new PolicyHandler())) | ||
{ | ||
Settings = new ClientFlurlHttpSettings { | ||
JsonSerializer = new TextJsonSerializer(NotionJsonSerializationOptions), | ||
Settings = new () { | ||
JsonSerializer = new SystemTextJsonSerializer(NotionJsonSerializationOptions), | ||
#if DEBUG | ||
BeforeCall = call => | ||
{ | ||
var request = call.Request; | ||
var requestBody = call.RequestBody; | ||
var i = 0; | ||
}, | ||
OnError = call => | ||
{ | ||
var exception = call.Exception; | ||
var response = call.Response; | ||
var i = 0; | ||
}, | ||
AfterCall = call => | ||
{ | ||
var exception = call.Exception; | ||
var response = call.Response; | ||
//var tt = response.ResponseMessage.Content.ReadAsStringAsync(); | ||
var i = 0; | ||
} | ||
BeforeCall = call => | ||
{ | ||
var request = call.Request; | ||
var requestBody = call.RequestBody; | ||
var i = 0; | ||
}, | ||
OnError = call => | ||
{ | ||
var exception = call.Exception; | ||
var response = call.Response; | ||
var i = 0; | ||
}, | ||
AfterCall = call => | ||
{ | ||
var exception = call.Exception; | ||
var response = call.Response; | ||
//var tt = response.ResponseMessage.Content.ReadAsStringAsync(); | ||
var i = 0; | ||
} | ||
#endif | ||
}, | ||
}; | ||
configure(flurlClient); | ||
} | ||
|
||
public IFlurlRequest CreateRequest(Uri uri) | ||
=> new FlurlRequest(uri) { Client = flurlClient }; | ||
}, | ||
}; | ||
configure(flurlClient); | ||
} | ||
|
||
public IFlurlRequest CreateRequest(string uri) | ||
=> new FlurlRequest(uri) { Client = flurlClient }; | ||
public IFluentRestRequest CreateRequest(Uri uri) | ||
=> new FluentRestRequest(uri) { Client = flurlClient }; | ||
|
||
class PolicyHandler : DelegatingHandler | ||
{ | ||
private readonly AsyncRetryPolicy<HttpResponseMessage> retryPolicy; | ||
public IFluentRestRequest CreateRequest(string uri) | ||
=> new FluentRestRequest(uri) { Client = flurlClient }; | ||
|
||
public PolicyHandler() | ||
{ | ||
InnerHandler = new HttpClientHandler(); | ||
class PolicyHandler : DelegatingHandler | ||
{ | ||
private readonly AsyncRetryPolicy<HttpResponseMessage> retryPolicy; | ||
|
||
//retry on 502 | ||
retryPolicy = Policy | ||
.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.BadGateway) | ||
.WaitAndRetryAsync(5, retry => TimeSpan.FromSeconds(0.3)); | ||
} | ||
public PolicyHandler() | ||
{ | ||
InnerHandler = new HttpClientHandler(); | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
=> retryPolicy.ExecuteAsync(ct => base.SendAsync(request, ct), cancellationToken); | ||
//retry on 502 | ||
retryPolicy = Policy | ||
.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.BadGateway) | ||
.WaitAndRetryAsync(5, retry => TimeSpan.FromSeconds(0.3)); | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
=> retryPolicy.ExecuteAsync(ct => base.SendAsync(request, ct), cancellationToken); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
using System.Collections.Generic; | ||
using Flurl.Http; | ||
using FluentRest.Http; | ||
using NotionSharp.ApiClient.Lib; | ||
using NotionSharp.ApiClient.Lib.Model; | ||
|
||
namespace NotionSharp.ApiClient | ||
namespace NotionSharp.ApiClient; | ||
|
||
/// <summary> | ||
/// Official integration | ||
/// </summary> | ||
public class NotionSession | ||
{ | ||
/// <summary> | ||
/// Official integration | ||
/// </summary> | ||
public class NotionSession | ||
public NotionSessionInfo SessionInfo { get; } | ||
public HttpNotionSession HttpSession { get; } | ||
|
||
public NotionSession(NotionSessionInfo sessionInfo) | ||
{ | ||
public NotionSessionInfo SessionInfo { get; } | ||
public HttpNotionSession HttpSession { get; } | ||
SessionInfo = sessionInfo; | ||
|
||
public NotionSession(NotionSessionInfo sessionInfo) | ||
HttpSession = new (client => | ||
{ | ||
SessionInfo = sessionInfo; | ||
|
||
HttpSession = new HttpNotionSession(client => | ||
client.WithHeaders(new Dictionary<string, string> | ||
{ | ||
client.WithHeaders(new Dictionary<string, string> | ||
{ | ||
{"Authorization", $"Bearer {sessionInfo.Token}"}, | ||
{"Notion-Version", Constants.NotionApiVersion}, //Required | ||
//{"User-Agent", Constants.UserAgent }, | ||
}); | ||
{"Authorization", $"Bearer {sessionInfo.Token}"}, | ||
{"Notion-Version", Constants.NotionApiVersion}, //Required | ||
//{"User-Agent", Constants.UserAgent }, | ||
}); | ||
} | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.