429 handling
This commit is contained in:
parent
1de6e05cfa
commit
add7f5ece2
|
@ -132,34 +132,62 @@ public class TelegramAPI implements Runnable {
|
|||
|
||||
public JSONObject request(String request, String parameter, boolean logging) throws IOException {
|
||||
//do https stuff
|
||||
URL url = new URL(API_URL + apikey + "/" + request);
|
||||
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
|
||||
con.setDoInput(true);
|
||||
con.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
||||
wr.write(parameter);
|
||||
wr.flush();
|
||||
boolean toomany = true; //für retry after 429 error
|
||||
int trycount = 0;
|
||||
while(toomany) {
|
||||
toomany = false;
|
||||
++trycount;
|
||||
URL url = new URL(API_URL + apikey + "/" + request);
|
||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||
con.setDoInput(true);
|
||||
con.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
||||
wr.write(parameter);
|
||||
wr.flush();
|
||||
|
||||
if(logging) {
|
||||
String small = parameter;
|
||||
if(small.length() > 60) {
|
||||
small = small.substring(0, Math.min(60, small.length())) + "...";
|
||||
if (logging) {
|
||||
String small = parameter;
|
||||
if (small.length() > 60) {
|
||||
small = small.substring(0, Math.min(60, small.length())) + "...";
|
||||
}
|
||||
log.log("request: " + request + " content " + small + " -> " + con.getResponseCode() + ", " + con.getResponseMessage());
|
||||
}
|
||||
log.log("request: " + request + " content " + small + " -> " + con.getResponseCode() + ", " + con.getResponseMessage());
|
||||
}
|
||||
int response = con.getResponseCode();
|
||||
if (response == 200) {
|
||||
return new JSONObject(readfromIS(con.getInputStream()));
|
||||
} else {
|
||||
System.out.println("request " + request + " " + parameter);
|
||||
String errdesc = "[No description available]";
|
||||
try {
|
||||
//try to read error message
|
||||
JSONObject returned = new JSONObject(readfromIS(con.getErrorStream()));
|
||||
errdesc = returned.getString("description");
|
||||
} catch (Throwable ignore) {
|
||||
}
|
||||
|
||||
if(con.getResponseCode() == 200) {
|
||||
return new JSONObject(readfromIS(con.getInputStream()));
|
||||
} else {
|
||||
System.out.println("request " + request + " " + parameter);
|
||||
String errdesc = "[No description available]";
|
||||
try {
|
||||
//try to read error message
|
||||
JSONObject returned = new JSONObject(readfromIS(con.getErrorStream()));
|
||||
errdesc = returned.getString("description");
|
||||
} catch(Throwable ignore) { }
|
||||
throw new APIError(parameter, request, con.getResponseCode(), null, errdesc);
|
||||
//throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||
//catch 429 too many error
|
||||
if (response == 429) {
|
||||
toomany = true;
|
||||
if(trycount > 10) break;
|
||||
|
||||
//try to read timeout
|
||||
//too Many Requests: retry after 19
|
||||
int timeout = 10;
|
||||
int idx = errdesc.lastIndexOf(" ");
|
||||
try {
|
||||
timeout = Integer.parseInt(errdesc.substring(idx))+1;
|
||||
System.out.println("timeout read: " + timeout);
|
||||
} catch(NumberFormatException | StringIndexOutOfBoundsException e ) {}
|
||||
try {
|
||||
System.out.println("Got 429 -> sleep for " + timeout + " seconds");
|
||||
Thread.sleep(timeout * 1000);
|
||||
} catch(InterruptedException e) {}
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new APIError(parameter, request, con.getResponseCode(), null, errdesc);
|
||||
//throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue