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

Implemented a mock http client for Issue #459 #483

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions RiotSharp/Http/Interfaces/IRiotApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace RiotSharp.Http.Interfaces
{
public interface IRiotApiClient
{
/// <summary>
/// Send a get request asynchronously to the riot api.
/// </summary>
/// <param name="requestUri"></param>
/// <returns></returns>
Task<HttpResponseMessage> GetAsync(Uri requestUri);
/// <summary>
/// Send a put request asynchronously to the riot api.
/// </summary>
/// <param name="requestUri"></param>
/// <param name="content"></param>
/// <returns></returns>
Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content);
/// <summary>
/// Send a post request asynchronously to the riot api.
/// </summary>
/// <param name="requestUri"></param>
/// <param name="content"></param>
/// <returns></returns>
Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content);
}
}
39 changes: 39 additions & 0 deletions RiotSharp/Http/MockRiotApiClient.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.Threading.Tasks;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Http
{
public class MockRiotApiClient : IRiotApiClient
{
private readonly Dictionary<Uri, HttpResponseMessage> responses;

public MockRiotApiClient()
{
responses = new Dictionary<Uri, HttpResponseMessage>();
}

public MockRiotApiClient(Dictionary<Uri, HttpResponseMessage> mockResponses)
{
responses = mockResponses;
}

public Task<HttpResponseMessage> GetAsync(Uri requestUri)
{
return Task.FromResult(responses.ContainsKey(requestUri) ? responses[requestUri] : new HttpResponseMessage(HttpStatusCode.NotFound));
}

public Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content)
{
return Task.FromResult(responses.ContainsKey(requestUri) ? responses[requestUri] : new HttpResponseMessage(HttpStatusCode.NotFound));
}

public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content)
{
return Task.FromResult(responses.ContainsKey(requestUri) ? responses[requestUri] : new HttpResponseMessage(HttpStatusCode.NotFound));
}
}
}
5 changes: 5 additions & 0 deletions RiotSharp/Http/RateLimitedRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public RateLimitedRequester(string apiKey, IDictionary<TimeSpan, int> rateLimits
RateLimits = rateLimits;
}

public RateLimitedRequester(string apiKey, IDictionary<TimeSpan, int> rateLimits, IRiotApiClient apiClient) : base(apiKey, apiClient)
{
RateLimits = rateLimits;
}

private readonly Dictionary<Region, RateLimiter> rateLimiters = new Dictionary<Region, RateLimiter>();

#region Public Methods
Expand Down
4 changes: 4 additions & 0 deletions RiotSharp/Http/Requester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public Requester(string apiKey) : base(apiKey)
{
}

public Requester(string apiKey, IRiotApiClient apiClient) : base(apiKey, apiClient)
{
}

#region Public Methods
public string CreateGetRequest(string relativeUrl, Region region, List<string> addedArguments = null,
bool useHttps = true)
Expand Down
26 changes: 17 additions & 9 deletions RiotSharp/Http/RequesterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Http
{
public abstract class RequesterBase
{
protected string rootDomain;
protected const string platformDomain = ".api.riotgames.com";
private readonly HttpClient httpClient;

private readonly IRiotApiClient riotApiClient;
public string ApiKey { get; set; }

protected RequesterBase(string apiKey)
{
if (string.IsNullOrWhiteSpace(apiKey))
throw new ArgumentNullException(nameof(apiKey));
ApiKey = apiKey;
httpClient = new HttpClient();
riotApiClient = new RiotApiClient();
}

protected RequesterBase(string apiKey, IRiotApiClient apiClient)
{
if (string.IsNullOrWhiteSpace(apiKey))
throw new ArgumentNullException(nameof(apiKey));
ApiKey = apiKey;
riotApiClient = apiClient;
}

#region Protected Methods
Expand All @@ -34,7 +42,7 @@ protected RequesterBase(string apiKey)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected HttpResponseMessage Get(HttpRequestMessage request)
{
var response = httpClient.GetAsync(request.RequestUri).Result;
var response = riotApiClient.GetAsync(request.RequestUri).Result;
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand All @@ -50,7 +58,7 @@ protected HttpResponseMessage Get(HttpRequestMessage request)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
{
var response = await httpClient.GetAsync(request.RequestUri);
var response = await riotApiClient.GetAsync(request.RequestUri);
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand All @@ -67,7 +75,7 @@ protected async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected HttpResponseMessage Put(HttpRequestMessage request)
{
var response = httpClient.PutAsync(request.RequestUri, request.Content).Result;
var response = riotApiClient.PutAsync(request.RequestUri, request.Content).Result;
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand All @@ -83,7 +91,7 @@ protected HttpResponseMessage Put(HttpRequestMessage request)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected async Task<HttpResponseMessage> PutAsync(HttpRequestMessage request)
{
var response = await httpClient.PutAsync(request.RequestUri, request.Content);
var response = await riotApiClient.PutAsync(request.RequestUri, request.Content);
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand All @@ -99,7 +107,7 @@ protected async Task<HttpResponseMessage> PutAsync(HttpRequestMessage request)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected HttpResponseMessage Post(HttpRequestMessage request)
{
var response = httpClient.PostAsync(request.RequestUri, request.Content).Result;
var response = riotApiClient.PostAsync(request.RequestUri, request.Content).Result;
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand All @@ -115,7 +123,7 @@ protected HttpResponseMessage Post(HttpRequestMessage request)
/// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception>
protected async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request)
{
var response = await httpClient.PostAsync(request.RequestUri, request.Content);
var response = await riotApiClient.PostAsync(request.RequestUri, request.Content);
if (!response.IsSuccessStatusCode)
{
HandleRequestFailure(response.StatusCode);
Expand Down
31 changes: 31 additions & 0 deletions RiotSharp/Http/RiotApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using RiotSharp.Http.Interfaces;

namespace RiotSharp.Http
{
public class RiotApiClient : IRiotApiClient
{
private readonly HttpClient httpClient;

public RiotApiClient()
{
httpClient = new HttpClient();
}
public Task<HttpResponseMessage> GetAsync(Uri requestUri)
{
return httpClient.GetAsync(requestUri);
}

public Task<HttpResponseMessage> PutAsync(Uri requestUri, HttpContent content)
{
return httpClient.PutAsync(requestUri, content);
}

public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content)
{
return httpClient.PostAsync(requestUri, content);
}
}
}