Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NinjaBotCore.Modules.Wow | ||
{ | ||
/// <summary> | ||
/// Used to send throttled HTTP requests to https://api.rocketleaguestats.com. | ||
/// Automatically detects your rate limit configuration from the headers. | ||
/// </summary> | ||
internal class ApiRequesterThrottle : WclApiRequestor | ||
{ | ||
private readonly Semaphore _queue; | ||
|
||
private int _rateLimitRemaining; | ||
|
||
private DateTime _rateLimitResetRemaining; | ||
|
||
public ApiRequesterThrottle(string apiKey) : base(apiKey) | ||
{ | ||
_queue = new Semaphore(1, 1); | ||
_rateLimitRemaining = 1; | ||
_rateLimitResetRemaining = DateTime.UtcNow.AddSeconds(1); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) | ||
{ | ||
try | ||
{ | ||
_queue.WaitOne(); | ||
|
||
if (_rateLimitRemaining == 0) | ||
{ | ||
var startTime = DateTime.UtcNow; | ||
//var difference = _rateLimitResetRemaining - startTime; | ||
//System.Console.WriteLine($"dif: {difference}"); | ||
|
||
await Task.Delay(1000); | ||
|
||
_rateLimitRemaining = 1; | ||
_rateLimitResetRemaining.AddSeconds(1); | ||
} | ||
|
||
var response = await base.SendAsync(request); | ||
_rateLimitRemaining -= 1; | ||
_rateLimitResetRemaining = _rateLimitResetRemaining.AddSeconds(-1); | ||
//System.Console.WriteLine(_rateLimitRemaining); | ||
return response; | ||
} | ||
finally | ||
{ | ||
_queue.Release(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Net; | ||
using Newtonsoft.Json; | ||
using NinjaBotCore.Models.Wow; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using NinjaBotCore.Database; | ||
using Discord; | ||
using Discord.Net; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace NinjaBotCore.Modules.Wow | ||
{ | ||
public class WclApiRequestor : IDisposable | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public WclApiRequestor(string apiKey) | ||
{ | ||
_client = new HttpClient | ||
{ | ||
BaseAddress = new Uri("https://www.warcraftlogs.com:443/v1/"), | ||
}; | ||
_client.DefaultRequestHeaders | ||
.Accept | ||
.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
} | ||
|
||
public async Task<T> Get<T>(string relativeUrl) | ||
{ | ||
|
||
System.Console.WriteLine($"{relativeUrl}"); | ||
using (var request = new HttpRequestMessage(HttpMethod.Get, relativeUrl)) | ||
using (var response = await SendAsync(request)) | ||
{ | ||
var result = await response.Content.ReadAsStringAsync(); | ||
|
||
return JsonConvert.DeserializeObject<T>(result); | ||
} | ||
} | ||
|
||
protected virtual async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) | ||
{ | ||
var response = await _client.SendAsync(request); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
return response; | ||
} | ||
|
||
try | ||
{ | ||
var errorMessage = await response.Content.ReadAsStringAsync(); | ||
|
||
if (string.IsNullOrEmpty(errorMessage)) | ||
{ | ||
throw new Exception("No message!"); | ||
} else { | ||
throw new Exception($"{errorMessage}"); | ||
} | ||
|
||
} | ||
catch (JsonException e) | ||
{ | ||
throw new Exception($"{e.Message}"); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_client?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters