From 101647aaa11838a55ddd5ab029ed2996527459a3 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 28 Sep 2022 15:53:39 +0200 Subject: [PATCH 1/2] Change MeiliSearchHttpRequest for MeilisearchHttpRequest --- src/main/java/com/meilisearch/sdk/Documents.java | 6 +++--- src/main/java/com/meilisearch/sdk/IndexesHandler.java | 4 ++-- src/main/java/com/meilisearch/sdk/KeysHandler.java | 4 ++-- .../java/com/meilisearch/sdk/MeilisearchHttpRequest.java | 6 ++++++ src/main/java/com/meilisearch/sdk/Search.java | 4 ++-- src/main/java/com/meilisearch/sdk/SettingsHandler.java | 4 ++-- 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java diff --git a/src/main/java/com/meilisearch/sdk/Documents.java b/src/main/java/com/meilisearch/sdk/Documents.java index 48b7631f..57d8e028 100644 --- a/src/main/java/com/meilisearch/sdk/Documents.java +++ b/src/main/java/com/meilisearch/sdk/Documents.java @@ -6,12 +6,12 @@ import com.meilisearch.sdk.model.Task; import java.util.List; -/** Wrapper around MeiliSearchHttpRequest class to use for Meilisearch documents */ +/** Wrapper around MeilisearchHttpRequest class to use for Meilisearch documents */ class Documents { - private final MeiliSearchHttpRequest meilisearchHttpRequest; + private final MeilisearchHttpRequest meilisearchHttpRequest; protected Documents(Config config) { - meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /** diff --git a/src/main/java/com/meilisearch/sdk/IndexesHandler.java b/src/main/java/com/meilisearch/sdk/IndexesHandler.java index bbae8096..f7fad638 100644 --- a/src/main/java/com/meilisearch/sdk/IndexesHandler.java +++ b/src/main/java/com/meilisearch/sdk/IndexesHandler.java @@ -5,7 +5,7 @@ /** Wrapper around the MeiliSearchHttpRequest class to ease usage for Meilisearch indexes */ class IndexesHandler { - MeiliSearchHttpRequest meilisearchHttpRequest; + MeilisearchHttpRequest meilisearchHttpRequest; /** * Creates and sets up an instance of IndexesHandler to simplify Meilisearch API calls to manage @@ -14,7 +14,7 @@ class IndexesHandler { * @param config Meilisearch configuration */ IndexesHandler(Config config) { - this.meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + this.meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /** diff --git a/src/main/java/com/meilisearch/sdk/KeysHandler.java b/src/main/java/com/meilisearch/sdk/KeysHandler.java index 3f7edc6e..c94c3e56 100644 --- a/src/main/java/com/meilisearch/sdk/KeysHandler.java +++ b/src/main/java/com/meilisearch/sdk/KeysHandler.java @@ -10,7 +10,7 @@ *

Refer https://docs.meilisearch.com/reference/api/keys.html */ public class KeysHandler { - private final MeiliSearchHttpRequest meilisearchHttpRequest; + private final MeilisearchHttpRequest meilisearchHttpRequest; /** * Creates and sets up an instance of Key to simplify Meilisearch API calls to manage keys @@ -18,7 +18,7 @@ public class KeysHandler { * @param config Meilisearch configuration */ public KeysHandler(Config config) { - this.meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + this.meilisearchHttpRequest = new MeilisearchHttpRequest; } /** diff --git a/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java b/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java new file mode 100644 index 00000000..b5a7f93c --- /dev/null +++ b/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java @@ -0,0 +1,6 @@ +public class MeilisearchHttpRequest { + * Constructor for the MeilisearchHttpRequest + public MeilisearchHttpRequest(Config config) { + * Constructor for the MeilisearchHttpRequest + * @param request Requestrequest for generating calls to server + public MeilisearchHttpRequest(AbstractHttpClient client, BasicRequest request) { diff --git a/src/main/java/com/meilisearch/sdk/Search.java b/src/main/java/com/meilisearch/sdk/Search.java index f05b6e90..cce15cd3 100644 --- a/src/main/java/com/meilisearch/sdk/Search.java +++ b/src/main/java/com/meilisearch/sdk/Search.java @@ -5,7 +5,7 @@ /** Search Object for searching on indexes */ public class Search { - private final MeiliSearchHttpRequest meilisearchHttpRequest; + private final MeilisearchHttpRequest meilisearchHttpRequest; /** * Constructor for the Meilisearch Search object @@ -13,7 +13,7 @@ public class Search { * @param config Meilisearch configuration */ protected Search(Config config) { - meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /** diff --git a/src/main/java/com/meilisearch/sdk/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/SettingsHandler.java index 01d5c1cf..f3209a96 100644 --- a/src/main/java/com/meilisearch/sdk/SettingsHandler.java +++ b/src/main/java/com/meilisearch/sdk/SettingsHandler.java @@ -12,7 +12,7 @@ *

Refer https://docs.meilisearch.com/reference/api/settings.html */ public class SettingsHandler { - private final MeiliSearchHttpRequest meilisearchHttpRequest; + private final MeilisearchHttpRequest meilisearchHttpRequest; /** * Constructor for the Meilisearch Settings object @@ -20,7 +20,7 @@ public class SettingsHandler { * @param config Meilisearch configuration */ public SettingsHandler(Config config) { - meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /** From 1cd90355dde88d5d6fa7b1c49c2990b804417419 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 28 Sep 2022 16:20:09 +0200 Subject: [PATCH 2/2] Change MeiliSearchHttpRequest for MeilisearchHttpRequest --- src/main/java/com/meilisearch/sdk/Index.java | 2 +- .../java/com/meilisearch/sdk/KeysHandler.java | 2 +- .../sdk/MeiliSearchHttpRequest.java | 129 ------------------ .../sdk/MeilisearchHttpRequest.java | 127 ++++++++++++++++- .../com/meilisearch/sdk/TasksHandler.java | 4 +- 5 files changed, 129 insertions(+), 135 deletions(-) delete mode 100644 src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java diff --git a/src/main/java/com/meilisearch/sdk/Index.java b/src/main/java/com/meilisearch/sdk/Index.java index 3dd02910..e93db3a2 100644 --- a/src/main/java/com/meilisearch/sdk/Index.java +++ b/src/main/java/com/meilisearch/sdk/Index.java @@ -651,7 +651,7 @@ public void waitForTask(int taskId, int timeoutInMs, int intervalInMs) */ public void fetchPrimaryKey() throws MeilisearchException { String requestQuery = "/indexes/" + this.uid; - MeiliSearchHttpRequest meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + MeilisearchHttpRequest meilisearchHttpRequest = new MeilisearchHttpRequest(config); Index retrievedIndex = config.jsonHandler.decode(meilisearchHttpRequest.get(requestQuery), Index.class); this.primaryKey = retrievedIndex.getPrimaryKey(); diff --git a/src/main/java/com/meilisearch/sdk/KeysHandler.java b/src/main/java/com/meilisearch/sdk/KeysHandler.java index c94c3e56..5e9f01f0 100644 --- a/src/main/java/com/meilisearch/sdk/KeysHandler.java +++ b/src/main/java/com/meilisearch/sdk/KeysHandler.java @@ -18,7 +18,7 @@ public class KeysHandler { * @param config Meilisearch configuration */ public KeysHandler(Config config) { - this.meilisearchHttpRequest = new MeilisearchHttpRequest; + this.meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /** diff --git a/src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java b/src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java deleted file mode 100644 index f5d660d3..00000000 --- a/src/main/java/com/meilisearch/sdk/MeiliSearchHttpRequest.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.meilisearch.sdk; - -import com.meilisearch.sdk.exceptions.APIError; -import com.meilisearch.sdk.exceptions.MeilisearchApiException; -import com.meilisearch.sdk.exceptions.MeilisearchException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.DefaultHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import com.meilisearch.sdk.http.response.HttpResponse; -import com.meilisearch.sdk.json.GsonJsonHandler; -import com.meilisearch.sdk.json.JsonHandler; -import java.util.Collections; - -/** The HTTP requests for the different functions to be done through Meilisearch */ -public class MeiliSearchHttpRequest { - private final AbstractHttpClient client; - private final RequestFactory factory; - protected final JsonHandler jsonHandler; - - /** - * Constructor for the MeiliSearchHttpRequest - * - * @param config Meilisearch configuration - */ - public MeiliSearchHttpRequest(Config config) { - this.client = new DefaultHttpClient(config); - this.jsonHandler = config.jsonHandler; - this.factory = new BasicRequestFactory(jsonHandler); - } - - /** - * Constructor for the MeiliSearchHttpRequest - * - * @param client HttpClient for making calls to server - * @param factory RequestFactory for generating calls to server - */ - public MeiliSearchHttpRequest(AbstractHttpClient client, RequestFactory factory) { - this.client = client; - this.factory = factory; - this.jsonHandler = new GsonJsonHandler(); - } - - /** - * Gets a document at the specified path - * - * @param api Path to document - * @return document that was requested - * @throws MeilisearchException if the response is an error - */ - public String get(String api) throws MeilisearchException { - return this.get(api, ""); - } - - /** - * Gets a document at the specified path with a given parameter - * - * @param api Path to document - * @param param Parameter to be passed - * @return document that was requested - * @throws MeilisearchException if the response is an error - */ - String get(String api, String param) throws MeilisearchException { - HttpResponse httpResponse = - this.client.get( - factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null)); - if (httpResponse.getStatusCode() >= 400) { - throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); - } - return new String(httpResponse.getContentAsBytes()); - } - - /** - * Adds a document to the specified path - * - * @param api Path to server - * @param body Query for search - * @return results of the search - * @throws MeilisearchException if the response is an error - */ - String post(String api, T body) throws MeilisearchException { - HttpResponse httpResponse = - this.client.post( - factory.create(HttpMethod.POST, api, Collections.emptyMap(), body)); - if (httpResponse.getStatusCode() >= 400) { - throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); - } - return new String(httpResponse.getContentAsBytes()); - } - - /** - * Replaces the requested resource with new data - * - * @param api Path to the requested resource - * @param body Replacement data for the requested resource - * @return updated resource - * @throws MeilisearchException if the response is an error - */ - String put(String api, T body) throws MeilisearchException { - HttpResponse httpResponse = - this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body)); - if (httpResponse.getStatusCode() >= 400) { - throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); - } - return new String(httpResponse.getContentAsBytes()); - } - - /** - * Deletes the specified resource - * - * @param api Path to the requested resource - * @return deleted resource - * @throws MeilisearchException if the response is an error - */ - String delete(String api) throws MeilisearchException { - HttpResponse httpResponse = - this.client.put( - factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null)); - if (httpResponse.getStatusCode() >= 400) { - throw new MeilisearchApiException( - jsonHandler.decode(httpResponse.getContent(), APIError.class)); - } - return new String(httpResponse.getContentAsBytes()); - } -} diff --git a/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java b/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java index b5a7f93c..4a0b114b 100644 --- a/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java +++ b/src/main/java/com/meilisearch/sdk/MeilisearchHttpRequest.java @@ -1,6 +1,129 @@ +package com.meilisearch.sdk; + +import com.meilisearch.sdk.exceptions.APIError; +import com.meilisearch.sdk.exceptions.MeilisearchApiException; +import com.meilisearch.sdk.exceptions.MeilisearchException; +import com.meilisearch.sdk.http.AbstractHttpClient; +import com.meilisearch.sdk.http.DefaultHttpClient; +import com.meilisearch.sdk.http.factory.BasicRequestFactory; +import com.meilisearch.sdk.http.factory.RequestFactory; +import com.meilisearch.sdk.http.request.HttpMethod; +import com.meilisearch.sdk.http.response.HttpResponse; +import com.meilisearch.sdk.json.GsonJsonHandler; +import com.meilisearch.sdk.json.JsonHandler; +import java.util.Collections; + +/** The HTTP requests for the different functions to be done through Meilisearch */ public class MeilisearchHttpRequest { + private final AbstractHttpClient client; + private final RequestFactory factory; + protected final JsonHandler jsonHandler; + + /** * Constructor for the MeilisearchHttpRequest + * + * @param config Meilisearch configuration + */ public MeilisearchHttpRequest(Config config) { + this.client = new DefaultHttpClient(config); + this.jsonHandler = config.jsonHandler; + this.factory = new BasicRequestFactory(jsonHandler); + } + + /** * Constructor for the MeilisearchHttpRequest - * @param request Requestrequest for generating calls to server - public MeilisearchHttpRequest(AbstractHttpClient client, BasicRequest request) { + * + * @param client HttpClient for making calls to server + * @param factory RequestFactory for generating calls to server + */ + public MeilisearchHttpRequest(AbstractHttpClient client, RequestFactory factory) { + this.client = client; + this.factory = factory; + this.jsonHandler = new GsonJsonHandler(); + } + + /** + * Gets a document at the specified path + * + * @param api Path to document + * @return document that was requested + * @throws MeilisearchException if the response is an error + */ + public String get(String api) throws MeilisearchException { + return this.get(api, ""); + } + + /** + * Gets a document at the specified path with a given parameter + * + * @param api Path to document + * @param param Parameter to be passed + * @return document that was requested + * @throws MeilisearchException if the response is an error + */ + String get(String api, String param) throws MeilisearchException { + HttpResponse httpResponse = + this.client.get( + factory.create(HttpMethod.GET, api + param, Collections.emptyMap(), null)); + if (httpResponse.getStatusCode() >= 400) { + throw new MeilisearchApiException( + jsonHandler.decode(httpResponse.getContent(), APIError.class)); + } + return new String(httpResponse.getContentAsBytes()); + } + + /** + * Adds a document to the specified path + * + * @param api Path to server + * @param body Query for search + * @return results of the search + * @throws MeilisearchException if the response is an error + */ + String post(String api, T body) throws MeilisearchException { + HttpResponse httpResponse = + this.client.post( + factory.create(HttpMethod.POST, api, Collections.emptyMap(), body)); + if (httpResponse.getStatusCode() >= 400) { + throw new MeilisearchApiException( + jsonHandler.decode(httpResponse.getContent(), APIError.class)); + } + return new String(httpResponse.getContentAsBytes()); + } + + /** + * Replaces the requested resource with new data + * + * @param api Path to the requested resource + * @param body Replacement data for the requested resource + * @return updated resource + * @throws MeilisearchException if the response is an error + */ + String put(String api, T body) throws MeilisearchException { + HttpResponse httpResponse = + this.client.put(factory.create(HttpMethod.PUT, api, Collections.emptyMap(), body)); + if (httpResponse.getStatusCode() >= 400) { + throw new MeilisearchApiException( + jsonHandler.decode(httpResponse.getContent(), APIError.class)); + } + return new String(httpResponse.getContentAsBytes()); + } + + /** + * Deletes the specified resource + * + * @param api Path to the requested resource + * @return deleted resource + * @throws MeilisearchException if the response is an error + */ + String delete(String api) throws MeilisearchException { + HttpResponse httpResponse = + this.client.put( + factory.create(HttpMethod.DELETE, api, Collections.emptyMap(), null)); + if (httpResponse.getStatusCode() >= 400) { + throw new MeilisearchApiException( + jsonHandler.decode(httpResponse.getContent(), APIError.class)); + } + return new String(httpResponse.getContentAsBytes()); + } +} diff --git a/src/main/java/com/meilisearch/sdk/TasksHandler.java b/src/main/java/com/meilisearch/sdk/TasksHandler.java index fd23de88..4c3ebe2d 100644 --- a/src/main/java/com/meilisearch/sdk/TasksHandler.java +++ b/src/main/java/com/meilisearch/sdk/TasksHandler.java @@ -12,7 +12,7 @@ *

Refer https://docs.meilisearch.com/reference/api/tasks.html */ public class TasksHandler { - private final MeiliSearchHttpRequest meilisearchHttpRequest; + private final MeilisearchHttpRequest meilisearchHttpRequest; public static final String SUCCEEDED = "succeeded"; public static final String FAILED = "failed"; @@ -22,7 +22,7 @@ public class TasksHandler { * @param config MeiliSearch configuration */ public TasksHandler(Config config) { - this.meilisearchHttpRequest = new MeiliSearchHttpRequest(config); + this.meilisearchHttpRequest = new MeilisearchHttpRequest(config); } /**