-
Notifications
You must be signed in to change notification settings - Fork 143
Refactored Requester #480
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
Closed
Closed
Refactored Requester #480
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ccecf2f
Improve string and date Util methods
frederickrogan 48830c7
Remove extra empty line in Util
frederickrogan 75ffae0
Merge remote-tracking branch 'BenFradet/develop' into develop
frederickrogan e9931e1
Alternative requester
frederickrogan 3849379
Split StatusRiotApi
frederickrogan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| using System.Net.Http; | ||
|
|
||
| namespace RiotSharp.Http.Interfaces | ||
| { | ||
| public interface IFailedRequestHandler | ||
| { | ||
| void Handle(HttpResponseMessage message); | ||
| } | ||
| } |
This file contains hidden or 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.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace RiotSharp.Http.Interfaces | ||
| { | ||
| public interface IRequestClient | ||
| { | ||
| /// <summary> | ||
| /// Send a get request synchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| HttpResponseMessage Get(HttpRequestMessage request); | ||
|
|
||
| /// <summary> | ||
| /// Send a get request asynchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| Task<HttpResponseMessage> GetAsync(HttpRequestMessage request); | ||
|
|
||
| /// <summary> | ||
| /// Send a put request synchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| HttpResponseMessage Put(HttpRequestMessage request); | ||
|
|
||
| /// <summary> | ||
| /// Send a put request asynchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| Task<HttpResponseMessage> PutAsync(HttpRequestMessage request); | ||
|
|
||
| /// <summary> | ||
| /// Send a post request synchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| HttpResponseMessage Post(HttpRequestMessage request); | ||
|
|
||
| /// <summary> | ||
| /// Send a post request asynchronously. | ||
| /// </summary> | ||
| /// <param name="request"></param> | ||
| /// <returns></returns> | ||
| /// <exception cref="RiotSharpException">Thrown if an Http error occurs. Contains the Http error code and error message.</exception> | ||
| Task<HttpResponseMessage> PostAsync(HttpRequestMessage request); | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using RiotSharp.Misc; | ||
|
|
||
| namespace RiotSharp.Http.Interfaces | ||
| { | ||
| public interface IRequestCreator | ||
| { | ||
| HttpRequestMessage CreateGetRequest(Region region, string apiEndpoint, List<string> addedArguments, bool useHttps = true); | ||
| } | ||
| } | ||
This file contains hidden or 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,40 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using RiotSharp.Misc; | ||
|
|
||
| namespace RiotSharp.Http.Interfaces | ||
| { | ||
| public interface IRequesterAlt | ||
| { | ||
| /// <summary> | ||
| /// Creates and sends a get request. Deserializes the response. | ||
| /// </summary> | ||
| /// <param name="relativeUrl"></param> | ||
| /// <param name="region"></param> | ||
| /// <param name="addedArguments"></param> | ||
| /// <param name="useHttps"></param> | ||
| /// <returns>The response deserialized to the requested type</returns> | ||
| /// <exception cref="RiotSharpException"> | ||
| /// Thrown if an Http error occurs. | ||
| /// Contains the Http error code and error message. | ||
| /// </exception> | ||
| T Get<T>(string relativeUrl, Region region, List<string> addedArguments = null, bool useHttps = true); | ||
|
|
||
| /// <summary> | ||
| /// Creates and asynchronously sends a get request. Deserializes the response. | ||
| /// </summary> | ||
| /// <param name="relativeUrl"></param> | ||
| /// <param name="region"></param> | ||
| /// <param name="addedArguments"></param> | ||
| /// <param name="useHttps"></param> | ||
| /// <returns>The response deserialized to the requested type</returns> | ||
| /// <exception cref="RiotSharpException"> | ||
| /// Thrown if an Http error occurs. | ||
| /// Contains the Http error code and error message. | ||
| /// </exception> | ||
| Task<T> GetAsync<T>(string relativeUrl, Region region, List<string> addedArguments = null, bool useHttps = true); | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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,15 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace RiotSharp.Http.Interfaces | ||
| { | ||
| public interface IResponseDeserializer | ||
| { | ||
| T DeserializeTo<T>(HttpResponseMessage message); | ||
|
|
||
| Task<T> DeserializeToAsync<T>(HttpResponseMessage message); | ||
| } | ||
| } |
This file contains hidden or 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,35 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using RiotSharp.Misc; | ||
|
|
||
| namespace RiotSharp.Http.Maps | ||
| { | ||
| public static class RegionPlatformMap | ||
| { | ||
|
|
||
| private static readonly IDictionary<Region, string> Map = new Dictionary<Region, string> | ||
| { | ||
| { Region.br, "br1" }, | ||
| { Region.eune , "eun1" }, | ||
| { Region.euw , "euw1" }, | ||
| { Region.jp , "jp1" }, | ||
| { Region.kr , "kr" }, | ||
| { Region.lan , "la1" }, | ||
| { Region.las , "la2" }, | ||
| { Region.na , "na1" }, | ||
| { Region.oce , "oc1" }, | ||
| { Region.tr , "tr1" }, | ||
| { Region.ru , "ru" }, | ||
| { Region.global , "global" } | ||
| }; | ||
|
|
||
| public static string GetPlatform(Region region) | ||
| => Map.ContainsKey(region) ? Map[region] : throw new NotImplementedException("Unsupported region"); | ||
|
|
||
| public static string GetPlatformDomain(Region region) | ||
| => $"{GetPlatform(region)}.api.riotgames.com"; | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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,52 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using RiotSharp.Http.Interfaces; | ||
|
|
||
| namespace RiotSharp.Http | ||
| { | ||
| public class RequestClient : IRequestClient | ||
| { | ||
| private readonly HttpClient client; | ||
| private readonly IFailedRequestHandler failureHandler; | ||
|
|
||
| public RequestClient(HttpClient client, IFailedRequestHandler failureHandler) | ||
| { | ||
| this.client = client; | ||
| this.failureHandler = failureHandler; | ||
| } | ||
|
|
||
| public HttpResponseMessage Get(HttpRequestMessage request) | ||
| => GetAsync(request).Result; | ||
|
|
||
| public async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request) | ||
| { | ||
| var response = await client.GetAsync(request.RequestUri); | ||
| failureHandler.Handle(response); | ||
| return response; | ||
| } | ||
|
|
||
| public HttpResponseMessage Put(HttpRequestMessage request) | ||
| => PutAsync(request).Result; | ||
|
|
||
| public async Task<HttpResponseMessage> PutAsync(HttpRequestMessage request) | ||
| { | ||
| var response = await client.PutAsync(request.RequestUri, request.Content); | ||
| failureHandler.Handle(response); | ||
| return response; | ||
| } | ||
|
|
||
| public HttpResponseMessage Post(HttpRequestMessage request) | ||
| => PostAsync(request).Result; | ||
|
|
||
| public async Task<HttpResponseMessage> PostAsync(HttpRequestMessage request) | ||
| { | ||
| var response = await client.PostAsync(request.RequestUri, request.Content); | ||
| failureHandler.Handle(response); | ||
| return response; | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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,43 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using RiotSharp.Http.Interfaces; | ||
| using RiotSharp.Http.Maps; | ||
| using RiotSharp.Misc; | ||
|
|
||
| namespace RiotSharp.Http | ||
| { | ||
| public class RequestCreator : IRequestCreator | ||
| { | ||
| private readonly string apiKey; | ||
|
|
||
| public RequestCreator(string apiKey) | ||
| { | ||
| this.apiKey = apiKey; | ||
| } | ||
|
|
||
| public HttpRequestMessage CreateGetRequest(Region region, string apiEndpoint, List<string> addedArguments, bool useHttps = true) | ||
| { | ||
| var url = BuildUrl(region, apiEndpoint, addedArguments, useHttps); | ||
| return new HttpRequestMessage(HttpMethod.Get, url); | ||
| } | ||
|
|
||
| private string BuildUrl(Region region, string apiEndpoint, List<string> addedArguments, bool useHttps) | ||
| { | ||
| var scheme = useHttps ? "https" : "http"; | ||
| var domain = RegionPlatformMap.GetPlatformDomain(region); | ||
| var arguments = BuildArgumentString(addedArguments); | ||
|
|
||
| return $"{scheme}://{domain}{apiEndpoint}?{arguments}"; | ||
| } | ||
|
|
||
| private string BuildArgumentString(IEnumerable<string> arguments) | ||
| => string.Join("&", arguments.Concat(GetApiKeyParameter()).Where(IsNotEmpty)); | ||
|
|
||
| private IEnumerable<string> GetApiKeyParameter() | ||
| => new[] { $"api_key={apiKey}" }; | ||
|
|
||
| private static bool IsNotEmpty(string argument) | ||
| => argument != string.Empty; | ||
| } | ||
| } |
This file contains hidden or 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,53 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using RiotSharp.Http.Interfaces; | ||
| using RiotSharp.Misc; | ||
|
|
||
| namespace RiotSharp.Http | ||
| { | ||
| /// <summary> | ||
| /// An alternative requester without a rate limiter. | ||
| /// </summary> | ||
| public class RequesterAlt : IRequesterAlt | ||
| { | ||
| private readonly IRequestClient client; | ||
| private readonly IRequestCreator requestCreator; | ||
| private readonly IResponseDeserializer responseDeserializer; | ||
|
|
||
| private static List<string> NoArguments() => new List<string>(); | ||
|
|
||
| public RequesterAlt(IRequestClient client, IRequestCreator requestCreator, IResponseDeserializer responseDeserializer) | ||
| { | ||
| this.client = client; | ||
| this.requestCreator = requestCreator; | ||
| this.responseDeserializer = responseDeserializer; | ||
| } | ||
|
|
||
| public T Get<T>( | ||
| string relativeUrl, | ||
| Region region, | ||
| List<string> addedArguments = null, | ||
| bool useHttps = true) | ||
| { | ||
| var arguments = addedArguments ?? NoArguments(); | ||
| var request = requestCreator.CreateGetRequest(region, relativeUrl, arguments, useHttps); | ||
| var response = client.Get(request); | ||
| return responseDeserializer.DeserializeTo<T>(response); | ||
| } | ||
|
|
||
| public async Task<T> GetAsync<T>( | ||
| string relativeUrl, | ||
| Region region, | ||
| List<string> addedArguments = null, | ||
| bool useHttps = true) | ||
| { | ||
| var arguments = addedArguments ?? NoArguments(); | ||
| var request = requestCreator.CreateGetRequest(region, relativeUrl, arguments, useHttps); | ||
| var response = await client.GetAsync(request); | ||
| return await responseDeserializer.DeserializeToAsync<T>(response); | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here might be needed functions for creating
PUTandPOSTrequests. They will be needed for the tournament endpoint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I hadn't written them yet since this only covered the standard requester which is only used by the status api. I'll include it in the branch you suggested.