Skip to content

Commit

Permalink
Merge pull request #719 from NemeStats/718_fix_bgg_integration
Browse files Browse the repository at this point in the history
718 fix bgg integration
  • Loading branch information
jakejgordon committed Jan 29, 2020
2 parents f7f770a + e10fbd9 commit 2a19b98
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Source/BoardGameGeekApiClient/Service/ApiDownloaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Xml.Linq;
using BoardGameGeekApiClient.Interfaces;
using Exceptionless;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;

namespace BoardGameGeekApiClient.Service
Expand All @@ -26,6 +27,7 @@ public class ApiDownloaderService : IApiDownloadService

public ApiDownloaderService()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
_retryPolicy = new RetryPolicy<BGGTransientErrorDetectionStrategy>(retryStrategy);
}
Expand All @@ -48,20 +50,31 @@ private static void ReadApiResult(Uri requestUrl, out XDocument data)
// Due to malformed header I cannot use GetContentAsync and ReadAsStringAsync :(
// UTF-8 is now hard-coded...

//wait 500ms before each read to avoid BGG block
//--wait 500ms before each read to avoid BGG block
Thread.Sleep(500);
data = null;
while (data == null)
var tryCount = 0;
while (tryCount < 2)
{
var request = WebRequest.CreateHttp(requestUrl);
request.Timeout = 10000;
//--setting this so BGG can see the request from NemeStats
request.UserAgent = "NemeStats website/2.96";
using (var response = (HttpWebResponse)(request.GetResponse()))
{
if (response.StatusCode != HttpStatusCode.OK) continue;

using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
if (response.StatusCode != HttpStatusCode.OK)
{
var exception = new Exception($"Expected a 200 OK response from BGG when calling '{requestUrl.AbsolutePath}', but got a '{response.StatusCode}' response.");
exception.ToExceptionless();
tryCount++;
}
else
{
data = XDocument.Parse(reader.ReadToEnd());
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
data = XDocument.Parse(reader.ReadToEnd());
break;
}
}
}
}
Expand Down

0 comments on commit 2a19b98

Please sign in to comment.