11package io .codef .api ;
22
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-
73import com .alibaba .fastjson2 .JSON ;
84import com .alibaba .fastjson2 .JSONObject ;
5+ import io .codef .api .constants .CodefResponseCode ;
96import io .codef .api .dto .EasyCodefResponse ;
107import io .codef .api .error .CodefError ;
118import 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+
1214import java .io .IOException ;
1315import java .net .URLDecoder ;
1416import java .nio .charset .StandardCharsets ;
17+ import java .util .List ;
1518import java .util .Optional ;
1619import 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 ;
2124
2225public class ResponseHandler {
26+
2327 private static final String UTF_8 = StandardCharsets .UTF_8 .toString ();
2428
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+
2545 /**
2646 * 토큰 응답 처리
2747 */
2848 public String handleTokenResponse (ClassicHttpResponse response ) throws CodefException {
2949 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
3555 );
3656 }
3757
3858 /**
3959 * 상품 응답 처리
4060 */
41- public EasyCodefResponse handleProductResponse (ClassicHttpResponse response ) throws CodefException {
61+ public EasyCodefResponse handleProductResponse (ClassicHttpResponse response )
62+ throws CodefException {
4263 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
4869 );
4970 }
5071
5172 /**
5273 * 공통 HTTP 응답 처리 로직
5374 */
5475 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
6081 ) throws CodefException {
6182 String responseBody = extractResponseBody (response , requireUrlDecoding );
6283
@@ -70,7 +91,8 @@ private <T> T handleHttpResponse(
7091 /**
7192 * HTTP 응답 본문 추출
7293 */
73- private String extractResponseBody (ClassicHttpResponse response , boolean requiresDecoding ) throws CodefException {
94+ private String extractResponseBody (ClassicHttpResponse response , boolean requiresDecoding )
95+ throws CodefException {
7496 try {
7597 String responseBody = EntityUtils .toString (response .getEntity ());
7698 return requiresDecoding ? URLDecoder .decode (responseBody , UTF_8 ) : responseBody ;
@@ -99,17 +121,38 @@ private EasyCodefResponse parseProductResponse(String responseBody) throws Codef
99121 try {
100122 JSONObject jsonResponse = JSON .parseObject (responseBody );
101123
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 ))
103135 .map (object -> object .to (EasyCodefResponse .Result .class ))
104136 .orElseThrow (() -> CodefException .from (CodefError .PARSE_ERROR ));
137+ }
105138
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 ))
107149 .map (obj -> obj .to (Object .class ))
108150 .orElseThrow (() -> CodefException .from (CodefError .PARSE_ERROR ));
151+ }
109152
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 ));
114157 }
115158}
0 commit comments