org.zodiac.okhttp.ResponseSpec Maven / Gradle / Ivy
The newest version!
package org.zodiac.okhttp;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.*;
import javax.annotation.Nullable;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
/**
* 相应接口
*
*/
public interface ResponseSpec {
/**
* Returns the HTTP code.
*
* @return code
*/
int code();
/**
* Returns the HTTP status message.
*
* @return message
*/
String message();
/**
* Returns the HTTP isSuccessful.
*
* @return boolean
*/
default boolean isOk() {
return false;
}
/**
* Returns the is Redirect.
*
* @return is Redirect
*/
boolean isRedirect();
/**
* Returns the Headers.
*
* @return Headers
*/
Headers headers();
/**
* Headers Consumer.
*
* @param consumer Consumer
* @return Headers
*/
default ResponseSpec headers(Consumer consumer) {
consumer.accept(this.headers());
return this;
}
/**
* Returns the Cookies.
*
* @return Cookie List
*/
List cookies();
/**
* 读取消费 cookie。
*
* @param consumer Consumer
* @return ResponseSpec
*/
default ResponseSpec cookies(Consumer> consumer) {
consumer.accept(this.cookies());
return this;
}
/**
* Returns body String.
*
* @return body String
*/
String asString();
/**
* Returns body to byte arrays.
*
* @return byte arrays
*/
byte[] asBytes();
/**
* Returns body to InputStream.
*
* @return InputStream
*/
InputStream asStream();
/**
* Returns body to JsonNode.
*
* @return JsonNode
*/
JsonNode asJsonNode();
/**
* Returns body to Object.
*
* @param type
* @param valueType value value type
* @return Object
*/
@Nullable
T asValue(Class valueType);
/**
* Returns body to Object.
*
* @param type
* @param typeReference value Type Reference
* @return Object
*/
@Nullable
T asValue(TypeReference typeReference);
/**
* Returns body to List.
*
* @param type
* @param valueType value type
* @return List
*/
List asList(Class valueType);
/**
* Returns body to Map.
*
* @param key type
* @param value type
* @param keyClass key type
* @param valueType value type
* @return Map
*/
Map asMap(Class> keyClass, Class> valueType);
/**
* Returns body to Map.
*
* @param value type
* @param valueType value 类型
* @return Map
*/
Map asMap(Class> valueType);
/**
* 将 xml、heml 转成对象。
*
* @param valueType 对象类
* @param 泛型
* @return 对象
*/
T asDomValue(Class valueType);
/**
* 将 xml、heml 转成对象。
*
* @param valueType 对象类
* @param 泛型
* @return 对象集合
*/
List asDomList(Class valueType);
/**
* toFile.
*
* @param file File
* @return {@link File}
*/
File toFile(File file);
/**
* toFile.
*
* @param path Path
* @return {@link Path}
*/
Path toFile(Path path);
/**
* Returns contentType.
*
* @return contentType
*/
@Nullable
MediaType contentType();
/**
* Returns contentLength.
*
* @return contentLength
*/
long contentLength();
/**
* Returns rawRequest.
*
* @return Request
*/
Request rawRequest();
/**
* rawRequest Consumer.
*
* @param consumer Consumer
* @return ResponseSpec
*/
@Nullable
default ResponseSpec rawRequest(Consumer consumer) {
consumer.accept(this.rawRequest());
return this;
}
/**
* Returns rawResponse.
*
* @return Response
*/
Response rawResponse();
/**
* rawResponse Consumer.
*
* @param consumer Consumer
* @return Response
*/
default ResponseSpec rawResponse(Consumer consumer) {
consumer.accept(this.rawResponse());
return this;
}
/**
* Returns rawBody.
*
* @return ResponseBody
*/
@Nullable
ResponseBody rawBody();
/**
* rawBody Consumer.
*
* @param consumer Consumer
* @return ResponseBody
*/
@Nullable
default ResponseSpec rawBody(Consumer consumer) {
consumer.accept(this.rawBody());
return this;
}
}