4343import org .cactoos .text .UncheckedText ;
4444import org .proshin .finapi .Jsonable ;
4545import org .proshin .finapi .accesstoken .AccessToken ;
46+ import org .proshin .finapi .exception .FinapiAuthenticationException ;
4647import org .proshin .finapi .exception .FinapiException ;
4748import org .slf4j .Logger ;
4849import org .slf4j .LoggerFactory ;
@@ -71,19 +72,8 @@ public String get(final String path, final AccessToken token, final Iterable<Nam
7172 builder .setParameters (new ListOf <>(parameters ));
7273 final HttpUriRequest get = new HttpGet (builder .build ());
7374 get .addHeader (new AuthorizationHeader (token .accessToken ()));
74- final HttpResponse response = this .client .execute (get );
75- if (response .getStatusLine ().getStatusCode () != HttpStatus .SC_OK ) {
76- throw new FinapiException (HttpStatus .SC_OK , response );
77- }
78- final String responseBody = new UncheckedText (
79- new TextOf (
80- new InputOf (response .getEntity ().getContent ()),
81- StandardCharsets .UTF_8
82- )
83- ).asString ();
84- LOGGER .info ("Response body was: {}" , responseBody );
85- return responseBody ;
86- } catch (final IOException | URISyntaxException e ) {
75+ return this .execute (get );
76+ } catch (final URISyntaxException e ) {
8777 throw new RuntimeException (
8878 new UncheckedText (
8979 new FormattedText (
@@ -103,19 +93,8 @@ public String delete(final String path, final AccessToken token, final Iterable<
10393 builder .setParameters (new ListOf <>(parameters ));
10494 final HttpUriRequest delete = new HttpDelete (builder .build ());
10595 delete .addHeader (new AuthorizationHeader (token .accessToken ()));
106- final HttpResponse response = this .client .execute (delete );
107- if (response .getStatusLine ().getStatusCode () != HttpStatus .SC_OK ) {
108- throw new FinapiException (HttpStatus .SC_OK , response );
109- }
110- final String responseBody = new UncheckedText (
111- new TextOf (
112- new InputOf (response .getEntity ().getContent ()),
113- StandardCharsets .UTF_8
114- )
115- ).asString ();
116- LOGGER .info ("Response body was: {}" , responseBody );
117- return responseBody ;
118- } catch (final IOException | URISyntaxException e ) {
96+ return this .execute (delete );
97+ } catch (final URISyntaxException e ) {
11998 throw new IllegalStateException (
12099 new UncheckedText (
121100 new FormattedText (
@@ -142,29 +121,9 @@ public HttpPost post(final String path) {
142121 */
143122 @ Override
144123 public String post (final String path , final HttpEntity entity , final int expected ) {
145- try {
146- final HttpPost post = new HttpPost (this .endpoint + path );
147- post .setEntity (entity );
148- final HttpResponse response = this .client .execute (post );
149- if (response .getStatusLine ().getStatusCode () != expected ) {
150- throw new FinapiException (expected , response );
151- }
152- return new UncheckedText (
153- new TextOf (
154- new InputOf (response .getEntity ().getContent ()),
155- StandardCharsets .UTF_8
156- )
157- ).asString ();
158- } catch (final IOException e ) {
159- throw new IllegalStateException (
160- new UncheckedText (
161- new FormattedText (
162- "Couldn't post to '%s'" ,
163- path
164- )
165- ).asString ()
166- );
167- }
124+ final HttpPost post = new HttpPost (this .endpoint + path );
125+ post .setEntity (entity );
126+ return this .execute (post , expected );
168127 }
169128
170129 @ Override
@@ -176,67 +135,50 @@ public HttpPost post(final String path, final AccessToken token) {
176135
177136 @ Override
178137 public String post (final String path , final AccessToken token , final int expected ) {
179- try {
180- final HttpUriRequest post = new HttpPost (this .endpoint + path );
181- post .addHeader (new AuthorizationHeader (token .accessToken ()));
182- final HttpResponse response = this .client .execute (post );
183- if (response .getStatusLine ().getStatusCode () != expected ) {
184- throw new FinapiException (expected , response );
185- }
186- return new UncheckedText (
187- new TextOf (
188- new InputOf (response .getEntity ().getContent ()),
189- StandardCharsets .UTF_8
190- )
191- ).asString ();
192- } catch (final IOException e ) {
193- throw new IllegalStateException (
194- new UncheckedText (
195- new FormattedText (
196- "Couldn't post to '%s'" ,
197- path
198- )
199- ).asString ()
200- );
201- }
138+ final HttpUriRequest post = new HttpPost (this .endpoint + path );
139+ post .addHeader (new AuthorizationHeader (token .accessToken ()));
140+ return this .execute (post , expected );
202141 }
203142
204143 @ Override
205144 public String post (final String path , final AccessToken token , final HttpEntity entity , final int expected ) {
206- try {
207- final HttpPost post = new HttpPost (this .endpoint + path );
208- post .addHeader (new AuthorizationHeader (token .accessToken ()));
209- post .setEntity (entity );
210- final HttpResponse response = this .client .execute (post );
211- if (response .getStatusLine ().getStatusCode () != expected ) {
212- throw new FinapiException (expected , response );
213- }
214- return new UncheckedText (
215- new TextOf (
216- new InputOf (response .getEntity ().getContent ()),
217- StandardCharsets .UTF_8
218- )
219- ).asString ();
220- } catch (final IOException e ) {
221- throw new IllegalStateException (
222- new UncheckedText (
223- new FormattedText (
224- "Couldn't post to '%s'" ,
225- path
226- )
227- ).asString ()
228- );
229- }
145+ final HttpPost post = new HttpPost (this .endpoint + path );
146+ post .addHeader (new AuthorizationHeader (token .accessToken ()));
147+ post .setEntity (entity );
148+ return this .execute (post , expected );
230149 }
231150
232151 @ Override
233152 public String patch (final String path , final AccessToken token , final HttpEntity entity , final int expected ) {
153+ final HttpPatch patch = new HttpPatch (this .endpoint + path );
154+ patch .addHeader (new AuthorizationHeader (token .accessToken ()));
155+ patch .setEntity (entity );
156+ return this .execute (patch , expected );
157+ }
158+
159+ @ Override
160+ public String patch (final String path , final AccessToken token , final Jsonable body ) {
161+ return this .patch (
162+ path , token ,
163+ new StringEntity (
164+ body .asString (),
165+ ContentType .APPLICATION_JSON
166+ ),
167+ HttpStatus .SC_OK
168+ );
169+ }
170+
171+ private String execute (final HttpUriRequest request ) {
172+ return this .execute (request , HttpStatus .SC_OK );
173+ }
174+
175+ private String execute (final HttpUriRequest request , final int expected ) {
234176 try {
235- final HttpPatch patch = new HttpPatch ( this .endpoint + path );
236- patch . addHeader ( new AuthorizationHeader ( token . accessToken ()) );
237- patch . setEntity ( entity );
238- final HttpResponse response = this . client . execute ( patch );
239- if (response . getStatusLine (). getStatusCode () != expected ) {
177+ final HttpResponse response = this .client . execute ( request );
178+ final int statusCode = response . getStatusLine (). getStatusCode ( );
179+ if ( statusCode == HttpStatus . SC_UNAUTHORIZED ) {
180+ throw new FinapiAuthenticationException ( response );
181+ } else if (statusCode != expected ) {
240182 throw new FinapiException (expected , response );
241183 }
242184 return new UncheckedText (
@@ -246,29 +188,18 @@ public String patch(final String path, final AccessToken token, final HttpEntity
246188 )
247189 ).asString ();
248190 } catch (final IOException e ) {
249- throw new IllegalStateException (
191+ throw new RuntimeException (
250192 new UncheckedText (
251193 new FormattedText (
252- "Couldn't post to '%s'" ,
253- path
194+ "Couldn't get '%s'" ,
195+ request . getURI ()
254196 )
255- ).asString ()
197+ ).asString (),
198+ e
256199 );
257200 }
258201 }
259202
260- @ Override
261- public String patch (final String path , final AccessToken token , final Jsonable body ) {
262- return this .patch (
263- path , token ,
264- new StringEntity (
265- body .asString (),
266- ContentType .APPLICATION_JSON
267- ),
268- HttpStatus .SC_OK
269- );
270- }
271-
272203 @ SuppressWarnings ("staticfree" )
273204 private static final class AuthorizationHeader implements Header {
274205
0 commit comments