5
5
6
6
package com .bitpay .sdk .client ;
7
7
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 ;
9
11
import com .bitpay .sdk .model .Facade ;
10
12
import com .bitpay .sdk .model .Token ;
11
13
import com .bitpay .sdk .util .GuidGenerator ;
16
18
import java .util .Arrays ;
17
19
import java .util .List ;
18
20
import java .util .Objects ;
19
- import org .apache .http .HttpResponse ;
20
21
21
22
/**
22
23
* The type Authorization client.
@@ -52,36 +53,34 @@ public AuthorizationClient(
52
53
* Authorize (pair) this client with the server using the specified pairing code.
53
54
*
54
55
* @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
56
58
*/
57
- public void authorizeClient (String pairingCode ) throws BitPayException {
59
+ public void authorizeClient (String pairingCode ) throws BitPayApiException , BitPayGenericException {
58
60
Token token = new Token ();
59
61
token .setId (this .identity );
60
62
token .setGuid (this .guidGenerator .execute ());
61
63
token .setPairingCode (pairingCode );
62
64
63
65
JsonMapper mapper = JsonMapperFactory .create ();
64
66
65
- String json ;
67
+ String json = null ;
66
68
67
69
try {
68
70
json = mapper .writeValueAsString (token );
69
71
} 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 ());
71
74
}
72
75
73
- HttpResponse response = this .bitPayClient .post ("tokens" , json );
76
+ String jsonResponse = this .bitPayClient .post ("tokens" , json );
74
77
75
- List <Token > tokens ;
78
+ List <Token > tokens = null ;
76
79
77
80
try {
78
- tokens = Arrays .asList (mapper .readValue (this . bitPayClient . responseToJsonString ( response ) , Token [].class ));
81
+ tokens = Arrays .asList (mapper .readValue (jsonResponse , Token [].class ));
79
82
} 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 ());
85
84
}
86
85
87
86
for (Token t : tokens ) {
@@ -94,12 +93,14 @@ public void authorizeClient(String pairingCode) throws BitPayException {
94
93
*
95
94
* @param facade Defines the level of API access being requested
96
95
* @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
98
98
*/
99
- public String authorizeClient (Facade facade ) throws BitPayException {
99
+ public String authorizeClient (Facade facade ) throws BitPayApiException , BitPayGenericException {
100
100
if (Objects .isNull (facade )) {
101
- throw new BitPayException ( null , "missing required parameter" );
101
+ BitPayExceptionProvider . throwValidationException ( "Missing required parameter" );
102
102
}
103
+
103
104
Token token = new Token ();
104
105
token .setId (this .identity );
105
106
token .setGuid (this .guidGenerator .execute ());
@@ -108,32 +109,31 @@ public String authorizeClient(Facade facade) throws BitPayException {
108
109
109
110
JsonMapper mapper = JsonMapperFactory .create ();
110
111
111
- String json ;
112
+ String json = null ;
112
113
113
114
try {
114
115
json = mapper .writeValueAsString (token );
115
116
} catch (JsonProcessingException e ) {
116
- throw new BitPayException ( null , "failed to serialize Token object : " + e .getMessage ());
117
+ BitPayExceptionProvider . throwSerializeResourceException ( " Token" , e .getMessage ());
117
118
}
118
119
119
- HttpResponse response = this .bitPayClient .post ("tokens" , json );
120
+ String response = this .bitPayClient .post ("tokens" , json );
120
121
121
- List <Token > tokens ;
122
+ List <Token > tokens = null ;
122
123
123
124
try {
124
- tokens = Arrays .asList (mapper .readValue (this . bitPayClient . responseToJsonString ( response ) , Token [].class ));
125
+ tokens = Arrays .asList (mapper .readValue (response , Token [].class ));
125
126
126
127
// Expecting a single token resource.
127
128
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
+ );
129
133
}
130
134
131
135
} 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 ());
137
137
}
138
138
139
139
this .accessToken .put (tokens .get (0 ).getFacade (), tokens .get (0 ).getValue ());
0 commit comments