2
2
3
3
import com .meilisearch .sdk .exceptions .APIError ;
4
4
import com .meilisearch .sdk .exceptions .MeiliSearchApiException ;
5
+ import com .meilisearch .sdk .exceptions .MeiliSearchCommunicationException ;
5
6
import com .meilisearch .sdk .http .AbstractHttpClient ;
6
7
import com .meilisearch .sdk .http .DefaultHttpClient ;
7
8
import com .meilisearch .sdk .http .factory .BasicRequestFactory ;
10
11
import com .meilisearch .sdk .http .response .HttpResponse ;
11
12
import com .meilisearch .sdk .json .GsonJsonHandler ;
12
13
import com .meilisearch .sdk .json .JsonHandler ;
14
+ import java .io .IOException ;
13
15
import java .util .Collections ;
14
16
15
17
/** The HTTP requests for the different functions to be done through Meilisearch */
@@ -63,14 +65,19 @@ public String get(String api) throws Exception, MeiliSearchApiException {
63
65
* @throws MeiliSearchApiException if the response is an error
64
66
*/
65
67
String get (String api , String param ) throws Exception , MeiliSearchApiException {
66
- HttpResponse <?> httpResponse =
67
- this .client .get (
68
- factory .create (HttpMethod .GET , api + param , Collections .emptyMap (), null ));
69
- if (httpResponse .getStatusCode () >= 400 ) {
70
- throw new MeiliSearchApiException (
71
- jsonHandler .decode (httpResponse .getContent (), APIError .class ));
68
+ try {
69
+ HttpResponse <?> httpResponse =
70
+ this .client .get (
71
+ factory .create (
72
+ HttpMethod .GET , api + param , Collections .emptyMap (), null ));
73
+ if (httpResponse .getStatusCode () >= 400 ) {
74
+ throw new MeiliSearchApiException (
75
+ jsonHandler .decode (httpResponse .getContent (), APIError .class ));
76
+ }
77
+ return new String (httpResponse .getContentAsBytes ());
78
+ } catch (IOException e ) {
79
+ throw new MeiliSearchCommunicationException (e );
72
80
}
73
- return new String (httpResponse .getContentAsBytes ());
74
81
}
75
82
76
83
/**
@@ -83,14 +90,18 @@ String get(String api, String param) throws Exception, MeiliSearchApiException {
83
90
* @throws MeiliSearchApiException if the response is an error
84
91
*/
85
92
<T > String post (String api , T body ) throws Exception , MeiliSearchApiException {
86
- HttpResponse <?> httpResponse =
87
- this .client .post (
88
- factory .create (HttpMethod .POST , api , Collections .emptyMap (), body ));
89
- if (httpResponse .getStatusCode () >= 400 ) {
90
- throw new MeiliSearchApiException (
91
- jsonHandler .decode (httpResponse .getContent (), APIError .class ));
93
+ try {
94
+ HttpResponse <?> httpResponse =
95
+ this .client .post (
96
+ factory .create (HttpMethod .POST , api , Collections .emptyMap (), body ));
97
+ if (httpResponse .getStatusCode () >= 400 ) {
98
+ throw new MeiliSearchApiException (
99
+ jsonHandler .decode (httpResponse .getContent (), APIError .class ));
100
+ }
101
+ return new String (httpResponse .getContentAsBytes ());
102
+ } catch (IOException e ) {
103
+ throw new MeiliSearchCommunicationException (e );
92
104
}
93
- return new String (httpResponse .getContentAsBytes ());
94
105
}
95
106
96
107
/**
@@ -103,13 +114,18 @@ <T> String post(String api, T body) throws Exception, MeiliSearchApiException {
103
114
* @throws MeiliSearchApiException if the response is an error
104
115
*/
105
116
<T > String put (String api , T body ) throws Exception , MeiliSearchApiException {
106
- HttpResponse <?> httpResponse =
107
- this .client .put (factory .create (HttpMethod .PUT , api , Collections .emptyMap (), body ));
108
- if (httpResponse .getStatusCode () >= 400 ) {
109
- throw new MeiliSearchApiException (
110
- jsonHandler .decode (httpResponse .getContent (), APIError .class ));
117
+ try {
118
+ HttpResponse <?> httpResponse =
119
+ this .client .put (
120
+ factory .create (HttpMethod .PUT , api , Collections .emptyMap (), body ));
121
+ if (httpResponse .getStatusCode () >= 400 ) {
122
+ throw new MeiliSearchApiException (
123
+ jsonHandler .decode (httpResponse .getContent (), APIError .class ));
124
+ }
125
+ return new String (httpResponse .getContentAsBytes ());
126
+ } catch (IOException e ) {
127
+ throw new MeiliSearchCommunicationException (e );
111
128
}
112
- return new String (httpResponse .getContentAsBytes ());
113
129
}
114
130
115
131
/**
@@ -121,13 +137,17 @@ <T> String put(String api, T body) throws Exception, MeiliSearchApiException {
121
137
* @throws MeiliSearchApiException if the response is an error
122
138
*/
123
139
String delete (String api ) throws Exception , MeiliSearchApiException {
124
- HttpResponse <?> httpResponse =
125
- this .client .put (
126
- factory .create (HttpMethod .DELETE , api , Collections .emptyMap (), null ));
127
- if (httpResponse .getStatusCode () >= 400 ) {
128
- throw new MeiliSearchApiException (
129
- jsonHandler .decode (httpResponse .getContent (), APIError .class ));
140
+ try {
141
+ HttpResponse <?> httpResponse =
142
+ this .client .put (
143
+ factory .create (HttpMethod .DELETE , api , Collections .emptyMap (), null ));
144
+ if (httpResponse .getStatusCode () >= 400 ) {
145
+ throw new MeiliSearchApiException (
146
+ jsonHandler .decode (httpResponse .getContent (), APIError .class ));
147
+ }
148
+ return new String (httpResponse .getContentAsBytes ());
149
+ } catch (IOException e ) {
150
+ throw new MeiliSearchCommunicationException (e );
130
151
}
131
- return new String (httpResponse .getContentAsBytes ());
132
152
}
133
153
}
0 commit comments