1
1
package io .codef .api ;
2
2
3
- import static io .codef .api .dto .EasyCodefRequest .ACCESS_TOKEN ;
4
- import static io .codef .api .dto .EasyCodefResponse .DATA ;
5
- import static io .codef .api .dto .EasyCodefResponse .RESULT ;
6
-
7
3
import com .alibaba .fastjson2 .JSON ;
8
4
import com .alibaba .fastjson2 .JSONObject ;
5
+ import io .codef .api .constants .CodefResponseCode ;
9
6
import io .codef .api .dto .EasyCodefResponse ;
10
7
import io .codef .api .error .CodefError ;
11
8
import io .codef .api .error .CodefException ;
9
+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
10
+ import org .apache .hc .core5 .http .HttpStatus ;
11
+ import org .apache .hc .core5 .http .ParseException ;
12
+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
13
+
12
14
import java .io .IOException ;
13
15
import java .net .URLDecoder ;
14
16
import java .nio .charset .StandardCharsets ;
17
+ import java .util .List ;
15
18
import java .util .Optional ;
16
19
import java .util .function .Function ;
17
- import org . apache . hc . core5 . http . ClassicHttpResponse ;
18
- import org . apache . hc . core5 . http . HttpStatus ;
19
- import org . apache . hc . core5 . http . ParseException ;
20
- import org . apache . hc . core5 . http . io . entity . EntityUtils ;
20
+
21
+ import static io . codef . api . dto . EasyCodefRequest . ACCESS_TOKEN ;
22
+ import static io . codef . api . dto . EasyCodefResponse . DATA ;
23
+ import static io . codef . api . dto . EasyCodefResponse . RESULT ;
21
24
22
25
public class ResponseHandler {
26
+
23
27
private static final String UTF_8 = StandardCharsets .UTF_8 .toString ();
24
28
29
+ public static boolean isSuccessResponse (EasyCodefResponse response ) {
30
+ return CodefResponseCode .CF_00000 .equals (response .code ());
31
+ }
32
+
33
+ public static boolean isAddAuthResponse (EasyCodefResponse response ) {
34
+ return CodefResponseCode .CF_03002 .equals (response .code ());
35
+ }
36
+
37
+ public static boolean isAddAuthExceedResponse (EasyCodefResponse response ) {
38
+ return CodefResponseCode .CF_12872 .equals (response .code ());
39
+ }
40
+
41
+ public static boolean isFailureResponse (EasyCodefResponse response ) {
42
+ return !isSuccessResponse (response ) && !isAddAuthResponse (response );
43
+ }
44
+
25
45
/**
26
46
* 토큰 응답 처리
27
47
*/
28
48
public String handleTokenResponse (ClassicHttpResponse response ) throws CodefException {
29
49
return handleHttpResponse (
30
- response ,
31
- this ::parseAccessToken ,
32
- CodefError .OAUTH_UNAUTHORIZED ,
33
- CodefError .OAUTH_INTERNAL_ERROR ,
34
- false
50
+ response ,
51
+ this ::parseAccessToken ,
52
+ CodefError .OAUTH_UNAUTHORIZED ,
53
+ CodefError .OAUTH_INTERNAL_ERROR ,
54
+ false
35
55
);
36
56
}
37
57
38
58
/**
39
59
* 상품 응답 처리
40
60
*/
41
- public EasyCodefResponse handleProductResponse (ClassicHttpResponse response ) throws CodefException {
61
+ public EasyCodefResponse handleProductResponse (ClassicHttpResponse response )
62
+ throws CodefException {
42
63
return handleHttpResponse (
43
- response ,
44
- this ::parseProductResponse ,
45
- CodefError .CODEF_API_UNAUTHORIZED ,
46
- CodefError .CODEF_API_SERVER_ERROR ,
47
- true
64
+ response ,
65
+ this ::parseProductResponse ,
66
+ CodefError .CODEF_API_UNAUTHORIZED ,
67
+ CodefError .CODEF_API_SERVER_ERROR ,
68
+ true
48
69
);
49
70
}
50
71
51
72
/**
52
73
* 공통 HTTP 응답 처리 로직
53
74
*/
54
75
private <T > T handleHttpResponse (
55
- ClassicHttpResponse response ,
56
- Function <String , T > parser ,
57
- CodefError unauthorizedError ,
58
- CodefError defaultError ,
59
- boolean requireUrlDecoding
76
+ ClassicHttpResponse response ,
77
+ Function <String , T > parser ,
78
+ CodefError unauthorizedError ,
79
+ CodefError defaultError ,
80
+ boolean requireUrlDecoding
60
81
) throws CodefException {
61
82
String responseBody = extractResponseBody (response , requireUrlDecoding );
62
83
@@ -70,7 +91,8 @@ private <T> T handleHttpResponse(
70
91
/**
71
92
* HTTP 응답 본문 추출
72
93
*/
73
- private String extractResponseBody (ClassicHttpResponse response , boolean requiresDecoding ) throws CodefException {
94
+ private String extractResponseBody (ClassicHttpResponse response , boolean requiresDecoding )
95
+ throws CodefException {
74
96
try {
75
97
String responseBody = EntityUtils .toString (response .getEntity ());
76
98
return requiresDecoding ? URLDecoder .decode (responseBody , UTF_8 ) : responseBody ;
@@ -99,17 +121,38 @@ private EasyCodefResponse parseProductResponse(String responseBody) throws Codef
99
121
try {
100
122
JSONObject jsonResponse = JSON .parseObject (responseBody );
101
123
102
- EasyCodefResponse .Result result = Optional .ofNullable (jsonResponse .getJSONObject (RESULT ))
124
+ EasyCodefResponse .Result result = parseResult (jsonResponse );
125
+ Object data = parseData (jsonResponse );
126
+
127
+ return new EasyCodefResponse (result , data );
128
+ } catch (Exception exception ) {
129
+ throw CodefException .of (CodefError .PARSE_ERROR , exception );
130
+ }
131
+ }
132
+
133
+ private EasyCodefResponse .Result parseResult (JSONObject jsonResponse ) throws CodefException {
134
+ return Optional .ofNullable (jsonResponse .getJSONObject (RESULT ))
103
135
.map (object -> object .to (EasyCodefResponse .Result .class ))
104
136
.orElseThrow (() -> CodefException .from (CodefError .PARSE_ERROR ));
137
+ }
105
138
106
- Object data = Optional .ofNullable (jsonResponse .getJSONObject (DATA ))
139
+ private Object parseData (JSONObject jsonResponse ) throws CodefException {
140
+ try {
141
+ return parseObjectData (jsonResponse );
142
+ } catch (Exception e ) {
143
+ return parseArrayData (jsonResponse );
144
+ }
145
+ }
146
+
147
+ private Object parseObjectData (JSONObject jsonResponse ) throws CodefException {
148
+ return Optional .ofNullable (jsonResponse .getJSONObject (DATA ))
107
149
.map (obj -> obj .to (Object .class ))
108
150
.orElseThrow (() -> CodefException .from (CodefError .PARSE_ERROR ));
151
+ }
109
152
110
- return new EasyCodefResponse ( result , data );
111
- } catch ( Exception e ) {
112
- throw CodefException . of ( CodefError . PARSE_ERROR , e );
113
- }
153
+ private List <?> parseArrayData ( JSONObject jsonResponse ) throws CodefException {
154
+ return Optional . ofNullable ( jsonResponse . getJSONArray ( DATA ))
155
+ . map ( obj -> obj . to ( List . class ))
156
+ . orElseThrow (() -> CodefException . from ( CodefError . PARSE_ERROR ));
114
157
}
115
158
}
0 commit comments