Skip to content

Commit

Permalink
Use RestSharp for log posting instead of HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Plenyx committed Jun 30, 2024
1 parent c25e68d commit e684c4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
19 changes: 8 additions & 11 deletions Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using PlenBotLogUploader.GitHub;
using PlenBotLogUploader.Gw2Api;
using PlenBotLogUploader.Tools;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -73,6 +74,7 @@ internal bool UpdateFound
private readonly List<string> allSessionLogs = [];
private readonly Regex songSmartCommandRegex = songRegex();
private readonly Regex buildSmartCommandRegex = buildRegex();
private readonly RestClient logPoster;
private SemaphoreSlim semaphore;
private TwitchChatClient chatConnect;
private readonly ArcLogsChangeObserver watcher;
Expand Down Expand Up @@ -141,6 +143,7 @@ internal FormMain()
toolTip.SetToolTip(checkBoxAutoUpdate, "Automatically downloads the newest version when it is available.\nOnly occurs during the start of the application.");
toolTip.SetToolTip(twitchCommandsLink.checkBoxSongEnable, "If checked, the given command will output current song from Spotify to Twitch chat.");
#endregion
logPoster = new RestClient();
try
{
Size = ApplicationSettings.Current.MainFormSize;
Expand Down Expand Up @@ -740,21 +743,16 @@ internal async Task SendLogToTwitchChatAsync(DpsReportJson reportJSON, bool bypa

internal async Task HttpUploadLogAsync(string file, Dictionary<string, string> postData, bool bypassMessage = false)
{
using var content = new MultipartFormDataContent();
foreach (var key in postData.Keys)
{
content.Add(new StringContent(postData[key]), key);
}
AddToText($">:> Uploading {Path.GetFileName(file)}");
var request = new RestRequest(CreateDPSReportLink());
request.AddBody(postData);
var bossId = 1;
try
{
using var inputStream = File.OpenRead(file);
using var contentStream = new StreamContent(inputStream);
content.Add(contentStream, "file", Path.GetFileName(file));
request.AddFile("file", file);
try
{
using var responseMessage = await HttpClientController.PostAsync(CreateDPSReportLink(), content);
var responseMessage = await logPoster.PostAsync(request);
if (!responseMessage.IsSuccessStatusCode)
{
var statusCode = (int)responseMessage.StatusCode;
Expand Down Expand Up @@ -786,10 +784,9 @@ internal async Task HttpUploadLogAsync(string file, Dictionary<string, string> p
AddToText($">:> Unable to upload file {Path.GetFileName(file)}, dps.report responded with an non-ok status code ({(int)responseMessage.StatusCode}).");
return;
}
var response = await responseMessage.Content.ReadAsStringAsync();
try
{
var reportJson = JsonConvert.DeserializeObject<DpsReportJson>(response);
var reportJson = JsonConvert.DeserializeObject<DpsReportJson>(responseMessage.Content);
if (!string.IsNullOrEmpty(reportJson.Error))
{
AddToText($">:> Error processing file {Path.GetFileName(file)}, dps.report responded with following error message: {reportJson.Error}");
Expand Down
3 changes: 2 additions & 1 deletion PlenBotLogUploader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
<PackageReference Include="HttpClientController" Version="1.0.2" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="111.3.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="TextTableFormatter.NET" Version="2.0.1" />
<PackageReference Include="TwitchIrcClient" Version="1.2.2" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="8.0.5" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<SupportedPlatform Include="windows" />
Expand Down

0 comments on commit e684c4c

Please sign in to comment.