Skip to content

Commit

Permalink
brought some refactoring from wingman branch
Browse files Browse the repository at this point in the history
  • Loading branch information
EliphasNUIT committed Aug 16, 2023
1 parent d3acb2f commit 1e1763e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 82 deletions.
105 changes: 33 additions & 72 deletions GW2EIDPSReport/DPSReportController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Http;
Expand All @@ -13,6 +12,9 @@
[assembly: System.CLSCompliant(false)]
namespace GW2EIDPSReport
{
/// <summary>
/// https://dps.report/api
/// </summary>
public static class DPSReportController
{

Expand All @@ -26,6 +28,8 @@ private static bool IsUserTokenValid(string userToken)
NamingStrategy = new CamelCaseNamingStrategy()
};

private static readonly HttpClient HTTPClient = new HttpClient();

private class DPSReportUserTokenResponse
{
public string UserToken { get; set; }
Expand Down Expand Up @@ -93,7 +97,25 @@ private static string GetGetUploadsURL(GetUploadsParameters parameters, string u
///////////////// APIs
public static DPSReportUploadObject UploadUsingEI(FileInfo fi, List<string> traces, string userToken, bool anonymous = false, bool detailedWvW = false)
{
return UploadToDPSR(fi, GetUploadContentURL(BaseUploadContentURL, userToken, anonymous, detailedWvW) + "&generator=ei", traces);
string fileName = fi.Name;
byte[] fileContents = File.ReadAllBytes(fi.FullName);
Func<HttpContent> contentCreator = () =>
{
var multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
var byteArrayContent = new ByteArrayContent(fileContents);
byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
multiPartContent.Add(byteArrayContent, "file", fileName);
return multiPartContent;
};

DPSReportUploadObject response = GetDPSReportResponse<DPSReportUploadObject>("UploadUsingEI", GetUploadContentURL(BaseUploadContentURL, userToken, anonymous, detailedWvW) + " & generator=ei", traces, contentCreator);
if (response != null && response.Error != null)
{
traces.Add("DPSReport: UploadUsingEI failed - " + response.Error);
return null;
}
return response;

}

public static DPSReportGetUploadsObject GetUploads(List<string> traces, string userToken, GetUploadsParameters parameters)
Expand Down Expand Up @@ -143,78 +165,23 @@ public static T GetJsonWithPermalink<T>(string permalink, List<string> traces)
return GetDPSReportResponse<T>("GetJsonWithPermalink", BaseGetJsonURL + "permalink=" + permalink, traces);
}
///////////////// Response Utilities
private static T GetDPSReportResponse<T>(string requestName, string URI, List<string> traces)
private static T GetDPSReportResponse<T>(string requestName, string URI, List<string> traces, Func<HttpContent> content = null)
{
const int tentatives = 5;
for (int i = 0; i < tentatives; i++)
{
traces.Add(requestName + " tentative");
traces.Add("DPSReport: " + requestName + " tentative");
var webService = new Uri(@URI);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
requestMessage.Headers.ExpectContinue = false;

var httpClient = new HttpClient();
try
{
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
HttpResponseMessage httpResponse = httpRequest.Result;
HttpStatusCode statusCode = httpResponse.StatusCode;
HttpContent responseContent = httpResponse.Content;

if (statusCode != HttpStatusCode.OK)
{
throw new HttpRequestException(statusCode.ToString());
}

if (responseContent != null)
{
Task<string> stringContentsTask = responseContent.ReadAsStringAsync();
string stringContents = stringContentsTask.Result;
T item = JsonConvert.DeserializeObject<T>(stringContents, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = DefaultJsonContractResolver,
StringEscapeHandling = StringEscapeHandling.EscapeHtml
});
traces.Add(requestName + " tentative successful");
return item;
}
}
catch (Exception e)
{
traces.Add(requestName + " tentative failed: " + e.Message);
}
finally
if (content != null)
{
httpClient.Dispose();
requestMessage.Dispose();
requestMessage.Content = content();
}
}
return default;
}
private static DPSReportUploadObject UploadToDPSR(FileInfo fi, string URI, List<string> traces)
{
string fileName = fi.Name;
byte[] fileContents = File.ReadAllBytes(fi.FullName);
const int tentatives = 5;
for (int i = 0; i < tentatives; i++)
{
traces.Add("Upload tentative");
var webService = new Uri(@URI);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
requestMessage.Headers.ExpectContinue = false;

var multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
var byteArrayContent = new ByteArrayContent(fileContents);
byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
multiPartContent.Add(byteArrayContent, "file", fileName);
//multiPartContent.Add(new StringContent("generator=ei"), "gen", "ei");
requestMessage.Content = multiPartContent;

var httpClient = new HttpClient();
try
{
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
Task<HttpResponseMessage> httpRequest = HTTPClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
HttpResponseMessage httpResponse = httpRequest.Result;
HttpStatusCode statusCode = httpResponse.StatusCode;
HttpContent responseContent = httpResponse.Content;
Expand All @@ -228,32 +195,26 @@ private static DPSReportUploadObject UploadToDPSR(FileInfo fi, string URI, List<
{
Task<string> stringContentsTask = responseContent.ReadAsStringAsync();
string stringContents = stringContentsTask.Result;
DPSReportUploadObject item = JsonConvert.DeserializeObject<DPSReportUploadObject>(stringContents, new JsonSerializerSettings
T item = JsonConvert.DeserializeObject<T>(stringContents, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = DefaultJsonContractResolver,
StringEscapeHandling = StringEscapeHandling.EscapeHtml
});
if (item.Error != null)
{
throw new InvalidOperationException(item.Error);
}
traces.Add("Upload tentative successful");
traces.Add("DPSReport: " + requestName + " tentative successful");
return item;
}
}
catch (Exception e)
{
traces.Add("Upload tentative failed: " + e.Message);
traces.Add("DPSReport: " + requestName + " tentative failed - " + e.Message);
}
finally
{
byteArrayContent.Dispose();
httpClient.Dispose();
requestMessage.Dispose();
}
}
return null;
return default;
}

}
Expand Down
4 changes: 2 additions & 2 deletions GW2EIDPSReport/GW2EIDPSReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<Authors>EliphasNUIT</Authors>
<Company>GW2 Elite Insights</Company>
<Product>GW2 Elite Insights</Product>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions GW2EIDiscord/GW2EIDiscord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<Version>3.1.1</Version>
<Version>3.2.0</Version>
<Product>GW2 Elite Insights</Product>
<Company>GW2 Elite Insights</Company>
<Authors>EliphasNUIT</Authors>
Expand All @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Webhook" Version="3.7.2" />
<PackageReference Include="Discord.Net.Webhook" Version="3.11.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions GW2EIGW2API/GW2EIGW2API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>GW2EIGW2API</RootNamespace>
<Version>2.2.2</Version>
<Version>2.3.0</Version>
<Configurations>Debug;Release;NoRewards</Configurations>
</PropertyGroup>

Expand All @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions GW2EIParser.tst/GW2EIParser.tst.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
</PropertyGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GW2EIBuilders\GW2EIBuilders.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion GW2EIParser.tst/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class TestHelper
NamingStrategy = new CamelCaseNamingStrategy()
};
private static readonly Version Version = new Version(1, 0);
private static readonly EvtcParserSettings parserSettings = new EvtcParserSettings(false, true, true, true, true, 2200, true);
private static readonly EvtcParserSettings parserSettings = new EvtcParserSettings(false, false, true, true, true, 2200, true);
private static readonly HTMLSettings htmlSettings = new HTMLSettings(false, false);
private static readonly RawFormatSettings rawSettings = new RawFormatSettings(true);
private static readonly CSVSettings csvSettings = new CSVSettings(",");
Expand Down

0 comments on commit 1e1763e

Please sign in to comment.