Skip to content

Commit

Permalink
Improve API errors when servers are bad
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxRev committed Jul 28, 2016
1 parent fb4ad3d commit 234e52c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions PokemonGo.RocketAPI/Exceptions/AccountNotVerifiedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ namespace PokemonGo.RocketAPI.Exceptions
{
public class AccountNotVerifiedException : Exception
{
public AccountNotVerifiedException(string message) : base(message)
{

}
}
}
21 changes: 19 additions & 2 deletions PokemonGo.RocketAPI/Helpers/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@ public RequestEnvelope GetRequestEnvelope(params Request[] customRequests)
StatusCode = 2, //1

RequestId = 1469378659230941192, //3
Requests = {customRequests}, //4
Requests = { customRequests }, //4

//Unknown6 = , //6
Latitude = _latitude, //7
Longitude = _longitude, //8
Altitude = _altitude, //9
AuthTicket = _authTicket, //11
Unknown12 = 989 //12
};
}

public RequestEnvelope GetInitialRequestEnvelope(params Request[] customRequests)
{
return new RequestEnvelope
{
StatusCode = 2, //1

RequestId = 1469378659230941192, //3
Requests = { customRequests }, //4

//Unknown6 = , //6
Latitude = _latitude, //7
Expand All @@ -48,7 +66,6 @@ public RequestEnvelope GetRequestEnvelope(params Request[] customRequests)
Unknown2 = 14
}
}, //10
AuthTicket = _authTicket, //11
Unknown12 = 989 //12
};
}
Expand Down
2 changes: 1 addition & 1 deletion PokemonGo.RocketAPI/Helpers/RetryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
try
{
var response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == HttpStatusCode.BadGateway)
if (response.StatusCode == HttpStatusCode.BadGateway || response.StatusCode == HttpStatusCode.InternalServerError)
throw new Exception(); //todo: proper implementation

return response;
Expand Down
8 changes: 6 additions & 2 deletions PokemonGo.RocketAPI/Login/PtcLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task<string> GetAccessToken(string username, string password
AllowAutoRedirect = false
};

using (var tempHttpClient = new System.Net.Http.HttpClient(handler))
using (var tempHttpClient = new System.Net.Http.HttpClient(new RetryHandler(handler)))
{
//Get session cookie
var sessionResp = await tempHttpClient.GetAsync(Resources.PtcLoginUrl);
Expand All @@ -41,11 +41,15 @@ public static async Task<string> GetAccessToken(string username, string password
new KeyValuePair<string, string>("password", password)
}));

var content = await loginResp.Content.ReadAsStringAsync();

if (loginResp.Headers.Location == null)
{
//This should be sufficient for catching AccountNotVerified exceptions
var errors = JsonHelper.GetValue(content, "errors");
if (loginResp.StatusCode == HttpStatusCode.OK && !loginResp.Headers.Contains("Set-Cookies"))
throw new AccountNotVerifiedException();
throw new AccountNotVerifiedException(errors);

throw new PtcOfflineException();
}

Expand Down
2 changes: 1 addition & 1 deletion PokemonGo.RocketAPI/Rpc/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private async Task SetServer()

#endregion

var serverRequest = RequestBuilder.GetRequestEnvelope(
var serverRequest = RequestBuilder.GetInitialRequestEnvelope(
new Request
{
RequestType = RequestType.GetPlayer,
Expand Down

0 comments on commit 234e52c

Please sign in to comment.