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

io.datarouter.httpclient.response.ApiResponseDto Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.httpclient.response;

import java.util.List;
import java.util.Optional;

import org.apache.http.HttpStatus;

import io.datarouter.httpclient.DocumentedGenericHolder;

public class ApiResponseDto implements DocumentedGenericHolder{

	private final T response;
	private final boolean success;
	private final ApiResponseErrorDto error;
	private final int httpStatus;

	public ApiResponseDto(
			T response,
			boolean success,
			ApiResponseErrorDto error,
			int httpStatus){
		this.response = response;
		this.success = success;
		this.error = error;
		this.httpStatus = httpStatus;
	}

	@Override
	public final List getGenericFieldNames(){
		return List.of("response");
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeSuccessResponse(T response){
		return success(response);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeSuccessResponse(T response, int httpServletResponseCode){
		return success(response, httpServletResponseCode);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeCreatedSuccessResponse(T response){
		return createdSuccess(response);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeInternalErrorResponse(String message){
		return internalError(message);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeUnavailableErrorResponse(String message){
		return unavailableError(message);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeBadRequestErrorResponse(String message){
		return badRequestError(message);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeNotFoundErrorResponse(String message){
		return notFoundError(message);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeErrorResponse(String message){
		return error(message);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeErrorResponse(
			String message,
			String code,
			T response,
			int httpStatus){
		return error(message, code, response, httpStatus);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public static  ApiResponseDto makeErrorResponse(String message, T response, int httpStatus){
		return error(message, response, httpStatus);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public final boolean success(){
		return getSuccess();
	}

	public static  ApiResponseDto success(T response){
		return success(response, HttpStatus.SC_OK);
	}

	public static  ApiResponseDto success(T response, int httpServletResponseCode){
		return new ApiResponseDto<>(response, true, null, httpServletResponseCode);
	}

	public static  ApiResponseDto createdSuccess(T response){
		return success(response, HttpStatus.SC_CREATED);
	}

	public static  ApiResponseDto internalError(String message){
		return error(message, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
	}

	public static  ApiResponseDto unavailableError(String message){
		return error(message, null, HttpStatus.SC_SERVICE_UNAVAILABLE);
	}

	public static  ApiResponseDto badRequestError(String message){
		return error(message, null, HttpStatus.SC_BAD_REQUEST);
	}

	public static  ApiResponseDto notFoundError(String message){
		return error(message, null, HttpStatus.SC_NOT_FOUND);
	}

	public static  ApiResponseDto methodNotAllowedError(String message){
		return error(message, null, HttpStatus.SC_METHOD_NOT_ALLOWED);
	}

	public static  ApiResponseDto forbidden(String message){
		return error(message, null, HttpStatus.SC_FORBIDDEN);
	}

	public static  ApiResponseDto error(String message){
		return error(message, null, HttpStatus.SC_FORBIDDEN);
	}

	public static  ApiResponseDto error(
			String message,
			String code,
			T response,
			int httpStatus){
		ApiResponseErrorDto error = new ApiResponseErrorDto<>(message, code, response);
		return new ApiResponseDto<>(null, false, error, httpStatus);
	}

	public static  ApiResponseDto error(String message, T response, int httpStatus){
		return new ApiResponseDto<>(
				null,
				false,
				new ApiResponseErrorDto<>(message, null, response),
				httpStatus);
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public final ApiResponseErrorDto error(){
		return getError();
	}

	public ApiResponseDtoV2 toV2(){
		var errorDtoV2 = Optional.ofNullable(error)
				.map(err -> new ApiResponseErrorDtoV2(err.message, err.code))
				.orElse(null);
		return new ApiResponseDtoV2<>(response, success, errorDtoV2, httpStatus);
	}

	public final T getResponse(){
		return response;
	}

	public final boolean getSuccess(){
		return success;
	}

	public final ApiResponseErrorDto getError(){
		return error;
	}

	public final int getHttpStatus(){
		return httpStatus;
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public final T response(){
		return getResponse();
	}

	/**
	 * @deprecated inline
	 */
	@Deprecated
	public final int httpStatus(){
		return getHttpStatus();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy