Skip to content

Commit 24c67c1

Browse files
authored
Consistently use RemoteServerException for network/server errors (#243)
1 parent 819d02d commit 24c67c1

File tree

7 files changed

+45
-57
lines changed

7 files changed

+45
-57
lines changed

src/main/java/com/pokegoapi/api/PokemonGo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.pokegoapi.api;
1717

1818
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass;
19-
2019
import com.pokegoapi.api.inventory.Inventories;
2120
import com.pokegoapi.api.map.Map;
2221
import com.pokegoapi.api.player.PlayerProfile;
@@ -25,7 +24,6 @@
2524
import com.pokegoapi.exceptions.RemoteServerException;
2625
import com.pokegoapi.main.RequestHandler;
2726
import com.pokegoapi.util.Log;
28-
2927
import lombok.Getter;
3028
import lombok.Setter;
3129
import okhttp3.OkHttpClient;
@@ -86,10 +84,12 @@ public PokemonGo(CredentialProvider credentialProvider, OkHttpClient client)
8684

8785
/**
8886
* Fetches valid AuthInfo
87+
*
8988
* @return AuthInfo object
9089
* @throws LoginFailedException when login fails
9190
*/
92-
public RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo getAuthInfo() throws LoginFailedException {
91+
public RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo getAuthInfo()
92+
throws LoginFailedException, RemoteServerException {
9393
return credentialProvider.getAuthInfo();
9494
}
9595

src/main/java/com/pokegoapi/auth/CredentialProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
1919

2020
import com.pokegoapi.exceptions.LoginFailedException;
21+
import com.pokegoapi.exceptions.RemoteServerException;
2122

2223
/**
2324
* Any Credential Provider can extend this.
2425
*/
2526
public abstract class CredentialProvider {
2627

27-
public abstract String getTokenId() throws LoginFailedException;
28+
public abstract String getTokenId() throws LoginFailedException, RemoteServerException;
2829

29-
public abstract AuthInfo getAuthInfo() throws LoginFailedException;
30+
public abstract AuthInfo getAuthInfo() throws LoginFailedException, RemoteServerException;
3031

3132
public abstract boolean isTokenIdExpired();
3233
}

src/main/java/com/pokegoapi/auth/GoogleCredentialProvider.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
package com.pokegoapi.auth;
1717

1818
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
19-
2019
import com.pokegoapi.exceptions.LoginFailedException;
20+
import com.pokegoapi.exceptions.RemoteServerException;
2121
import com.pokegoapi.util.Log;
2222
import com.squareup.moshi.Moshi;
23-
2423
import lombok.Getter;
2524
import okhttp3.HttpUrl;
2625
import okhttp3.OkHttpClient;
@@ -33,16 +32,13 @@
3332

3433
public class GoogleCredentialProvider extends CredentialProvider {
3534

36-
private static final String TAG = GoogleCredentialProvider.class.getSimpleName();
37-
38-
//We try and refresh token 5 minutes before it actually expires
39-
private static final long REFRESH_TOKEN_BUFFER_TIME = 5 * 60 * 1000;
40-
4135
public static final String SECRET = "NCjF1TLi2CcY6t5mt0ZveuL7";
4236
public static final String CLIENT_ID = "848232511240-73ri3t7plvk96pj4f85uj8otdat2alem.apps.googleusercontent.com";
4337
public static final String OAUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/device/code";
4438
public static final String OAUTH_TOKEN_ENDPOINT = "https://www.googleapis.com/oauth2/v4/token";
45-
39+
private static final String TAG = GoogleCredentialProvider.class.getSimpleName();
40+
//We try and refresh token 5 minutes before it actually expires
41+
private static final long REFRESH_TOKEN_BUFFER_TIME = 5 * 60 * 1000;
4642
private final OkHttpClient client;
4743

4844
private final OnGoogleLoginOAuthCompleteListener onGoogleLoginOAuthCompleteListener;
@@ -63,7 +59,8 @@ public class GoogleCredentialProvider extends CredentialProvider {
6359
* @param refreshToken Refresh Token Persisted by user
6460
* @throws LoginFailedException When login fails
6561
*/
66-
public GoogleCredentialProvider(OkHttpClient client, String refreshToken) throws LoginFailedException {
62+
public GoogleCredentialProvider(OkHttpClient client, String refreshToken)
63+
throws LoginFailedException, RemoteServerException {
6764
this.client = client;
6865
this.refreshToken = refreshToken;
6966
onGoogleLoginOAuthCompleteListener = null;
@@ -97,7 +94,7 @@ public GoogleCredentialProvider(OkHttpClient client,
9794
* @param refreshToken Refresh token persisted by the user after initial login
9895
* @throws LoginFailedException If we fail to get tokenId
9996
*/
100-
public void refreshToken(String refreshToken) throws LoginFailedException {
97+
public void refreshToken(String refreshToken) throws LoginFailedException, RemoteServerException {
10198
HttpUrl url = HttpUrl.parse(OAUTH_TOKEN_ENDPOINT).newBuilder()
10299
.addQueryParameter("client_id", CLIENT_ID)
103100
.addQueryParameter("client_secret", SECRET)
@@ -115,15 +112,15 @@ public void refreshToken(String refreshToken) throws LoginFailedException {
115112
try {
116113
response = client.newCall(request).execute();
117114
} catch (IOException e) {
118-
throw new LoginFailedException("Network Request failed to fetch refreshed tokenId", e);
115+
throw new RemoteServerException("Network Request failed to fetch refreshed tokenId", e);
119116
}
120117
Moshi moshi = new Moshi.Builder().build();
121118
GoogleAuthTokenJson googleAuthTokenJson = null;
122119
try {
123120
googleAuthTokenJson = moshi.adapter(GoogleAuthTokenJson.class).fromJson(response.body().string());
124121
Log.d(TAG, "" + googleAuthTokenJson.getExpiresIn());
125122
} catch (IOException e) {
126-
throw new LoginFailedException("Failed to unmarshell the Json response to fetch refreshed tokenId", e);
123+
throw new RemoteServerException("Failed to unmarshal the Json response to fetch refreshed tokenId", e);
127124
}
128125
if (googleAuthTokenJson.getError() != null) {
129126
throw new LoginFailedException(googleAuthTokenJson.getError());
@@ -224,7 +221,7 @@ private GoogleAuthTokenJson poll(GoogleAuthJson json) throws URISyntaxException,
224221
}
225222

226223
@Override
227-
public String getTokenId() throws LoginFailedException {
224+
public String getTokenId() throws LoginFailedException, RemoteServerException {
228225
if (isTokenIdExpired()) {
229226
refreshToken(refreshToken);
230227
}
@@ -238,7 +235,7 @@ public String getTokenId() throws LoginFailedException {
238235
* @throws LoginFailedException When login fails
239236
*/
240237
@Override
241-
public AuthInfo getAuthInfo() throws LoginFailedException {
238+
public AuthInfo getAuthInfo() throws LoginFailedException, RemoteServerException {
242239
if (isTokenIdExpired()) {
243240
refreshToken(refreshToken);
244241
}

src/main/java/com/pokegoapi/auth/PtcCredentialProvider.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515

1616
package com.pokegoapi.auth;
1717

18-
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass;
1918
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
20-
2119
import com.pokegoapi.exceptions.LoginFailedException;
22-
import com.pokegoapi.util.Log;
20+
import com.pokegoapi.exceptions.RemoteServerException;
2321
import com.squareup.moshi.Moshi;
24-
25-
import lombok.Getter;
2622
import okhttp3.Cookie;
2723
import okhttp3.CookieJar;
2824
import okhttp3.HttpUrl;
@@ -39,40 +35,33 @@
3935

4036

4137
public class PtcCredentialProvider extends CredentialProvider {
42-
private static final String TAG = PtcCredentialProvider.class.getSimpleName();
43-
4438
public static final String CLIENT_SECRET = "w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR";
4539
public static final String REDIRECT_URI = "https://www.nianticlabs.com/pokemongo/error";
4640
public static final String CLIENT_ID = "mobile-app_pokemon-go";
47-
4841
public static final String API_URL = "https://pgorelease.nianticlabs.com/plfe/rpc";
4942
public static final String LOGIN_URL = "https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize";
5043
public static final String LOGIN_OAUTH = "https://sso.pokemon.com/sso/oauth2.0/accessToken";
51-
5244
public static final String USER_AGENT = "niantic";
53-
45+
private static final String TAG = PtcCredentialProvider.class.getSimpleName();
5446
//We try and refresh token 5 minutes before it actually expires
5547
private static final long REFRESH_TOKEN_BUFFER_TIME = 5 * 60 * 1000;
5648

5749
private final OkHttpClient client;
58-
50+
private final String username;
51+
private final String password;
5952
private String tokenId;
60-
6153
private long expiresTimestamp;
62-
6354
private AuthInfo.Builder authbuilder;
6455

65-
private final String username;
66-
private final String password;
67-
6856
/**
6957
* Instantiates a new Ptc login.
7058
*
71-
* @param client the client
59+
* @param client the client
7260
* @param username Username
7361
* @param password password
7462
*/
75-
public PtcCredentialProvider(OkHttpClient client, String username, String password) throws LoginFailedException {
63+
public PtcCredentialProvider(OkHttpClient client, String username, String password)
64+
throws LoginFailedException, RemoteServerException {
7665
this.username = username;
7766
this.password = password;
7867
/*
@@ -119,7 +108,7 @@ public Response intercept(Chain chain) throws IOException {
119108
* @param username PTC username
120109
* @param password PTC password
121110
*/
122-
private void login(String username, String password) throws LoginFailedException {
111+
private void login(String username, String password) throws LoginFailedException, RemoteServerException {
123112
//TODO: stop creating an okhttp client per request
124113
Request get = new Request.Builder()
125114
.url(LOGIN_URL)
@@ -130,7 +119,7 @@ private void login(String username, String password) throws LoginFailedException
130119
try {
131120
getResponse = client.newCall(get).execute();
132121
} catch (IOException e) {
133-
throw new LoginFailedException(e);
122+
throw new RemoteServerException("Failed to receive contents from server", e);
134123
}
135124

136125
Moshi moshi = new Moshi.Builder().build();
@@ -140,7 +129,7 @@ private void login(String username, String password) throws LoginFailedException
140129
String response = getResponse.body().string();
141130
ptcAuth = moshi.adapter(PtcAuthJson.class).fromJson(response);
142131
} catch (IOException e) {
143-
throw new LoginFailedException("Looks like the servers are down", e);
132+
throw new RemoteServerException("Looks like the servers are down", e);
144133
}
145134

146135
HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder()
@@ -168,22 +157,22 @@ private void login(String username, String password) throws LoginFailedException
168157
.newCall(postRequest)
169158
.execute();
170159
} catch (IOException e) {
171-
throw new LoginFailedException("Network failure", e);
160+
throw new RemoteServerException("Network failure", e);
172161
}
173162

174163
String body = null;
175164
try {
176165
body = response.body().string();
177166
} catch (IOException e) {
178-
throw new LoginFailedException("Response body fetching failed", e);
167+
throw new RemoteServerException("Response body fetching failed", e);
179168
}
180169

181170
if (body.length() > 0) {
182171
PtcError ptcError = null;
183172
try {
184173
ptcError = moshi.adapter(PtcError.class).fromJson(body);
185174
} catch (IOException e) {
186-
throw new LoginFailedException("Unmarshling failure", e);
175+
throw new RemoteServerException("Unmarshalling failure", e);
187176
}
188177
if (ptcError.getError() != null && ptcError.getError().length() > 0) {
189178
throw new LoginFailedException(ptcError.getError());
@@ -211,13 +200,13 @@ private void login(String username, String password) throws LoginFailedException
211200
try {
212201
response = client.newCall(postRequest).execute();
213202
} catch (IOException e) {
214-
throw new LoginFailedException("Network Failure ", e);
203+
throw new RemoteServerException("Network Failure ", e);
215204
}
216205

217206
try {
218207
body = response.body().string();
219208
} catch (IOException e) {
220-
throw new LoginFailedException(e);
209+
throw new RemoteServerException("Network failure", e);
221210
}
222211

223212
String[] params;
@@ -232,7 +221,7 @@ private void login(String username, String password) throws LoginFailedException
232221
}
233222

234223
@Override
235-
public String getTokenId() throws LoginFailedException {
224+
public String getTokenId() throws LoginFailedException, RemoteServerException {
236225
if (isTokenIdExpired()) {
237226
login(username, password);
238227
}
@@ -246,7 +235,7 @@ public String getTokenId() throws LoginFailedException {
246235
* @throws LoginFailedException when refreshing fails
247236
*/
248237
@Override
249-
public AuthInfo getAuthInfo() throws LoginFailedException {
238+
public AuthInfo getAuthInfo() throws LoginFailedException, RemoteServerException {
250239
if (isTokenIdExpired()) {
251240
login(username, password);
252241
}

src/main/java/com/pokegoapi/exceptions/LoginFailedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public LoginFailedException(Exception exception) {
2929
}
3030

3131
public LoginFailedException(String reason, Exception exception) {
32-
super(reason,exception);
32+
super(reason, exception);
3333
}
3434
}

src/main/java/com/pokegoapi/exceptions/RemoteServerException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public RemoteServerException(String reason) {
2727
public RemoteServerException(Exception exception) {
2828
super(exception);
2929
}
30+
31+
public RemoteServerException(String reason, Exception exception) {
32+
super(reason, exception);
33+
}
3034
}

src/main/java/com/pokegoapi/main/RequestHandler.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717

1818
import POGOProtos.Networking.Envelopes.AuthTicketOuterClass;
1919
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass;
20-
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
2120
import POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass;
22-
2321
import com.google.protobuf.ByteString;
2422
import com.pokegoapi.api.PokemonGo;
25-
import com.pokegoapi.auth.GoogleCredentialProvider;
2623
import com.pokegoapi.exceptions.LoginFailedException;
2724
import com.pokegoapi.exceptions.RemoteServerException;
2825
import com.pokegoapi.util.Log;
29-
3026
import okhttp3.OkHttpClient;
3127
import okhttp3.RequestBody;
3228
import okhttp3.Response;
@@ -54,7 +50,7 @@ public class RequestHandler {
5450
* @param api the api
5551
* @param client the client
5652
*/
57-
public RequestHandler(PokemonGo api, OkHttpClient client) throws LoginFailedException {
53+
public RequestHandler(PokemonGo api, OkHttpClient client) throws LoginFailedException, RemoteServerException {
5854
this.api = api;
5955
this.client = client;
6056
apiEndpoint = ApiSettings.API_ENDPOINT;
@@ -178,7 +174,7 @@ public void sendServerRequests() throws RemoteServerException, LoginFailedExcept
178174
try {
179175
request.writeTo(stream);
180176
} catch (IOException e) {
181-
Log.wtf(TAG, "Failed to write request to bytearray ouput stream. This should never happen", e);
177+
Log.wtf(TAG, "Failed to write request to bytearray output stream. This should never happen", e);
182178
}
183179

184180
RequestBody body = RequestBody.create(null, stream.toByteArray());
@@ -194,7 +190,7 @@ public void sendServerRequests() throws RemoteServerException, LoginFailedExcept
194190
}
195191

196192
if (response.code() != 200) {
197-
throw new RemoteServerException("Got a unexcepted http code : " + response.code());
193+
throw new RemoteServerException("Got a unexpected http code : " + response.code());
198194
}
199195

200196
ResponseEnvelopeOuterClass.ResponseEnvelope responseEnvelop = null;
@@ -237,14 +233,15 @@ public void sendServerRequests() throws RemoteServerException, LoginFailedExcept
237233
}
238234

239235
@Deprecated
240-
private void resetBuilder() throws LoginFailedException {
236+
private void resetBuilder() throws LoginFailedException, RemoteServerException {
241237
builder = RequestEnvelopeOuterClass.RequestEnvelope.newBuilder();
242238
resetBuilder(builder);
243239
hasRequests = false;
244240
serverRequests.clear();
245241
}
246242

247-
private void resetBuilder(RequestEnvelopeOuterClass.RequestEnvelope.Builder builder) throws LoginFailedException {
243+
private void resetBuilder(RequestEnvelopeOuterClass.RequestEnvelope.Builder builder)
244+
throws LoginFailedException, RemoteServerException {
248245
builder.setStatusCode(2);
249246
builder.setRequestId(8145806132888207460L);
250247
if (lastAuth != null

0 commit comments

Comments
 (0)