ResponseSpec.java 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019-2029, Dreamlu (596392912@qq.com & www.dreamlu.net).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * 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.
 */

17
package net.dreamlu.mica.http;
18 19 20

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
21 22
import com.fasterxml.jackson.databind.type.CollectionLikeType;
import net.dreamlu.mica.core.utils.JsonUtil;
23 24
import okhttp3.*;

25
import javax.annotation.Nullable;
26 27
import java.io.File;
import java.io.InputStream;
28
import java.nio.charset.Charset;
29 30 31
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
32
import java.util.function.Consumer;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/**
 * 相应接口
 *
 * @author L.cm
 */
public interface ResponseSpec {

	/**
	 * Returns the HTTP code.
	 *
	 * @return code
	 */
	int code();

	/**
	 * Returns the HTTP status message.
	 *
	 * @return message
	 */
	String message();

55 56 57 58 59
	/**
	 * Returns the HTTP isSuccessful.
	 *
	 * @return boolean
	 */
60 61 62
	default boolean isOk() {
		return false;
	}
63

64 65 66 67 68 69 70 71 72
	/**
	 * Returns the HTTP is not successful.
	 *
	 * @return boolean
	 */
	default boolean isNotOk() {
		return !isOk();
	}

73 74 75 76 77 78 79 80 81 82 83 84 85 86
	/**
	 * Returns the is Redirect.
	 *
	 * @return is Redirect
	 */
	boolean isRedirect();

	/**
	 * Returns the Headers.
	 *
	 * @return Headers
	 */
	Headers headers();

87 88 89 90 91 92 93 94 95 96 97
	/**
	 * Headers Consumer.
	 *
	 * @param consumer Consumer
	 * @return Headers
	 */
	default ResponseSpec headers(Consumer<Headers> consumer) {
		consumer.accept(this.headers());
		return this;
	}

98 99 100 101 102 103 104
	/**
	 * Returns the Cookies.
	 *
	 * @return Cookie List
	 */
	List<Cookie> cookies();

105 106 107 108 109 110 111 112 113 114 115
	/**
	 * 读取消费 cookie
	 *
	 * @param consumer Consumer
	 * @return ResponseSpec
	 */
	default ResponseSpec cookies(Consumer<List<Cookie>> consumer) {
		consumer.accept(this.cookies());
		return this;
	}

116 117 118 119 120 121 122
	/**
	 * Returns body String.
	 *
	 * @return body String
	 */
	String asString();

123 124 125 126 127 128 129 130
	/**
	 * Returns body String.
	 *
	 * @param charset Charset
	 * @return body String
	 */
	String asString(Charset charset);

131 132 133 134 135 136 137 138 139 140 141 142
	/**
	 * Returns body to byte arrays.
	 *
	 * @return byte arrays
	 */
	byte[] asBytes();

	/**
	 * Returns body to InputStream.
	 *
	 * @return InputStream
	 */
143
	InputStream asStream();
144 145 146 147 148 149 150 151

	/**
	 * Returns body to JsonNode.
	 *
	 * @return JsonNode
	 */
	JsonNode asJsonNode();

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	/**
	 * jackson json path 语法读取节点
	 *
	 * @param jsonPtrExpr json path 表达式
	 * @return JsonNode
	 */
	default JsonNode atJsonPath(String jsonPtrExpr) {
		return this.asJsonNode().at(jsonPtrExpr);
	}

	/**
	 * jackson json path 语法读取节点
	 *
	 * @param jsonPtrExpr json path 表达式
	 * @param valueType   value value type
	 * @return JsonNode
	 */
	default <T> T atJsonPathValue(String jsonPtrExpr, Class<T> valueType) {
		return JsonUtil.convertValue(atJsonPath(jsonPtrExpr), valueType);
	}

	/**
	 * jackson json path 语法读取节点
	 *
	 * @param jsonPtrExpr   json path 表达式
	 * @param typeReference value Type Reference
	 * @return JsonNode
	 */
	default <T> T atJsonPathValue(String jsonPtrExpr, TypeReference<T> typeReference) {
		return JsonUtil.convertValue(atJsonPath(jsonPtrExpr), typeReference);
	}

	/**
	 * jackson json path 语法读取节点
	 *
	 * @param jsonPtrExpr json path 表达式
	 * @param valueType   value value type
	 * @return List
	 */
	default <T> List<T> atJsonPathList(String jsonPtrExpr, Class<T> valueType) {
		CollectionLikeType collectionLikeType = JsonUtil.getListType(valueType);
		return JsonUtil.convertValue(atJsonPath(jsonPtrExpr), collectionLikeType);
	}

196 197 198 199 200 201
	/**
	 * Returns body to Object.
	 *
	 * @param valueType value value type
	 * @return Object
	 */
202
	@Nullable
203
	<T> T asValue(Class<T> valueType);
204 205 206 207 208 209 210

	/**
	 * Returns body to Object.
	 *
	 * @param typeReference value Type Reference
	 * @return Object
	 */
211
	@Nullable
如梦技术's avatar
如梦技术 已提交
212
	<T> T asValue(TypeReference<T> typeReference);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

	/**
	 * Returns body to List.
	 *
	 * @param valueType value type
	 * @return List
	 */
	<T> List<T> asList(Class<T> valueType);

	/**
	 * Returns body to Map.
	 *
	 * @param keyClass  key type
	 * @param valueType value type
	 * @return Map
	 */
	<K, V> Map<K, V> asMap(Class<?> keyClass, Class<?> valueType);

	/**
	 * Returns body to Map.
	 *
	 * @param valueType value 类型
	 * @return Map
	 */
	<V> Map<String, V> asMap(Class<?> valueType);

	/**
	 * toFile.
	 *
	 * @param file File
243
	 * @return File
244
	 */
245
	File toFile(File file);
246 247 248 249 250

	/**
	 * toFile.
	 *
	 * @param path Path
251
	 * @return Path
252
	 */
253
	Path toFile(Path path);
254 255 256 257 258 259

	/**
	 * Returns contentType.
	 *
	 * @return contentType
	 */
260
	@Nullable
261 262 263 264 265 266 267 268 269
	MediaType contentType();

	/**
	 * Returns contentLength.
	 *
	 * @return contentLength
	 */
	long contentLength();

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	/**
	 * Returns rawRequest.
	 *
	 * @return Request
	 */
	Request rawRequest();

	/**
	 * rawRequest Consumer.
	 *
	 * @param consumer Consumer
	 * @return ResponseSpec
	 */
	@Nullable
	default ResponseSpec rawRequest(Consumer<Request> consumer) {
		consumer.accept(this.rawRequest());
		return this;
	}

289 290 291 292 293 294 295
	/**
	 * Returns rawResponse.
	 *
	 * @return Response
	 */
	Response rawResponse();

296 297 298 299 300 301 302 303 304 305 306
	/**
	 * rawResponse Consumer.
	 *
	 * @param consumer Consumer
	 * @return Response
	 */
	default ResponseSpec rawResponse(Consumer<Response> consumer) {
		consumer.accept(this.rawResponse());
		return this;
	}

307 308 309 310 311
	/**
	 * Returns rawBody.
	 *
	 * @return ResponseBody
	 */
312
	@Nullable
313
	ResponseBody rawBody();
314 315 316 317 318 319 320 321 322 323 324 325

	/**
	 * rawBody Consumer.
	 *
	 * @param consumer Consumer
	 * @return ResponseBody
	 */
	@Nullable
	default ResponseSpec rawBody(Consumer<ResponseBody> consumer) {
		consumer.accept(this.rawBody());
		return this;
	}
326

327
}