io.datarouter.httpclient.response.ApiResponseDtoV2 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 java.util.function.Function;
import org.apache.http.HttpStatus;
import io.datarouter.httpclient.DocumentedGenericHolder;
public record ApiResponseDtoV2(
T response,
boolean success,
ApiResponseErrorDtoV2 error,
int httpStatus)
implements DocumentedGenericHolder{
@Override
public List getGenericFieldNames(){
return List.of("response");
}
public static ApiResponseDtoV2 successResponse(T response){
return successResponse(response, HttpStatus.SC_OK);
}
public static ApiResponseDtoV2 successResponse(T response, int httpsStatus){
return new ApiResponseDtoV2<>(response, true, null, httpsStatus);
}
public static ApiResponseDtoV2 createdSuccess(T response){
return successResponse(response, HttpStatus.SC_CREATED);
}
public static ApiResponseDtoV2 internalError(String message){
return errorResponse(message, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
public static ApiResponseDtoV2 unavailableError(String message){
return errorResponse(message, null, HttpStatus.SC_SERVICE_UNAVAILABLE);
}
public static ApiResponseDtoV2 badRequestError(String message){
return errorResponse(message, null, HttpStatus.SC_BAD_REQUEST);
}
public static ApiResponseDtoV2 notFoundError(String message){
return errorResponse(message, null, HttpStatus.SC_NOT_FOUND);
}
public static ApiResponseDtoV2 forbidden(String message){
return errorResponse(message, null, HttpStatus.SC_FORBIDDEN);
}
public static ApiResponseDtoV2 errorResponse(
String message,
String code,
int httpStatus){
ApiResponseErrorDtoV2 error = new ApiResponseErrorDtoV2(message, code);
return new ApiResponseDtoV2<>(null, false, error, httpStatus);
}
public ApiResponseDtoV2 map(
Function super T,? extends U> successMapper){
if(success){
return new ApiResponseDtoV2<>(successMapper.apply(response), true, error, httpStatus);
}
return new ApiResponseDtoV2<>(null, false, error, httpStatus);
}
public Optional getResponse(){
return Optional.ofNullable(response);
}
public Optional getError(){
return Optional.ofNullable(error);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy