Skip to content
Merged
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
604 changes: 604 additions & 0 deletions Ink Canvas/Helpers/BaseUploadQueue.cs

Large diffs are not rendered by default.

107 changes: 68 additions & 39 deletions Ink Canvas/Helpers/DlassApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ink_Canvas.Helpers
Expand Down Expand Up @@ -54,7 +55,8 @@ public DlassApiClient(string appId, string appSecret, string baseUrl = null, str
/// <summary>
/// 获取访问令牌(Access Token)
/// </summary>
public async Task<string> GetAccessTokenAsync()
/// <param name="cancellationToken">取消令牌</param>
public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default)
{
if (!string.IsNullOrEmpty(_userToken))
{
Expand All @@ -68,6 +70,8 @@ public async Task<string> GetAccessTokenAsync()

try
{
cancellationToken.ThrowIfCancellationRequested();

var requestData = new
{
app_id = _appId,
Expand All @@ -78,7 +82,7 @@ public async Task<string> GetAccessTokenAsync()
var json = JsonConvert.SerializeObject(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync("/oauth/token", content);
var response = await _httpClient.PostAsync("/oauth/token", content, cancellationToken);
var responseContent = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
Expand All @@ -93,13 +97,13 @@ public async Task<string> GetAccessTokenAsync()
throw new Exception($"获取Access Token失败: {response.StatusCode}");
}
}
catch (HttpRequestException httpEx)
catch (OperationCanceledException)
{
throw new Exception($"获取Access Token时网络错误: {httpEx.Message}", httpEx);
throw;
}
catch (TaskCanceledException timeoutEx)
catch (HttpRequestException httpEx)
{
throw new Exception("获取Access Token时请求超时", timeoutEx);
throw new Exception($"获取Access Token时网络错误: {httpEx.Message}", httpEx);
}
catch (Exception ex)
{
Expand All @@ -110,14 +114,19 @@ public async Task<string> GetAccessTokenAsync()
/// <summary>
/// 发送GET请求
/// </summary>
public async Task<T> GetAsync<T>(string endpoint, bool requireAuth = true)
/// <param name="endpoint">API端点</param>
/// <param name="requireAuth">是否需要认证</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<T> GetAsync<T>(string endpoint, bool requireAuth = true, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();

string token = null;
if (requireAuth)
{
token = await GetAccessTokenAsync();
token = await GetAccessTokenAsync(cancellationToken);
}

var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
Expand All @@ -134,7 +143,7 @@ public async Task<T> GetAsync<T>(string endpoint, bool requireAuth = true)
}
}

var response = await _httpClient.SendAsync(request);
var response = await _httpClient.SendAsync(request, cancellationToken);
var content = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
Expand All @@ -150,13 +159,13 @@ public async Task<T> GetAsync<T>(string endpoint, bool requireAuth = true)
throw new Exception($"API请求失败: {response.StatusCode} - {content}");
}
}
catch (HttpRequestException httpEx)
catch (OperationCanceledException)
{
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
throw;
}
catch (TaskCanceledException timeoutEx)
catch (HttpRequestException httpEx)
{
throw new Exception($"请求超时: {endpoint}", timeoutEx);
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
}
catch (Exception ex)
{
Expand All @@ -167,14 +176,20 @@ public async Task<T> GetAsync<T>(string endpoint, bool requireAuth = true)
/// <summary>
/// 发送POST请求
/// </summary>
public async Task<T> PostAsync<T>(string endpoint, object data = null, bool requireAuth = true)
/// <param name="endpoint">API端点</param>
/// <param name="data">请求数据</param>
/// <param name="requireAuth">是否需要认证</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<T> PostAsync<T>(string endpoint, object data = null, bool requireAuth = true, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();

string token = null;
if (requireAuth)
{
token = await GetAccessTokenAsync();
token = await GetAccessTokenAsync(cancellationToken);
}

var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
Expand All @@ -197,7 +212,7 @@ public async Task<T> PostAsync<T>(string endpoint, object data = null, bool requ
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}

var response = await _httpClient.SendAsync(request);
var response = await _httpClient.SendAsync(request, cancellationToken);
var content = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
Expand All @@ -213,13 +228,13 @@ public async Task<T> PostAsync<T>(string endpoint, object data = null, bool requ
throw new Exception($"API请求失败: {response.StatusCode} - {content}");
}
}
catch (HttpRequestException httpEx)
catch (OperationCanceledException)
{
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
throw;
}
catch (TaskCanceledException timeoutEx)
catch (HttpRequestException httpEx)
{
throw new Exception($"请求超时: {endpoint}", timeoutEx);
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
}
catch (Exception ex)
{
Expand All @@ -230,14 +245,20 @@ public async Task<T> PostAsync<T>(string endpoint, object data = null, bool requ
/// <summary>
/// 发送PUT请求
/// </summary>
public async Task<T> PutAsync<T>(string endpoint, object data = null, bool requireAuth = true)
/// <param name="endpoint">API端点</param>
/// <param name="data">请求数据</param>
/// <param name="requireAuth">是否需要认证</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<T> PutAsync<T>(string endpoint, object data = null, bool requireAuth = true, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();

string token = null;
if (requireAuth)
{
token = await GetAccessTokenAsync();
token = await GetAccessTokenAsync(cancellationToken);
}

var request = new HttpRequestMessage(HttpMethod.Put, endpoint);
Expand All @@ -261,7 +282,7 @@ public async Task<T> PutAsync<T>(string endpoint, object data = null, bool requi
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}

var response = await _httpClient.SendAsync(request);
var response = await _httpClient.SendAsync(request, cancellationToken);
var content = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
Expand All @@ -277,13 +298,13 @@ public async Task<T> PutAsync<T>(string endpoint, object data = null, bool requi
throw new Exception($"API请求失败: {response.StatusCode} - {content}");
}
}
catch (HttpRequestException httpEx)
catch (OperationCanceledException)
{
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
throw;
}
catch (TaskCanceledException timeoutEx)
catch (HttpRequestException httpEx)
{
throw new Exception($"请求超时: {endpoint}", timeoutEx);
throw new Exception($"发送请求时出错: {httpEx.Message}", httpEx);
}
catch (Exception ex)
{
Expand All @@ -294,14 +315,19 @@ public async Task<T> PutAsync<T>(string endpoint, object data = null, bool requi
/// <summary>
/// 发送DELETE请求
/// </summary>
public async Task<bool> DeleteAsync(string endpoint, bool requireAuth = true)
/// <param name="endpoint">API端点</param>
/// <param name="requireAuth">是否需要认证</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<bool> DeleteAsync(string endpoint, bool requireAuth = true, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();

string token = null;
if (requireAuth)
{
token = await GetAccessTokenAsync();
token = await GetAccessTokenAsync(cancellationToken);
}

var request = new HttpRequestMessage(HttpMethod.Delete, endpoint);
Expand All @@ -319,7 +345,7 @@ public async Task<bool> DeleteAsync(string endpoint, bool requireAuth = true)
}
}

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

if (response.IsSuccessStatusCode)
{
Expand All @@ -330,11 +356,11 @@ public async Task<bool> DeleteAsync(string endpoint, bool requireAuth = true)
return false;
}
}
catch (HttpRequestException)
catch (OperationCanceledException)
{
return false;
throw;
}
catch (TaskCanceledException)
catch (HttpRequestException)
{
return false;
}
Expand All @@ -354,10 +380,13 @@ public async Task<bool> DeleteAsync(string endpoint, bool requireAuth = true)
/// <param name="title">笔记标题(可选)</param>
/// <param name="description">笔记描述(可选)</param>
/// <param name="tags">笔记标签(可选)</param>
public async Task<T> UploadNoteAsync<T>(string endpoint, string filePath, string boardId, string secretKey, string title = null, string description = null, string tags = null)
/// <param name="cancellationToken">取消令牌</param>
public async Task<T> UploadNoteAsync<T>(string endpoint, string filePath, string boardId, string secretKey, string title = null, string description = null, string tags = null, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();

if (!File.Exists(filePath))
{
throw new FileNotFoundException($"文件不存在: {filePath}");
Expand Down Expand Up @@ -394,7 +423,7 @@ public async Task<T> UploadNoteAsync<T>(string endpoint, string filePath, string

request.Content = content;

var response = await _httpClient.SendAsync(request);
var response = await _httpClient.SendAsync(request, cancellationToken);
var responseContent = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
Expand All @@ -410,13 +439,13 @@ public async Task<T> UploadNoteAsync<T>(string endpoint, string filePath, string
throw new Exception($"上传文件失败: {response.StatusCode} - {responseContent}");
}
}
catch (HttpRequestException httpEx)
catch (OperationCanceledException)
{
throw new Exception($"上传文件时网络错误: {httpEx.Message}", httpEx);
throw;
}
catch (TaskCanceledException timeoutEx)
catch (HttpRequestException httpEx)
{
throw new Exception($"上传文件超时: {endpoint}", timeoutEx);
throw new Exception($"上传文件时网络错误: {httpEx.Message}", httpEx);
}
catch (Exception ex)
{
Expand Down
Loading
Loading