Skip to content

Commit

Permalink
♻️ refactor: refactor HTTPStatusBuilder and WrapResponse #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jun 6, 2024
1 parent 0cd2444 commit 6241296
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
public class HttpStatusBuilder {
protected static List<HttpResponse> statuses;

/**
* @param statusCode - the status code
* @return new HTTP wrapper response, class {@link HttpResponse}
*/
public static HttpResponse valueOf(int statusCode) {
if (Collection4j.isEmpty(statuses)) {
statuses = Arrays.asList(
Expand Down Expand Up @@ -96,6 +100,58 @@ public static HttpResponse valueOf(int statusCode) {
return statuses.stream().filter(e -> e.getCode() == statusCode).findFirst().orElse(new HttpResponse(-1, "unknown", "unknown", "out of HTTP status version"));
}

/**
* Checks if the given HTTP status code indicates a successful response.
* <p>
* A successful response is indicated by status codes in the range 200 to 299.
* This includes statuses like 200 OK, 201 Created, 204 No Content, etc.
*
* @param code the HTTP status code to check
* @return true if the code is between 200 and 299 (inclusive), false otherwise
*/
public static boolean isSuccess(int code) {
return (200 <= code) && (code <= 299);
}

/**
* Checks if the given HTTP status code indicates a redirection response.
* <p>
* A redirection response is indicated by status codes in the range 300 to 399.
* This includes statuses like 301 Moved Permanently, 302 Found, 304 Not Modified, etc.
*
* @param code the HTTP status code to check
* @return true if the code is between 300 and 399 (inclusive), false otherwise
*/
public static boolean isRedirection(int code) {
return (300 <= code) && (code <= 399);
}

/**
* Checks if the given HTTP status code indicates a client error response.
* <p>
* A client error response is indicated by status codes in the range 400 to 499.
* This includes statuses like 400 Bad Request, 401 Unauthorized, 404 Not Found, etc.
*
* @param code the HTTP status code to check
* @return true if the code is between 400 and 499 (inclusive), false otherwise
*/
public static boolean isClientError(int code) {
return (400 <= code) && (code <= 499);
}

/**
* Checks if the given HTTP status code indicates a server error response.
* <p>
* A server error response is indicated by status codes in the range 500 to 599.
* This includes statuses like 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, etc.
*
* @param code the HTTP status code to check
* @return true if the code is between 500 and 599 (inclusive), false otherwise
*/
public static boolean isServerError(int code) {
return (500 <= code) && (code <= 599);
}

// 1xx Informational responses
public static final HttpResponse CONTINUE = new HttpResponse(100, "Continue", "Informational", "");
public static final HttpResponse SWITCHING_PROTOCOLS = new HttpResponse(101, "Switching Protocols", "Informational", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ public void setPagination(PaginationResponse pagination) {

@JsonIgnore
public boolean isError() {
return this.errors != null;
return this.errors != null ||
HttpStatusBuilder.isClientError(this.statusCode) ||
HttpStatusBuilder.isServerError(this.statusCode);
}

@JsonIgnore
public boolean isSuccess() {
return this.statusCode >= HttpStatusBuilder.OK.getCode() && this.statusCode < HttpStatusBuilder.MULTIPLE_CHOICES.getCode();
return HttpStatusBuilder.isSuccess(this.statusCode);
}

public String getPath() {
Expand Down

0 comments on commit 6241296

Please sign in to comment.