Skip to content

Commit

Permalink
Added API failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Jul 29, 2016
1 parent 2be656d commit 33654cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions PokemonGo.RocketAPI/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
23 changes: 21 additions & 2 deletions PokemonGo.RocketAPI/Extensions/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,43 @@

namespace PokemonGo.RocketAPI.Extensions
{
public enum ApiOperation
{
Retry,
Abort
}

public interface IApiFailureStrategy
{
Task<ApiOperation> HandleApiFailure();
void HandleApiSuccess();
}

public static class HttpClientExtensions
{
public static async Task<TResponsePayload> PostProtoPayload<TRequest, TResponsePayload>(this System.Net.Http.HttpClient client,
string url, RequestEnvelope requestEnvelope) where TRequest : IMessage<TRequest>
string url, RequestEnvelope requestEnvelope, IApiFailureStrategy strategy) where TRequest : IMessage<TRequest>
where TResponsePayload : IMessage<TResponsePayload>, new()
{
Debug.WriteLine($"Requesting {typeof(TResponsePayload).Name}");
var response = await PostProto<TRequest>(client, url, requestEnvelope);

while (response.Returns.Count == 0)
{
await Task.Delay(500);
var operation = await strategy.HandleApiFailure();
if (operation == ApiOperation.Abort)
{
break;
}

response = await PostProto<TRequest>(client, url, requestEnvelope);
}

if (response.Returns.Count == 0)
throw new InvalidResponseException();

strategy.HandleApiSuccess();

//Decode payload
//todo: multi-payload support
var payload = response.Returns[0];
Expand Down
4 changes: 2 additions & 2 deletions PokemonGo.RocketAPI/Rpc/BaseRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ protected async Task<TResponsePayload> PostProtoPayload<TRequest, TResponsePaylo
where TResponsePayload : IMessage<TResponsePayload>, new()
{
var requestEnvelops = RequestBuilder.GetRequestEnvelope(type, message);
return await _client.PokemonHttpClient.PostProtoPayload<TRequest, TResponsePayload>(ApiUrl, requestEnvelops);
return await _client.PokemonHttpClient.PostProtoPayload<TRequest, TResponsePayload>(ApiUrl, requestEnvelops, _client.ApiFailure);
}

protected async Task<TResponsePayload> PostProtoPayload<TRequest, TResponsePayload>(RequestEnvelope requestEnvelope) where TRequest : IMessage<TRequest>
where TResponsePayload : IMessage<TResponsePayload>, new()
{
return await _client.PokemonHttpClient.PostProtoPayload<TRequest, TResponsePayload>(ApiUrl, requestEnvelope);
return await _client.PokemonHttpClient.PostProtoPayload<TRequest, TResponsePayload>(ApiUrl, requestEnvelope, _client.ApiFailure);
}

protected async Task<ResponseEnvelope> PostProto<TRequest>(RequestEnvelope requestEnvelope) where TRequest : IMessage<TRequest>
Expand Down

0 comments on commit 33654cd

Please sign in to comment.