ludc
2023-03-16 86b6157299a50579f454e4fb45a09ff21d252dab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package org.springblade.core.http;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import okhttp3.Call;
import okhttp3.Request;
import org.springblade.core.tool.utils.Exceptions;
 
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
 
/**
 * Exchange
 *
 * @author L.cm
 */
@RequiredArgsConstructor
public class Exchange {
    private BiConsumer<Request, IOException> failedBiConsumer = (r, e) -> {};
    private final Call call;
 
    public Exchange onFailed(BiConsumer<Request, IOException> failConsumer) {
        this.failedBiConsumer = failConsumer;
        return this;
    }
 
    public <R> R onResponse(Function<ResponseSpec, R> func) {
        try (HttpResponse response = new HttpResponse(call.execute())) {
            return func.apply(response);
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    @Nullable
    public <R> R onSuccess(Function<ResponseSpec, R> func) {
        try (HttpResponse response = new HttpResponse(call.execute())) {
            return func.apply(response);
        } catch (IOException e) {
            failedBiConsumer.accept(call.request(), e);
            return null;
        }
    }
 
    @Nullable
    public <R> R onSuccessful(Function<ResponseSpec, R> func) {
        try (HttpResponse response = new HttpResponse(call.execute())) {
            if (response.isOk()) {
                return func.apply(response);
            } else {
                failedBiConsumer.accept(call.request(), new IOException(response.toString()));
            }
        } catch (IOException e) {
            failedBiConsumer.accept(call.request(), e);
        }
        return null;
    }
 
    public <R> Optional<R> onSuccessOpt(Function<ResponseSpec, R> func) {
        return Optional.ofNullable(this.onSuccess(func));
    }
 
    public <R> Optional<R> onSuccessfulOpt(Function<ResponseSpec, R> func) {
        return Optional.ofNullable(this.onSuccessful(func));
    }
 
    /**
     * Returns body String.
     *
     * @return body String
     */
    public String asString() {
        return onResponse(ResponseSpec::asString);
    }
 
    /**
     * Returns body to byte arrays.
     *
     * @return byte arrays
     */
    public byte[] asBytes() {
        return onResponse(ResponseSpec::asBytes);
    }
 
    /**
     * Returns body to JsonNode.
     *
     * @return JsonNode
     */
    public JsonNode asJsonNode() {
        return onResponse(ResponseSpec::asJsonNode);
    }
 
    /**
     * Returns body to Object.
     *
     * @param valueType value value type
     * @return Object
     */
    public <T> T asValue(Class<T> valueType) {
        return onResponse(responseSpec -> responseSpec.asValue(valueType));
    }
 
    /**
     * Returns body to Object.
     *
     * @param typeReference value Type Reference
     * @return Object
     */
    public <T> T asValue(TypeReference<T> typeReference) {
        return onResponse(responseSpec -> responseSpec.asValue(typeReference));
    }
 
    /**
     * Returns body to List.
     *
     * @param valueType value type
     * @return List
     */
    public <T> List<T> asList(Class<T> valueType) {
        return onResponse(responseSpec -> responseSpec.asList(valueType));
    }
 
    /**
     * Returns body to Map.
     *
     * @param keyClass  key type
     * @param valueType value type
     * @return Map
     */
    public <K, V> Map<K, V> asMap(Class<?> keyClass, Class<?> valueType) {
        return onResponse(responseSpec -> responseSpec.asMap(keyClass, valueType));
    }
 
    /**
     * Returns body to Map.
     *
     * @param valueType value 类型
     * @return Map
     */
    public <V> Map<String, V> asMap(Class<?> valueType) {
        return onResponse(responseSpec -> responseSpec.asMap(valueType));
    }
 
    /**
     * 将 xml、heml 转成对象
     *
     * @param valueType 对象类
     * @param <T>       泛型
     * @return 对象
     */
    public <T> T asDomValue(Class<T> valueType) {
        return onResponse(responseSpec -> responseSpec.asDomValue(valueType));
    }
 
    /**
     * 将 xml、heml 转成对象
     *
     * @param valueType 对象类
     * @param <T>       泛型
     * @return 对象集合
     */
    public <T> List<T> asDomList(Class<T> valueType) {
        return onResponse(responseSpec -> responseSpec.asDomList(valueType));
    }
 
    /**
     * toFile.
     *
     * @param file File
     */
    public File toFile(File file) {
        return onResponse(responseSpec -> responseSpec.toFile(file));
    }
 
    /**
     * toFile.
     *
     * @param path Path
     */
    public Path toFile(Path path) {
        return onResponse(responseSpec -> responseSpec.toFile(path));
    }
 
}