All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kohsuke.github.AbuseLimitHandler Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha-2
Show newest version
package org.kohsuke.github;

import org.kohsuke.github.connector.GitHubConnectorResponse;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import javax.annotation.Nonnull;

// TODO: Auto-generated Javadoc
/**
 * Pluggable strategy to determine what to do when the API abuse limit is hit.
 *
 * @author Kohsuke Kawaguchi
 * @see GitHubBuilder#withAbuseLimitHandler(GitHubAbuseLimitHandler)
 *      GitHubBuilder#withAbuseLimitHandler(GitHubAbuseLimitHandler)
 * @see documentation
 * @see RateLimitHandler
 * @deprecated Switch to {@link GitHubAbuseLimitHandler}.
 */
@Deprecated
public abstract class AbuseLimitHandler extends GitHubAbuseLimitHandler {

    /**
     * Create default AbuseLimitHandler instance
     */
    public AbuseLimitHandler() {
    }

    /**
     * Called when the library encounters HTTP error indicating that the API abuse limit is reached.
     *
     * 

* Any exception thrown from this method will cause the request to fail, and the caller of github-api will receive * an exception. If this method returns normally, another request will be attempted. For that to make sense, the * implementation needs to wait for some time. * * @param connectorResponse * Response information for this request. * @throws IOException * on failure * @see API documentation from GitHub * @see Dealing * with abuse rate limits * */ public void onError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException { GHIOException e = new HttpException("Abuse limit reached", connectorResponse.statusCode(), connectorResponse.header("Status"), connectorResponse.request().url().toString()).withResponseHeaderFields(connectorResponse.allHeaders()); onError(e, connectorResponse.toHttpURLConnection()); } /** * Called when the library encounters HTTP error indicating that the API abuse limit is reached. * *

* Any exception thrown from this method will cause the request to fail, and the caller of github-api will receive * an exception. If this method returns normally, another request will be attempted. For that to make sense, the * implementation needs to wait for some time. * * @param e * Exception from Java I/O layer. If you decide to fail the processing, you can throw this exception (or * wrap this exception into another exception and throw it). * @param uc * Connection that resulted in an error. Useful for accessing other response headers. * @throws IOException * on failure * @see API documentation from GitHub * @see Dealing * with abuse rate limits * */ @Deprecated public abstract void onError(IOException e, HttpURLConnection uc) throws IOException; /** * Wait until the API abuse "wait time" is passed. */ @Deprecated public static final AbuseLimitHandler WAIT = new AbuseLimitHandler() { @Override public void onError(IOException e, HttpURLConnection uc) throws IOException { try { Thread.sleep(parseWaitTime(uc)); } catch (InterruptedException ex) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } } }; /** * Fail immediately. */ @Deprecated public static final AbuseLimitHandler FAIL = new AbuseLimitHandler() { @Override public void onError(IOException e, HttpURLConnection uc) throws IOException { throw e; } }; // If "Retry-After" missing, wait for unambiguously over one minute per GitHub guidance static long DEFAULT_WAIT_MILLIS = 61 * 1000; /* * Exposed for testability. Given an http response, find the retry-after header field and parse it as either a * number or a date (the spec allows both). If no header is found, wait for a reasonably amount of time. */ long parseWaitTime(HttpURLConnection uc) { String v = uc.getHeaderField("Retry-After"); if (v == null) { // can't tell, wait for unambiguously over one minute per GitHub guidance return DEFAULT_WAIT_MILLIS; } try { return Math.max(1000, Long.parseLong(v) * 1000); } catch (NumberFormatException nfe) { // The retry-after header could be a number in seconds, or an http-date ZonedDateTime zdt = ZonedDateTime.parse(v, DateTimeFormatter.RFC_1123_DATE_TIME); return ChronoUnit.MILLIS.between(ZonedDateTime.now(), zdt); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy