package io.ljunggren.usps.api.model;
public enum HttpStatusCode {
OK(200, "OK response"),
BAD_REQUEST(400, "Bad request"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not found"),
THROTTLING(429, "Too many requests"),
INVALID_INPUT(422, "Invalid input"),
SERVER_ERROR(500, "Server error"),
SERVICE_UNAVAILABLE(503, "Service unavailable");
private int code;
private String text;
HttpStatusCode(int code, String text) {
this.code = code;
this.text = text;
}
public int getCode() {
return code;
}
public String getText() {
return text;
}
public boolean isRetryable() {
return this == SERVICE_UNAVAILABLE || this == THROTTLING || this == SERVER_ERROR;
}
public static HttpStatusCode getByCode(int code) {
for (HttpStatusCode responseCode : HttpStatusCode.values()) {
if (responseCode.getCode() == code) {
return responseCode;
}
}
return null;
}
public static String getTextByCode(int code) {
HttpStatusCode responseCode = getByCode(code);
return responseCode == null ? null : responseCode.getText();
}
}