429 handling
This commit is contained in:
parent
1de6e05cfa
commit
add7f5ece2
|
@ -132,6 +132,11 @@ public class TelegramAPI implements Runnable {
|
||||||
|
|
||||||
public JSONObject request(String request, String parameter, boolean logging) throws IOException {
|
public JSONObject request(String request, String parameter, boolean logging) throws IOException {
|
||||||
//do https stuff
|
//do https stuff
|
||||||
|
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);
|
URL url = new URL(API_URL + apikey + "/" + request);
|
||||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||||
con.setDoInput(true);
|
con.setDoInput(true);
|
||||||
|
@ -147,8 +152,8 @@ public class TelegramAPI implements Runnable {
|
||||||
}
|
}
|
||||||
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(con.getResponseCode() == 200) {
|
if (response == 200) {
|
||||||
return new JSONObject(readfromIS(con.getInputStream()));
|
return new JSONObject(readfromIS(con.getInputStream()));
|
||||||
} else {
|
} else {
|
||||||
System.out.println("request " + request + " " + parameter);
|
System.out.println("request " + request + " " + parameter);
|
||||||
|
@ -157,11 +162,34 @@ public class TelegramAPI implements Runnable {
|
||||||
//try to read error message
|
//try to read error message
|
||||||
JSONObject returned = new JSONObject(readfromIS(con.getErrorStream()));
|
JSONObject returned = new JSONObject(readfromIS(con.getErrorStream()));
|
||||||
errdesc = returned.getString("description");
|
errdesc = returned.getString("description");
|
||||||
} catch(Throwable ignore) { }
|
} catch (Throwable ignore) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//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 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);
|
//throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected String readfromIS(InputStream is) {
|
protected String readfromIS(InputStream is) {
|
||||||
Scanner s = new Scanner(is);
|
Scanner s = new Scanner(is);
|
||||||
|
|
Loading…
Reference in New Issue