use of com.pokegoapi.exceptions.request.HashLimitExceededException in project PokeGOAPI-Java by Grover-c13.
the class PokeHashProvider method provide.
/**
* Provides a hash for the given arguments
*
* @param timestamp timestamp to hash
* @param latitude latitude to hash
* @param longitude longitude to hash
* @param altitude altitude to hash
* @param authTicket auth ticket to hash
* @param sessionData session data to hash
* @param requests request data to hash
* @return the hash provider
* @throws HashException if an exception occurs while providing this hash
*/
@Override
public Hash provide(long timestamp, double latitude, double longitude, double altitude, byte[] authTicket, byte[] sessionData, byte[][] requests) throws HashException {
if (key.hasTested()) {
if (awaitRequests) {
try {
key.await();
} catch (InterruptedException e) {
throw new HashException(e);
}
} else {
long time = System.currentTimeMillis();
long timeLeft = time - key.getRatePeriodEnd();
if (key.getRequestsRemaining() <= 0 && timeLeft > 0) {
throw new HashLimitExceededException("Exceeded hash request limit! Period ends in " + timeLeft + "ms");
}
}
}
Request request = new Request(latitude, longitude, altitude, timestamp, authTicket, sessionData, requests);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("X-AuthToken", key.getKey());
connection.setRequestProperty("content-type", "application/json");
connection.setRequestProperty("User-Agent", "PokeGOAPI-Java");
connection.setDoOutput(true);
String requestJSON = MOSHI.adapter(Request.class).toJson(request);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(requestJSON);
out.flush();
out.close();
int responseCode = connection.getResponseCode();
this.key.setProperties(connection);
String error = getError(connection);
switch(responseCode) {
case HttpURLConnection.HTTP_OK:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
builder.append(line);
}
in.close();
Response response = MOSHI.adapter(Response.class).fromJson(builder.toString());
long locationAuth = response.getLocationAuthHash();
long location = response.getLocationHash();
int locationAuthHash = (int) ((locationAuth & 0xFFFFFFFFL) ^ (locationAuth >>> 32));
int locationHash = (int) ((location & 0xFFFFFFFFL) ^ (location >>> 32));
return new Hash(locationAuthHash, locationHash, response.getRequestHashes());
case HttpURLConnection.HTTP_BAD_REQUEST:
if (error.length() > 0) {
throw new HashException(error);
}
throw new HashException("Bad hash request!");
case HttpURLConnection.HTTP_UNAUTHORIZED:
if (error.length() > 0) {
throw new HashException(error);
}
throw new HashException("Unauthorized hash request!");
case 429:
if (awaitRequests) {
try {
key.await();
return provide(timestamp, latitude, longitude, altitude, authTicket, sessionData, requests);
} catch (InterruptedException e) {
throw new HashException(e);
}
} else {
if (error.length() > 0) {
throw new HashLimitExceededException(error);
}
throw new HashLimitExceededException("Exceeded hash limit!");
}
case 404:
throw new HashException("Unknown hashing endpoint! \"" + this.endpoint + "\"");
default:
if (error.length() > 0) {
throw new HashException(error + " (" + responseCode + ")");
}
throw new HashException("Received unknown response code! (" + responseCode + ")");
}
} catch (IOException e) {
throw new HashException("Failed to perform PokeHash request", e);
}
}
Aggregations