7
7
import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
8
8
import com .svix .exceptions .ApiException ;
9
9
10
- import lombok .Getter ;
11
-
12
10
import okhttp3 .*;
13
11
14
12
import java .io .IOException ;
15
13
import java .util .List ;
16
14
import java .util .Map ;
17
- import java .util .Objects ;
18
15
import java .util .concurrent .ThreadLocalRandom ;
19
16
import java .util .concurrent .locks .LockSupport ;
20
17
@@ -24,14 +21,12 @@ public class SvixHttpClient {
24
21
private final List <Long > retrySchedule ;
25
22
private final OkHttpClient client ;
26
23
private final ObjectMapper objectMapper ;
27
- private final boolean debug ;
28
24
29
25
public SvixHttpClient (
30
26
HttpUrl baseUrl , Map <String , String > defaultHeaders , List <Long > retrySchedule ) {
31
27
this .baseUrl = baseUrl ;
32
28
this .defaultHeaders = defaultHeaders ;
33
29
this .retrySchedule = retrySchedule ;
34
- this .debug = Objects .equals (System .getenv ("DEBUG" ), "YES" );
35
30
this .client = new OkHttpClient ();
36
31
37
32
ObjectMapper mapper = new ObjectMapper ();
@@ -77,14 +72,13 @@ public <Req, Res> Res executeRequest(
77
72
String .valueOf (ThreadLocalRandom .current ().nextLong (0 , Long .MAX_VALUE )));
78
73
79
74
Request request = reqBuilder .build ();
80
- Result res = executeRequestWithRetry (request , jsonBody );
81
- Response response = res .getSecond ();
75
+ Response response = executeRequestWithRetry (request , jsonBody );
82
76
83
77
if (response .body () == null ) {
84
78
throw new ApiException ("Body is null" , response .code (), "" );
85
79
}
86
80
87
- String bodyString = res . getFirst (); // response.body().string();
81
+ String bodyString = response .body ().string ();
88
82
89
83
if (response .code () == 204 ) {
90
84
return null ;
@@ -98,10 +92,8 @@ public <Req, Res> Res executeRequest(
98
92
"Non 200 status code: `" + response .code () + "`" , response .code (), bodyString );
99
93
}
100
94
101
- private Result executeRequestWithRetry (Request request , String body ) throws IOException {
102
- dbgReq (request , body );
95
+ private Response executeRequestWithRetry (Request request , String body ) throws IOException {
103
96
Response response = client .newCall (request ).execute ();
104
- String resBody = dbgRes (response );
105
97
106
98
int retryCount = 0 ;
107
99
while (response .code () >= 500 && retryCount < retrySchedule .size ()) {
@@ -114,87 +106,9 @@ private Result executeRequestWithRetry(Request request, String body) throws IOEx
114
106
request .newBuilder ()
115
107
.header ("svix-retry-count" , String .valueOf (retryCount + 1 ))
116
108
.build ();
117
- dbgReq (retryRequest , body );
118
109
response = client .newCall (retryRequest ).execute ();
119
- resBody = dbgRes (response );
120
110
retryCount ++;
121
111
}
122
- return new Result (resBody , response );
123
- }
124
-
125
- private void dbgReq (Request request , String body ) {
126
- StringBuilder dump = new StringBuilder ();
127
- dump .append ("--------- start req ---------\n " );
128
-
129
- // Request line
130
- dump .append (request .method ()).append (" " ).append (request .url ().encodedPath ());
131
-
132
- // Add query parameters if present
133
- if (request .url ().encodedQuery () != null ) {
134
- dump .append ("?" ).append (request .url ().encodedQuery ());
135
- }
136
-
137
- dump .append (" HTTP/1.1\r \n " );
138
-
139
- // Headers
140
- Headers headers = request .headers ();
141
- for (int i = 0 ; i < headers .size (); i ++) {
142
- dump .append (headers .name (i )).append (": " ).append (headers .value (i )).append ("\r \n " );
143
- }
144
-
145
- // Empty line between headers and body
146
- dump .append ("\r \n " );
147
-
148
- // Body
149
- dump .append (body );
150
- if (this .debug ) {
151
- System .out .println (dump .toString ());
152
- }
153
- }
154
-
155
- private String dbgRes (Response response ) throws IOException {
156
- StringBuilder dump = new StringBuilder ();
157
-
158
- // Status line with protocol and status code
159
- dump .append (response .protocol ()) // Will show HTTP/1.1 【1】【2】
160
- .append (" " )
161
- .append (response .code ())
162
- .append (" " )
163
- .append (response .message ())
164
- .append ("\r \n " );
165
-
166
- // Headers
167
- Headers headers = response .headers ();
168
- for (int i = 0 ; i < headers .size (); i ++) {
169
- dump .append (headers .name (i )).append (": " ).append (headers .value (i )).append ("\r \n " );
170
- }
171
-
172
- // Empty line between headers and body
173
- dump .append ("\r \n " );
174
-
175
- // Body - note that response body can only be consumed once 【3】【2】
176
- ResponseBody body = response .body ();
177
- String bodyString = "" ;
178
- if (body != null ) {
179
- bodyString = body .string ();
180
- dump .append (bodyString );
181
- }
182
- dump .append ("\n --------- end req ---------" );
183
- if (this .debug ) {
184
- System .out .println (dump .toString ());
185
- }
186
- return bodyString ;
187
- }
188
-
189
- @ Getter
190
- static class Result {
191
- // Getters
192
- private final String first ;
193
- private final Response second ;
194
-
195
- public Result (String first , Response second ) {
196
- this .first = first ;
197
- this .second = second ;
198
- }
112
+ return response ;
199
113
}
200
114
}
0 commit comments