@@ -33,13 +33,8 @@ public IdentityTokens generateToken(TokenGenerateInput tokenGenerateInput) {
3333 .post (RequestBody .create (envelope .getEnvelope (), FORM ))
3434 .build ();
3535
36-
3736 try (Response response = client .newCall (request ).execute ()) {
38- if (!response .isSuccessful ()) {
39- throw new Uid2Exception ("Unexpected code " + response );
40- }
41-
42- String responseString = response .body ().string ();
37+ final String responseString = getResponse (response );
4338 return publisherUid2Helper .createIdentityfromTokenGenerateResponse (responseString , envelope );
4439 } catch (IOException e ) {
4540 throw new Uid2Exception ("error communicating with api endpoint" , e );
@@ -62,16 +57,32 @@ public TokenGenerateResponse generateTokenResponse(TokenGenerateInput tokenGener
6257
6358
6459 try (Response response = client .newCall (request ).execute ()) {
65- if (!response .isSuccessful ()) {
66- throw new Uid2Exception ("Unexpected code " + response );
67- }
68-
69- String responseString = response .body () != null ? response .body ().string () : "" ;
60+ final String responseString = getResponse (response );
7061 return publisherUid2Helper .createTokenGenerateResponse (responseString , envelope );
7162 } catch (IOException e ) {
7263 throw new Uid2Exception ("Error communicating with api endpoint" , e );
7364 }
7465 }
66+
67+ private static String getResponse (Response response ) {
68+
69+ String responseString = "" ;
70+
71+ try {
72+ if (response == null ) {
73+ throw new Uid2Exception ("Response is null" );
74+ }
75+ else {
76+ responseString = response .body () != null ? response .body ().string () : response .toString ();
77+ if (!response .isSuccessful ()) {
78+ throw new Uid2Exception ("Unexpected code " + responseString );
79+ }
80+ }
81+ return responseString ;
82+ } catch (IOException e ) {
83+ throw new Uid2Exception ("Error communicating with api endpoint" , e );
84+ }
85+ }
7586
7687 /**
7788 * @param currentIdentity the current IdentityTokens instance, typically retrieved from a user's session
@@ -86,10 +97,8 @@ public TokenRefreshResponse refreshToken(IdentityTokens currentIdentity) {
8697
8798
8899 try (Response response = client .newCall (request ).execute ()) {
89- final String responseString = response .body () != null ? response .body ().string () : "" ;
90- if (!response .isSuccessful ()) {
91- throw new Uid2Exception ("Unexpected code " + response + " " + responseString );
92- }
100+ final String responseString = getResponse (response );
101+
93102
94103 return PublisherUid2Helper .createTokenRefreshResponse (responseString , currentIdentity );
95104 } catch (IOException e ) {
0 commit comments