Skip to content

Commit 8a4244a

Browse files
committed
Change MeiliSearchHttpRequest for MeilisearchHttpRequest
1 parent 101647a commit 8a4244a

File tree

3 files changed

+126
-132
lines changed

3 files changed

+126
-132
lines changed

src/main/java/com/meilisearch/sdk/KeysHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class KeysHandler {
1818
* @param config Meilisearch configuration
1919
*/
2020
public KeysHandler(Config config) {
21-
this.meilisearchHttpRequest = new MeilisearchHttpRequest;
21+
this.meilisearchHttpRequest = new MeilisearchHttpRequest(config);
2222
}
2323

2424
/**

src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java

Lines changed: 0 additions & 129 deletions
This file was deleted.
Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,129 @@
1+
package com.meilisearch.sdk;
2+
3+
import com.meilisearch.sdk.exceptions.APIError;
4+
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
5+
import com.meilisearch.sdk.exceptions.MeilisearchException;
6+
import com.meilisearch.sdk.http.AbstractHttpClient;
7+
import com.meilisearch.sdk.http.DefaultHttpClient;
8+
import com.meilisearch.sdk.http.factory.BasicRequestFactory;
9+
import com.meilisearch.sdk.http.factory.RequestFactory;
10+
import com.meilisearch.sdk.http.request.HttpMethod;
11+
import com.meilisearch.sdk.http.response.HttpResponse;
12+
import com.meilisearch.sdk.json.GsonJsonHandler;
13+
import com.meilisearch.sdk.json.JsonHandler;
14+
import java.util.Collections;
15+
16+
/** The HTTP requests for the different functions to be done through Meilisearch */
117
public class MeilisearchHttpRequest {
18+
private final AbstractHttpClient client;
19+
private final RequestFactory factory;
20+
protected final JsonHandler jsonHandler;
21+
22+
/**
223
* Constructor for the MeilisearchHttpRequest
24+
*
25+
* @param config Meilisearch configuration
26+
*/
327
public MeilisearchHttpRequest(Config config) {
28+
this.client = new DefaultHttpClient(config);
29+
this.jsonHandler = config.jsonHandler;
30+
this.factory = new BasicRequestFactory(jsonHandler);
31+
}
32+
33+
/**
434
* Constructor for the MeilisearchHttpRequest
5-
* @param request Requestrequest for generating calls to server
6-
public MeilisearchHttpRequest(AbstractHttpClient client, BasicRequest request) {
35+
*
36+
* @param client HttpClient for making calls to server
37+
* @param factory RequestFactory for generating calls to server
38+
*/
39+
public MeilisearchHttpRequest(AbstractHttpClient client, RequestFactory factory) {
40+
this.client = client;
41+
this.factory = factory;
42+
this.jsonHandler = new GsonJsonHandler();
43+
}
44+
45+
/**
46+
* Gets a document at the specified path
47+
*
48+
* @param api Path to document
49+
* @return document that was requested
50+
* @throws MeilisearchException if the response is an error
51+
*/
52+
public String get(String api) throws MeilisearchException {
53+
return this.get(api, "");
54+
}
55+
56+
/**
57+
* Gets a document at the specified path with a given parameter
58+
*
59+
* @param api Path to document
60+
* @param param Parameter to be passed
61+
* @return document that was requested
62+
* @throws MeilisearchException if the response is an error
63+
*/
64+
String get(String api, String param) throws MeilisearchException {
65+
HttpResponse<?> httpResponse =
66+
this.client.get(
67+
factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null));
68+
if (httpResponse.getStatusCode() >= 400) {
69+
throw new MeilisearchApiException(
70+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
71+
}
72+
return new String(httpResponse.getContentAsBytes());
73+
}
74+
75+
/**
76+
* Adds a document to the specified path
77+
*
78+
* @param api Path to server
79+
* @param body Query for search
80+
* @return results of the search
81+
* @throws MeilisearchException if the response is an error
82+
*/
83+
<T> String post(String api, T body) throws MeilisearchException {
84+
HttpResponse<?> httpResponse =
85+
this.client.post(
86+
factory.create(HttpMethod.POST, api, Collections.emptyMap(), body));
87+
if (httpResponse.getStatusCode() >= 400) {
88+
throw new MeilisearchApiException(
89+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
90+
}
91+
return new String(httpResponse.getContentAsBytes());
92+
}
93+
94+
/**
95+
* Replaces the requested resource with new data
96+
*
97+
* @param api Path to the requested resource
98+
* @param body Replacement data for the requested resource
99+
* @return updated resource
100+
* @throws MeilisearchException if the response is an error
101+
*/
102+
<T> String put(String api, T body) throws MeilisearchException {
103+
HttpResponse<?> httpResponse =
104+
this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body));
105+
if (httpResponse.getStatusCode() >= 400) {
106+
throw new MeilisearchApiException(
107+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
108+
}
109+
return new String(httpResponse.getContentAsBytes());
110+
}
111+
112+
/**
113+
* Deletes the specified resource
114+
*
115+
* @param api Path to the requested resource
116+
* @return deleted resource
117+
* @throws MeilisearchException if the response is an error
118+
*/
119+
String delete(String api) throws MeilisearchException {
120+
HttpResponse<?> httpResponse =
121+
this.client.put(
122+
factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null));
123+
if (httpResponse.getStatusCode() >= 400) {
124+
throw new MeilisearchApiException(
125+
jsonHandler.decode(httpResponse.getContent(), APIError.class));
126+
}
127+
return new String(httpResponse.getContentAsBytes());
128+
}
129+
}

0 commit comments

Comments
 (0)