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 {
|
public JSONObject request(String request, String parameter, boolean logging) throws IOException {
|
||||||
//do https stuff
|
//do https stuff
|
||||||
URL url = new URL(API_URL + apikey + "/" + request);
|
boolean toomany = true; //für retry after 429 error
|
||||||
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
|
int trycount = 0;
|
||||||
con.setDoInput(true);
|
while(toomany) {
|
||||||
con.setDoOutput(true);
|
toomany = false;
|
||||||
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
++trycount;
|
||||||
wr.write(parameter);
|
URL url = new URL(API_URL + apikey + "/" + request);
|
||||||
wr.flush();
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||||
|
con.setDoInput(true);
|
||||||
|
con.setDoOutput(true);
|
||||||
|
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
|
||||||
|
wr.write(parameter);
|
||||||
|
wr.flush();
|
||||||
|
|
||||||
if(logging) {
|
if (logging) {
|
||||||
String small = parameter;
|
String small = parameter;
|
||||||
if(small.length() > 60) {
|
if (small.length() > 60) {
|
||||||
small = small.substring(0, Math.min(60, small.length())) + "...";
|
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) {
|
//catch 429 too many error
|
||||||
return new JSONObject(readfromIS(con.getInputStream()));
|
if (response == 429) {
|
||||||
} else {
|
toomany = true;
|
||||||
System.out.println("request " + request + " " + parameter);
|
if(trycount > 10) break;
|
||||||
String errdesc = "[No description available]";
|
|
||||||
try {
|
//try to read timeout
|
||||||
//try to read error message
|
//too Many Requests: retry after 19
|
||||||
JSONObject returned = new JSONObject(readfromIS(con.getErrorStream()));
|
int timeout = 10;
|
||||||
errdesc = returned.getString("description");
|
int idx = errdesc.lastIndexOf(" ");
|
||||||
} catch(Throwable ignore) { }
|
try {
|
||||||
throw new APIError(parameter, request, con.getResponseCode(), null, errdesc);
|
timeout = Integer.parseInt(errdesc.substring(idx))+1;
|
||||||
//throw new IOException("API request returned HTTP " + con.getResponseCode() + " (" + con.getResponseMessage() + ") for action " + request + "\nurl: " + url.toString() + "\nparams: " + parameter + "\nerror description: " + errdesc);
|
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