Skip to content

Commit

Permalink
修复错误地使用同步方法导致阻塞的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleFish-233 committed May 5, 2023
1 parent b301f41 commit 9e6a086
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 23 deletions.
Expand Up @@ -21,7 +21,6 @@ public class ChatGPTService:IChatGPTService
private readonly IHttpService _httpService;
private readonly IConfiguration _configuration;
private readonly List<DateTime> _record = new List<DateTime>();
private readonly HttpClient _httpClient;
private readonly ILogger<ChatGPTService> _logger;

private readonly JsonSerializerOptions _jsonOptions = new()
Expand All @@ -34,13 +33,18 @@ public ChatGPTService(IHttpService httpService, IConfiguration configuration, IL
_httpService = httpService;
_configuration = configuration;
_logger = logger;

_httpClient = _httpService.GetClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _configuration["ChatGPTApiKey"]);
}

public async Task< string > GetReply(string question)
{
HttpClient _httpClient = null;
if (_httpClient == null)
{
_httpClient =await _httpService.GetClientAsync();
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _configuration["ChatGPTApiKey"]);
}


var datetime= DateTime.Now.ToCstTime();

//检查上限
Expand Down
2 changes: 0 additions & 2 deletions CnGalWebSite/CnGalWebSite.Core/Services/IHttpService.cs
Expand Up @@ -15,7 +15,5 @@ public interface IHttpService
bool IsAuth { get; set; }

Task<HttpClient> GetClientAsync();

HttpClient GetClient();
}
}
Expand Up @@ -64,10 +64,5 @@ public async Task<HttpClient> GetClientAsync()
_client.SetBearerToken(token);
return _client;
}

public HttpClient GetClient()
{
return GetClientAsync().GetAwaiter().GetResult();
}
}
}
Expand Up @@ -100,7 +100,7 @@ public async Task<CreateVideoViewModel> GetVideoContext(RepostVideoModel item, I

OnProgressUpdate(item, OutputLevel.Infor, $"获取视频内容");
//获取视频内容
var json = await _httpService.GetClient().GetStringAsync(ToolHelper.WebApiPath + "api/thirdparties/GetBilibiliVideoInfor?id=" + model.Relevances.BilibiliId);
var json = await(await _httpService.GetClientAsync()).GetStringAsync(ToolHelper.WebApiPath + "api/thirdparties/GetBilibiliVideoInfor?id=" + model.Relevances.BilibiliId);
var data = JObject.Parse(json);

if (data["code"].ToObject<int>() != 0)
Expand Down
10 changes: 0 additions & 10 deletions CnGalWebSite/CnGalWebSite.Server/Services/HttpService.cs
Expand Up @@ -66,15 +66,5 @@ public async Task<HttpClient> GetClientAsync()
_client.SetBearerToken(token);
return _client;
}

public HttpClient GetClient()
{
var state = _authenticationStateProvider.GetAuthenticationStateAsync().GetAwaiter().GetResult();
var token = _tokenManagementService.GetUserAccessTokenAsync(state.User).GetAwaiter().GetResult();

_client.SetBearerToken(token);
return _client;

}
}
}
Expand Up @@ -68,6 +68,7 @@
"中文维基百科" => "Wiki.png",
"月幕Galgame" => "YMGal.png",
"Bilibili" => "bilibili.png",
"bilibili" => "bilibili.png",
"WikiData" => "Wikidata.png",
"微博" => "weibo.png",
"AcFun" => "AcFun.png",
Expand Down Expand Up @@ -103,7 +104,7 @@
{
return Model.DisplayName switch
{
"Bilibili" => IsVideo ? "前往 bilibili 观看" : Model.DisplayName,
"bilibili" => IsVideo ? "前往 bilibili 观看" : Model.DisplayName,
_ => Model.DisplayName
};
}
Expand Down

0 comments on commit 9e6a086

Please sign in to comment.