Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requester and RateLimitRequester refactor #481

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
43 changes: 29 additions & 14 deletions RiotSharp.AspNetCore/IServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using RiotSharp.Interfaces;
using System;
using System.Collections.Generic;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace RiotSharp.AspNetCore
{
Expand All @@ -25,18 +27,27 @@ public static IServiceCollection AddRiotSharp(this IServiceCollection serviceCol
if (riotSharpOptions.TournamentApi.ApiKey == null && riotSharpOptions.RiotApi.ApiKey == null)
throw new ArgumentNullException("No api key provided.", innerException: null);

var serializer = new RequestContentSerializer();
var deserializer = new ResponseDeserializer();
var httpClient = new HttpClient();
var failedRequestHandler = new FailedRequestHandler();
var client = new RequestClient(httpClient, failedRequestHandler);

if (riotSharpOptions.RiotApi.ApiKey != null)
{
var rateLimitedRequester = new RateLimitedRequester(riotSharpOptions.RiotApi.ApiKey,
riotSharpOptions.RiotApi.RateLimits);
serviceCollection.AddSingleton<ITournamentRiotApi>(serviceProvider =>
new TournamentRiotApi(rateLimitedRequester));
serviceCollection.AddSingleton<IRiotApi>(serviceProvider => new RiotApi(rateLimitedRequester));
var requestCreator = new RequestCreator(riotSharpOptions.RiotApi.ApiKey, serializer);
var basicRequester = new Requester(client, requestCreator, deserializer);
var riotRateLimitProvider = new RateLimitProvider(riotSharpOptions.RiotApi.RateLimits);


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we remove the double empty lines in this file

var riotRateLimitedRequester = new RateLimitedRequester(basicRequester, riotRateLimitProvider);

serviceCollection.AddSingleton<IRiotApi>(provider => new RiotApi(riotRateLimitedRequester));

var staticApiRequester = new RateLimitedRequester(riotSharpOptions.RiotApi.ApiKey, new Dictionary<TimeSpan, int>
{
{ new TimeSpan(1, 0, 0), 10 }
});

var staticRateLimitProvider = new RateLimitProvider(new Dictionary<TimeSpan, int>{{new TimeSpan(1, 0, 0), 10}});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you limit the lines to 120 chars?


var staticRateLimtedRequester = new RateLimitedRequester(basicRequester, staticRateLimitProvider);

if (riotSharpOptions.RiotApi.UseMemoryCache)
{
Expand All @@ -49,15 +60,19 @@ public static IServiceCollection AddRiotSharp(this IServiceCollection serviceCol
serviceCollection.AddSingleton<ICache, PassThroughCache>();

serviceCollection.AddSingleton<IStaticRiotApi>(serviceProvider =>
new StaticRiotApi(staticApiRequester, serviceProvider.GetRequiredService<ICache>(), riotSharpOptions.RiotApi.SlidingExpirationTime));
new StaticRiotApi(staticRateLimtedRequester, serviceProvider.GetRequiredService<ICache>(), riotSharpOptions.RiotApi.SlidingExpirationTime));
}

if (riotSharpOptions.TournamentApi.ApiKey != null)
{
var rateLimitedRequester = new RateLimitedRequester(riotSharpOptions.TournamentApi.ApiKey,
riotSharpOptions.TournamentApi.RateLimits);
serviceCollection.AddSingleton<ITournamentRiotApi>(serviceProvider =>
new TournamentRiotApi(rateLimitedRequester, riotSharpOptions.TournamentApi.UseStub));
var requestCreator = new RequestCreator(riotSharpOptions.TournamentApi.ApiKey, serializer);
var basicRequester = new Requester(client, requestCreator, deserializer);
var riotRateLimitProvider = new RateLimitProvider(riotSharpOptions.TournamentApi.RateLimits);


var tournamentRateLimitedRequester = new RateLimitedRequester(basicRequester, riotRateLimitProvider);

serviceCollection.AddSingleton<ITournamentRiotApi>(serviceProvider => new TournamentRiotApi(tournamentRateLimitedRequester, riotSharpOptions.TournamentApi.UseStub));
}

return serviceCollection;
Expand Down
33 changes: 33 additions & 0 deletions RiotSharp.Test/RateLimitedRequesterTestDoubles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using RiotSharp.Http;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Test
{
class RateLimitedRequesterTestDoubles
{
public static IRateLimitedRequester Real(string apiKey, IDictionary<TimeSpan, int> rateLimits)
{
var serializer = new RequestContentSerializer();
var deserializer = new ResponseDeserializer();
var requestCreator = new RequestCreator(apiKey, serializer);
var httpClient = new HttpClient();
var failedRequestHandler = new FailedRequestHandler();
var client = new RequestClient(httpClient, failedRequestHandler);
var basicRequester = new Requester(client, requestCreator, deserializer);
var rateLimitProvider = new RateLimitProvider(rateLimits);

return new RateLimitedRequester(basicRequester, rateLimitProvider);
}

public static IRateLimitedRequester Real(string apiKey)
{
var rateLimits = new Dictionary<TimeSpan, int> { { new TimeSpan(1, 0, 0), 10 } };

return Real(apiKey, rateLimits);
}
}
}
26 changes: 26 additions & 0 deletions RiotSharp.Test/RequesterTestDoubles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using RiotSharp.Http;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Test
{
public class RequesterTestDoubles
{
public static IRequester Real(string apiKey)
{
var serializer = new RequestContentSerializer();
var deserializer = new ResponseDeserializer();
var requestCreator = new RequestCreator(apiKey, serializer);

var httpClient = new HttpClient();
var failedRequestHandler = new FailedRequestHandler();

var client = new RequestClient(httpClient, failedRequestHandler);

return new Requester(client, requestCreator, deserializer);
}
}
}
6 changes: 2 additions & 4 deletions RiotSharp.Test/StaticRiotApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
using RiotSharp.StaticDataEndpoint.Champion;
using RiotSharp.Http;
using System;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Test
{
[TestClass]
public class StaticRiotApiTest : CommonTestBase
{
private StaticRiotApi api;
private static RateLimitedRequester requester = new RateLimitedRequester(apiKey, new Dictionary<TimeSpan, int>
{
{ new TimeSpan(1, 0, 0), 10 }
});
private static IRateLimitedRequester requester = RateLimitedRequesterTestDoubles.Real(apiKey);

public StaticRiotApiTest()
{
Expand Down
6 changes: 3 additions & 3 deletions RiotSharp.Test/StatusRiotApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace RiotSharp.Test
[TestClass]
public class StatusRiotApiTest : CommonTestBase
{
private static StatusRiotApi api = StatusRiotApi.GetInstance(CommonTestBase.apiKey);
private static StatusRiotApi api = StatusRiotApi.GetInstance(apiKey);

[TestMethod]
[TestCategory("StatusRiotApi")]
public void GetShardStatus_Test()
{
EnsureCredibility(() =>
{
var shardStatus = api.GetShardStatus(StatusRiotApiTestBase.summoner1and2Region);
var shardStatus = api.GetShardStatus(summoner1and2Region);

Assert.AreEqual(StatusRiotApiTestBase.platform.ToString().ToLower(),
shardStatus.RegionTag);
Expand All @@ -29,7 +29,7 @@ public void GetShardStatusAsync_Test()
{
EnsureCredibility(() =>
{
var shardStatus = api.GetShardStatusAsync(StatusRiotApiTestBase.summoner1and2Region);
var shardStatus = api.GetShardStatusAsync(summoner1and2Region);

Assert.AreEqual(StatusRiotApiTestBase.platform.ToString().ToLower(),
shardStatus.Result.RegionTag);
Expand Down
39 changes: 39 additions & 0 deletions RiotSharp/Http/FailedRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Http
{
public class FailedRequestHandler : IFailedRequestHandler
{
public void Handle(HttpResponseMessage message)
{
if (message.IsSuccessStatusCode)
{
return;
}
switch (message.StatusCode)
{
case HttpStatusCode.ServiceUnavailable:
throw new RiotSharpException("503, Service unavailable", message.StatusCode);
case HttpStatusCode.InternalServerError:
throw new RiotSharpException("500, Internal server error", message.StatusCode);
case HttpStatusCode.Unauthorized:
throw new RiotSharpException("401, Unauthorized", message.StatusCode);
case HttpStatusCode.BadRequest:
throw new RiotSharpException("400, Bad request", message.StatusCode);
case HttpStatusCode.NotFound:
throw new RiotSharpException("404, Resource not found", message.StatusCode);
case HttpStatusCode.Forbidden:
throw new RiotSharpException("403, Forbidden", message.StatusCode);
case (HttpStatusCode)429:
throw new RiotSharpException("429, Rate Limit Exceeded", message.StatusCode);
default:
throw new RiotSharpException("Unexpeced failure", message.StatusCode);
}
}
}
}
9 changes: 9 additions & 0 deletions RiotSharp/Http/Interfaces/IFailedRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Net.Http;

namespace RiotSharp.Http.Interfaces
{
public interface IFailedRequestHandler
{
void Handle(HttpResponseMessage message);
}
}
12 changes: 12 additions & 0 deletions RiotSharp/Http/Interfaces/IRateLimitProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using RiotSharp.Misc;

namespace RiotSharp.Http.Interfaces
{
public interface IRateLimitProvider
{
RateLimiter GetLimiter(Region region);
}
}
95 changes: 1 addition & 94 deletions RiotSharp/Http/Interfaces/IRateLimitedRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,7 @@

namespace RiotSharp.Http.Interfaces
{
public interface IRateLimitedRequester
public interface IRateLimitedRequester : IRequester
{
/// <summary>
/// Create a get request and send it synchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
string CreateGetRequest(string relativeUrl, Region region, List<string> addedArguments = null,
bool useHttps = true);

/// <summary>
/// Create a get request and send it asynchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
Task<string> CreateGetRequestAsync(string relativeUrl, Region region, List<string> addedArguments = null,
bool useHttps = true);

/// <summary>
/// Create a post request and send it synchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="body"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
string CreatePostRequest(string relativeUrl, Region region, string body,
List<string> addedArguments = null, bool useHttps = true);

/// <summary>
/// Create a post request and send it asynchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="body"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
Task<string> CreatePostRequestAsync(string relativeUrl, Region region, string body,
List<string> addedArguments = null, bool useHttps = true);

/// <summary>
/// Create a put request and send it synchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="body"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
bool CreatePutRequest(string relativeUrl, Region region, string body, List<string> addedArguments = null,
bool useHttps = true);

/// <summary>
/// Create a post request and send it asynchronously to the server.
/// </summary>
/// <param name="relativeUrl"></param>
/// <param name="region"></param>
/// <param name="body"></param>
/// <param name="addedArguments"></param>
/// <param name="useHttps"></param>
/// <returns>The content of the response.</returns>
/// <exception cref="RiotSharpException">
/// Thrown if an Http error occurs.
/// Contains the Http error code and error message.
/// </exception>
Task<bool> CreatePutRequestAsync(string relativeUrl, Region region, string body,
List<string> addedArguments = null, bool useHttps = true);
}
}
Loading