From 33654cd598bf0f146872169a86eafe297de8a608 Mon Sep 17 00:00:00 2001 From: Maxime GRIOT Date: Fri, 29 Jul 2016 14:47:23 +0200 Subject: [PATCH] Added API failure handling --- PokemonGo.RocketAPI/Client.cs | 1 + .../Extensions/HttpClientExtensions.cs | 23 +++++++++++++++++-- PokemonGo.RocketAPI/Rpc/BaseRpc.cs | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/PokemonGo.RocketAPI/Client.cs b/PokemonGo.RocketAPI/Client.cs index a037524..3d6eddb 100644 --- a/PokemonGo.RocketAPI/Client.cs +++ b/PokemonGo.RocketAPI/Client.cs @@ -29,6 +29,7 @@ public class Client public Rpc.Encounter Encounter; public Rpc.Misc Misc; + public IApiFailureStrategy ApiFailure { get; set; } public ISettings Settings { get; } public string AuthToken { get; set; } diff --git a/PokemonGo.RocketAPI/Extensions/HttpClientExtensions.cs b/PokemonGo.RocketAPI/Extensions/HttpClientExtensions.cs index 2822439..6eea2f3 100644 --- a/PokemonGo.RocketAPI/Extensions/HttpClientExtensions.cs +++ b/PokemonGo.RocketAPI/Extensions/HttpClientExtensions.cs @@ -7,10 +7,22 @@ namespace PokemonGo.RocketAPI.Extensions { + public enum ApiOperation + { + Retry, + Abort + } + + public interface IApiFailureStrategy + { + Task HandleApiFailure(); + void HandleApiSuccess(); + } + public static class HttpClientExtensions { public static async Task PostProtoPayload(this System.Net.Http.HttpClient client, - string url, RequestEnvelope requestEnvelope) where TRequest : IMessage + string url, RequestEnvelope requestEnvelope, IApiFailureStrategy strategy) where TRequest : IMessage where TResponsePayload : IMessage, new() { Debug.WriteLine($"Requesting {typeof(TResponsePayload).Name}"); @@ -18,13 +30,20 @@ public static async Task PostProtoPayload(client, url, requestEnvelope); } if (response.Returns.Count == 0) throw new InvalidResponseException(); + strategy.HandleApiSuccess(); + //Decode payload //todo: multi-payload support var payload = response.Returns[0]; diff --git a/PokemonGo.RocketAPI/Rpc/BaseRpc.cs b/PokemonGo.RocketAPI/Rpc/BaseRpc.cs index 4b12d25..1544291 100644 --- a/PokemonGo.RocketAPI/Rpc/BaseRpc.cs +++ b/PokemonGo.RocketAPI/Rpc/BaseRpc.cs @@ -26,13 +26,13 @@ protected async Task PostProtoPayload, new() { var requestEnvelops = RequestBuilder.GetRequestEnvelope(type, message); - return await _client.PokemonHttpClient.PostProtoPayload(ApiUrl, requestEnvelops); + return await _client.PokemonHttpClient.PostProtoPayload(ApiUrl, requestEnvelops, _client.ApiFailure); } protected async Task PostProtoPayload(RequestEnvelope requestEnvelope) where TRequest : IMessage where TResponsePayload : IMessage, new() { - return await _client.PokemonHttpClient.PostProtoPayload(ApiUrl, requestEnvelope); + return await _client.PokemonHttpClient.PostProtoPayload(ApiUrl, requestEnvelope, _client.ApiFailure); } protected async Task PostProto(RequestEnvelope requestEnvelope) where TRequest : IMessage