|
1 | 1 | package com.meilisearch.sdk;
|
2 | 2 |
|
3 |
| -import java.io.BufferedReader; |
4 |
| -import java.io.IOException; |
5 |
| -import java.io.InputStreamReader; |
6 |
| -import java.net.HttpURLConnection; |
7 |
| -import java.net.URL; |
8 |
| -import java.nio.charset.StandardCharsets; |
9 |
| -import java.util.Optional; |
| 3 | +import com.meilisearch.sdk.http.AbstractHttpClient; |
| 4 | +import com.meilisearch.sdk.http.DefaultHttpClient; |
| 5 | +import com.meilisearch.sdk.http.factory.BasicRequestFactory; |
| 6 | +import com.meilisearch.sdk.http.factory.RequestFactory; |
| 7 | +import com.meilisearch.sdk.http.request.HttpMethod; |
| 8 | +import com.meilisearch.sdk.http.response.HttpResponse; |
| 9 | + |
| 10 | +import java.util.Collections; |
10 | 11 |
|
11 | 12 | class MeiliSearchHttpRequest {
|
12 |
| - private final Config config; |
| 13 | + private final AbstractHttpClient client; |
| 14 | + private final RequestFactory factory; |
13 | 15 |
|
14 | 16 | protected MeiliSearchHttpRequest(Config config) {
|
15 |
| - this.config = config; |
| 17 | + this.client = new DefaultHttpClient(config); |
| 18 | + this.factory = new BasicRequestFactory(); |
16 | 19 | }
|
17 | 20 |
|
18 |
| - /** |
19 |
| - * Create and get a validated HTTP connection to url with method and API key |
20 |
| - * |
21 |
| - * @param url URL to connect to |
22 |
| - * @param method HTTP method to use for the connection |
23 |
| - * @param apiKey API Key to use for the connection |
24 |
| - * @return Validated connection (otherwise, will throw a {@link IOException}) |
25 |
| - * @throws IOException If unable to establish connection |
26 |
| - */ |
27 |
| - private HttpURLConnection getConnection(final URL url, final String method, final String apiKey) throws IOException { |
28 |
| - if (url == null || "".equals(method)) throw new IOException("Unable to open an HttpURLConnection with no URL or method"); |
29 |
| - |
30 |
| - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
31 |
| - connection.setRequestMethod(method); |
32 |
| - connection.setRequestProperty("Content-Type", "application/json"); |
33 |
| - |
34 |
| - // Use API key header only if one is provided |
35 |
| - if (!"".equals(apiKey)) { |
36 |
| - connection.setRequestProperty("X-Meili-API-Key", apiKey); |
37 |
| - } |
38 |
| - |
39 |
| - // Ensure connection is set |
40 |
| - Optional<HttpURLConnection> connectionOptional = Optional.of(connection); |
41 |
| - return connectionOptional.orElseThrow(IOException::new); |
42 |
| - } |
43 |
| - |
44 |
| - |
45 |
| - private String parseConnectionResponse(HttpURLConnection connection) throws IOException { |
46 |
| - BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); |
47 |
| - StringBuilder sb = new StringBuilder(); |
48 |
| - String responseLine; |
49 |
| - |
50 |
| - while ((responseLine = br.readLine()) != null) { |
51 |
| - sb.append(responseLine); |
52 |
| - } |
53 |
| - |
54 |
| - br.close(); |
55 |
| - |
56 |
| - return sb.toString(); |
| 21 | + public MeiliSearchHttpRequest(AbstractHttpClient client, RequestFactory factory) { |
| 22 | + this.client = client; |
| 23 | + this.factory = factory; |
57 | 24 | }
|
58 | 25 |
|
59 | 26 |
|
60 |
| - String get(String api) throws Exception { |
| 27 | + public String get(String api) throws Exception { |
61 | 28 | return this.get(api, "");
|
62 | 29 | }
|
63 | 30 |
|
64 |
| - |
65 | 31 | String get(String api, String param) throws Exception {
|
66 |
| - StringBuilder urlBuilder = new StringBuilder(config.hostUrl + api); |
67 |
| - if (!param.equals("")) { |
68 |
| - urlBuilder.append(param); |
69 |
| - } |
70 |
| - |
71 |
| - URL url = new URL(urlBuilder.toString()); |
72 |
| - HttpURLConnection connection = this.getConnection(url, "GET", this.config.apiKey); |
73 |
| - |
74 |
| - return this.parseConnectionResponse(connection); |
| 32 | + HttpResponse<?> httpResponse = this.client.get(factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null)); |
| 33 | + return new String(httpResponse.getContentAsBytes()); |
75 | 34 | }
|
76 | 35 |
|
77 | 36 |
|
78 |
| - String post(String api, String params) throws IOException { |
79 |
| - URL url = new URL(config.hostUrl + api); |
80 |
| - |
81 |
| - HttpURLConnection connection = this.getConnection(url, "POST", config.apiKey); |
82 |
| - connection.setDoOutput(true); |
83 |
| - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
84 |
| - connection.setRequestProperty("Content-Length", String.valueOf(params.length())); |
85 |
| - connection.getOutputStream().write(params.getBytes(StandardCharsets.UTF_8)); |
86 |
| - connection.connect(); |
87 |
| - |
88 |
| - return this.parseConnectionResponse(connection); |
| 37 | + String post(String api, String body) throws Exception { |
| 38 | + HttpResponse<?> httpResponse = this.client.post(factory.create(HttpMethod.POST, api, Collections.emptyMap(), body)); |
| 39 | + return new String(httpResponse.getContentAsBytes()); |
89 | 40 | }
|
90 | 41 |
|
91 | 42 |
|
92 |
| - String put(String api, String params) throws Exception { |
93 |
| - URL url = new URL(config.hostUrl + api); |
94 |
| - |
95 |
| - HttpURLConnection connection = this.getConnection(url, "PUT", config.apiKey); |
96 |
| - connection.setDoOutput(true); |
97 |
| - connection.getOutputStream().write(params.getBytes()); |
98 |
| - connection.connect(); |
99 |
| - |
100 |
| - return this.parseConnectionResponse(connection); |
| 43 | + String put(String api, String body) throws Exception { |
| 44 | + HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body)); |
| 45 | + return new String(httpResponse.getContentAsBytes()); |
101 | 46 | }
|
102 | 47 |
|
103 | 48 |
|
104 | 49 | String delete(String api) throws Exception {
|
105 |
| - URL url = new URL(config.hostUrl + api); |
106 |
| - |
107 |
| - HttpURLConnection connection = this.getConnection(url, "DELETE", config.apiKey); |
108 |
| - connection.connect(); |
109 |
| - return this.parseConnectionResponse(connection); |
| 50 | + HttpResponse<?> httpResponse = this.client.put(factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null)); |
| 51 | + return new String(httpResponse.getContentAsBytes()); |
110 | 52 | }
|
111 | 53 | }
|
0 commit comments