55
66package com .bitpay .sdk .client ;
77
8- import com .bitpay .sdk .exceptions .BitPayException ;
8+ import com .bitpay .sdk .exceptions .BitPayApiException ;
9+ import com .bitpay .sdk .exceptions .BitPayExceptionProvider ;
10+ import com .bitpay .sdk .exceptions .BitPayGenericException ;
911import com .bitpay .sdk .model .Facade ;
1012import com .bitpay .sdk .model .Token ;
1113import com .bitpay .sdk .util .GuidGenerator ;
1618import java .util .Arrays ;
1719import java .util .List ;
1820import java .util .Objects ;
19- import org .apache .http .HttpResponse ;
2021
2122/**
2223 * The type Authorization client.
@@ -52,36 +53,34 @@ public AuthorizationClient(
5253 * Authorize (pair) this client with the server using the specified pairing code.
5354 *
5455 * @param pairingCode A code obtained from the server; typically from bitpay.com/api-tokens.
55- * @throws BitPayException BitPayException class
56+ * @throws BitPayApiException BitPayApiException class
57+ * @throws BitPayGenericException BitPayGenericException class
5658 */
57- public void authorizeClient (String pairingCode ) throws BitPayException {
59+ public void authorizeClient (String pairingCode ) throws BitPayApiException , BitPayGenericException {
5860 Token token = new Token ();
5961 token .setId (this .identity );
6062 token .setGuid (this .guidGenerator .execute ());
6163 token .setPairingCode (pairingCode );
6264
6365 JsonMapper mapper = JsonMapperFactory .create ();
6466
65- String json ;
67+ String json = null ;
6668
6769 try {
6870 json = mapper .writeValueAsString (token );
6971 } catch (JsonProcessingException e ) {
70- throw new BitPayException (null , "failed to serialize Token object : " + e .getMessage ());
72+ BitPayExceptionProvider .throwGenericExceptionWithMessage (
73+ "Failed to serialize Token object : " + e .getMessage ());
7174 }
7275
73- HttpResponse response = this .bitPayClient .post ("tokens" , json );
76+ String jsonResponse = this .bitPayClient .post ("tokens" , json );
7477
75- List <Token > tokens ;
78+ List <Token > tokens = null ;
7679
7780 try {
78- tokens = Arrays .asList (mapper .readValue (this . bitPayClient . responseToJsonString ( response ) , Token [].class ));
81+ tokens = Arrays .asList (mapper .readValue (jsonResponse , Token [].class ));
7982 } catch (JsonProcessingException e ) {
80- throw new BitPayException (null ,
81- "failed to deserialize BitPay server response (Tokens) : " + e .getMessage ());
82- } catch (Exception e ) {
83- throw new BitPayException (null ,
84- "failed to deserialize BitPay server response (Tokens) : " + e .getMessage ());
83+ BitPayExceptionProvider .throwDeserializeResourceException ("Tokens" , e .getMessage ());
8584 }
8685
8786 for (Token t : tokens ) {
@@ -94,12 +93,14 @@ public void authorizeClient(String pairingCode) throws BitPayException {
9493 *
9594 * @param facade Defines the level of API access being requested
9695 * @return A pairing code for claim at https://bitpay.com/dashboard/merchant/api-tokens.
97- * @throws BitPayException BitPayException class
96+ * @throws BitPayGenericException BitPayGenericException class
97+ * @throws BitPayApiException BitPayApiException class
9898 */
99- public String authorizeClient (Facade facade ) throws BitPayException {
99+ public String authorizeClient (Facade facade ) throws BitPayApiException , BitPayGenericException {
100100 if (Objects .isNull (facade )) {
101- throw new BitPayException ( null , "missing required parameter" );
101+ BitPayExceptionProvider . throwValidationException ( "Missing required parameter" );
102102 }
103+
103104 Token token = new Token ();
104105 token .setId (this .identity );
105106 token .setGuid (this .guidGenerator .execute ());
@@ -108,32 +109,31 @@ public String authorizeClient(Facade facade) throws BitPayException {
108109
109110 JsonMapper mapper = JsonMapperFactory .create ();
110111
111- String json ;
112+ String json = null ;
112113
113114 try {
114115 json = mapper .writeValueAsString (token );
115116 } catch (JsonProcessingException e ) {
116- throw new BitPayException ( null , "failed to serialize Token object : " + e .getMessage ());
117+ BitPayExceptionProvider . throwSerializeResourceException ( " Token" , e .getMessage ());
117118 }
118119
119- HttpResponse response = this .bitPayClient .post ("tokens" , json );
120+ String response = this .bitPayClient .post ("tokens" , json );
120121
121- List <Token > tokens ;
122+ List <Token > tokens = null ;
122123
123124 try {
124- tokens = Arrays .asList (mapper .readValue (this . bitPayClient . responseToJsonString ( response ) , Token [].class ));
125+ tokens = Arrays .asList (mapper .readValue (response , Token [].class ));
125126
126127 // Expecting a single token resource.
127128 if (tokens .size () != 1 ) {
128- throw new BitPayException (null , "failed to get token resource; expected 1 token, got " + tokens .size ());
129+ BitPayExceptionProvider .throwDeserializeResourceException (
130+ "Token" ,
131+ "expected 1 token, got " + tokens .size ()
132+ );
129133 }
130134
131135 } catch (JsonProcessingException e ) {
132- throw new BitPayException (null ,
133- "failed to deserialize BitPay server response (Tokens) : " + e .getMessage ());
134- } catch (Exception e ) {
135- throw new BitPayException (null ,
136- "failed to deserialize BitPay server response (Tokens) : " + e .getMessage ());
136+ BitPayExceptionProvider .throwDeserializeResourceException ("Tokens" , e .getMessage ());
137137 }
138138
139139 this .accessToken .put (tokens .get (0 ).getFacade (), tokens .get (0 ).getValue ());
0 commit comments