Skip to content

Refactorization of the SDK #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 96 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
441ce02
Switch to v0.27.2 to fix the tests
alallema Aug 4, 2022
6a4db61
Remove Dump and DumpHandler
alallema Aug 8, 2022
0ee8a43
relaunch ci with good base branch
alallema Aug 9, 2022
e5b4696
Remove duplicate and useless code
alallema Aug 9, 2022
91f49b2
Move all Class for data struct in model directory
alallema Aug 9, 2022
44165b4
Normalize json handler
alallema Aug 17, 2022
d2a40dd
Modification due to review
alallema Aug 24, 2022
7713cf4
Add setQuery method
alallema Sep 5, 2022
4a17c02
Fix JacksonJsonHandler
alallema Sep 7, 2022
cafed1c
Update Readme about jsonHandler
alallema Sep 8, 2022
12e6976
Remove JsonbJson from json library
alallema Sep 10, 2022
6fcca4d
Adding Accessor
alallema Sep 13, 2022
5d44508
Normalize exception
alallema Sep 13, 2022
69b4e79
Add test for CommunicationException
alallema Sep 13, 2022
d44705d
Throw MeilisearchException from each SDK method
alallema Sep 13, 2022
f431e0a
Test rename
alallema Sep 13, 2022
8e2ed57
Move meilisearch exception at the http interface
alallema Sep 14, 2022
5f90cda
Change MeiliSearch to Meilisearch
alallema Sep 14, 2022
16b345c
Modification due to review
alallema Sep 14, 2022
198da1a
Merge pull request #439 from meilisearch/renaming
alallema Sep 14, 2022
364293c
Update src/test/java/com/meilisearch/integration/ExceptionsTest.java
alallema Sep 14, 2022
bff5070
Add test for exceptions
alallema Sep 15, 2022
e63713b
Merge pull request #438 from meilisearch/exception
alallema Sep 19, 2022
101647a
Change MeiliSearchHttpRequest for MeilisearchHttpRequest
alallema Sep 28, 2022
1cd9035
Change MeiliSearchHttpRequest for MeilisearchHttpRequest
alallema Sep 28, 2022
2393779
Merge pull request #447 from meilisearch/typo-meilisearchhttprequest
alallema Sep 28, 2022
cf4fe71
Move MeilisearchHttpClient to Config
alallema Sep 28, 2022
9549251
Merge pull request #448 from meilisearch/http-client-in-config
alallema Sep 28, 2022
647e7e0
Use OkHttpClient by default and remove the others
alallema Sep 28, 2022
5aca6f0
Remove library from gradle file
alallema Sep 28, 2022
6d63165
Merge pull request #449 from meilisearch/clean-client
alallema Sep 28, 2022
875c9f1
Redesign Request and Response without factory
alallema Sep 28, 2022
92e2d8d
Add toString method to SearchRequest Class
alallema Sep 28, 2022
dfd6f74
Switch put to putOpt and clearify tests
alallema Oct 11, 2022
a2b86e7
Update src/main/java/com/meilisearch/sdk/SearchRequest.java
alallema Oct 13, 2022
f916319
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
7799b3a
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
810313d
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
ad9315a
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
10168ed
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
b011219
Update src/test/java/com/meilisearch/sdk/SearchRequestTest.java
alallema Oct 13, 2022
e738626
Merge pull request #451 from meilisearch/search-request
alallema Oct 13, 2022
858d2de
Update src/main/java/com/meilisearch/sdk/http/CustomOkHttpClient.java
alallema Oct 13, 2022
1da8674
Remove useless cast
alallema Oct 13, 2022
b23e770
Merge pull request #450 from meilisearch/request
alallema Oct 13, 2022
fee919a
Rename and remove abstraction from HttpClient class
alallema Oct 13, 2022
d76a4df
Update src/main/java/com/meilisearch/sdk/Documents.java
alallema Oct 18, 2022
4fdc210
Update src/main/java/com/meilisearch/sdk/Documents.java
alallema Oct 18, 2022
6e23f7e
Update src/main/java/com/meilisearch/sdk/Documents.java
alallema Oct 18, 2022
9841ef6
Update src/main/java/com/meilisearch/sdk/Documents.java
alallema Oct 18, 2022
9986026
Style
alallema Oct 18, 2022
147ce19
Merge pull request #456 from meilisearch/http-client-simplifying
alallema Oct 18, 2022
82d32f9
Modification of the get method with generic parameters
alallema Oct 24, 2022
0fd3466
Clarify get method
alallema Oct 25, 2022
7c8524f
Clarify httpresponse content
alallema Oct 25, 2022
b9b29f1
Add space
alallema Oct 25, 2022
67a3e9d
Provide a generic to http post method
alallema Oct 25, 2022
0ec8dd9
Add generic type to HttpResponse
alallema Oct 25, 2022
46f8c55
Provide a generic to http put method
alallema Oct 25, 2022
06d74d5
Provide a generic to http delete method
alallema Oct 25, 2022
d295326
Remove unused method
alallema Oct 26, 2022
12202db
Merge pull request #468 from meilisearch/patch-post-method
alallema Oct 26, 2022
8f9e509
Merge pull request #469 from meilisearch/patch-put-method
alallema Oct 26, 2022
5e7757a
Fix extra curly bracket
alallema Oct 26, 2022
662fcc3
Merge pull request #470 from meilisearch/patch-delete-method
alallema Oct 26, 2022
5380e4e
Merge pull request #466 from meilisearch/patch-get-method
alallema Oct 26, 2022
d322d75
Adding patch method and tests
alallema Oct 26, 2022
3091dfc
Update src/main/java/com/meilisearch/sdk/HttpClient.java
alallema Oct 26, 2022
328b43d
Update src/main/java/com/meilisearch/sdk/HttpClient.java
alallema Oct 26, 2022
dc661ad
Add instance handler for missing method
alallema Oct 26, 2022
49518e6
Merge pull request #472 from meilisearch/patch-method
alallema Oct 26, 2022
68e5de4
Merge pull request #473 from meilisearch/missing-method
alallema Oct 26, 2022
4bc3220
Add test for basic http response and request
alallema Oct 26, 2022
f3551c8
Clean methods definitions
alallema Oct 26, 2022
64e8412
Add updateKey method
alallema Oct 26, 2022
17bce6c
Rename getAllIndexes in getIndexes
alallema Oct 27, 2022
a484a6b
Merge pull request #474 from meilisearch/basic_http_test
alallema Oct 27, 2022
23669b3
Merge pull request #475 from meilisearch/clean-documentation
alallema Oct 27, 2022
9925fa0
Merge branch 'refactor' into update-key
alallema Oct 27, 2022
4d4c32c
Merge pull request #477 from meilisearch/rename-index-list
alallema Oct 27, 2022
2505ca6
Merge pull request #476 from meilisearch/update-key
alallema Oct 27, 2022
e7810d0
MOdification due to review
alallema Nov 7, 2022
57fdd3f
Add test for jackson json handler
alallema Nov 7, 2022
75095ef
Add constructor
alallema Nov 7, 2022
0abbf3b
Remaned variables
alallema Nov 7, 2022
71fd604
Add test for jackson json handler
alallema Nov 7, 2022
6840d4a
Modify setQuery return
alallema Nov 7, 2022
c98b507
Changes Details class name
alallema Nov 8, 2022
06b4743
Update README.md
alallema Nov 8, 2022
70eb6a2
Update README.md
alallema Nov 8, 2022
ab9aa0b
Update README.md
alallema Nov 8, 2022
4e57da6
Update README.md
alallema Nov 8, 2022
544f53f
Update README.md
alallema Nov 8, 2022
f86a505
Improve README
alallema Nov 8, 2022
23e186a
Update README.md
alallema Nov 8, 2022
1621246
Simplify Config class
alallema Nov 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
get_one_index_1: |-
client.getIndex("movies");
list_all_indexes_1: |-
client.getAllIndexes();
client.getIndexes();
create_an_index_1: |-
client.createIndex("movies", "id");
update_an_index_1: |-
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jobs:
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Meilisearch (latest version) setup with Docker
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest meilisearch --no-analytics --master-key='masterKey'
- name: Meilisearch (v0.27.2 version) setup with Docker
run: docker run -d -p 7700:7700 getmeili/meilisearch:v0.27.2 meilisearch --no-analytics --master-key='masterKey'
- name: Build and run unit and integration tests
run: ./gradlew build integrationTest
- name: Archive test report
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ You can set up your local environment natively or using `docker`, check out the

Example of running all the checks with docker:
```bash
docker-compose run --rm package bash -c "./gradlew build && ./gradlew test IntegrationTest"
docker-compose run --rm package bash -c "./gradlew build && ./gradlew build integrationTest"
```

To install dependencies:
Expand Down
102 changes: 17 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,109 +223,41 @@ index.search(

### JSON <!-- omit in toc -->

#### Basic JSON <!-- omit in toc -->
#### Default JSON `GsonJsonHandler` <!-- omit in toc -->

The default JSON can be created by calling the default constructor of `JsonbJsonHandler` class which will create a config of type `JsonbConfig` and using this config. It will initialize the mapper variable by calling the create method of `JsonbBuilder` class.
The default JSON library is `Gson`. You can however use another library with the `JsonHandler` Class.

#### Creating a Custom `GsonJsonHandler` <!-- omit in toc -->
*Notes*: We strongly recommend using the `Gson` library.

To create a custom JSON handler, create an object of GsonJsonHandler and send the GSON object in the parameterized constructor.<br>
#### Using `JacksonJsonHandler` <!-- omit in toc -->

```java
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.serializeNulls()
.create();
private GsonJsonHandler jsonGson = new GsonJsonHandler(gson);
jsonGson.encode("your_data");
```

#### Creating a Custom `JacksonJsonHandler` <!-- omit in toc -->

Another method is to create an object of `JacksonJsonHandler` and set the required parameters. The supported option is an object of `ObjectMapper`. It's passed as a parameter to the `JacksonJsonHandler`’s parameterized constructor. This is used to initialize the mapper variable.

The mapper variable is responsible for the encoding and decoding of the JSON.

Using the custom JSON:

```java
Config config = new Config("http://localhost:7700", "masterKey");
HttpAsyncClient client = HttpAsyncClients.createDefault();
ApacheHttpClient client = new ApacheHttpClient(config, client);
private final JsonHandler jsonHandler = new JacksonJsonHandler(new ObjectMapper());
private final RequestFactory requestFactory = new BasicRequestFactory(jsonHandler);
private final GenericServiceTemplate serviceTemplate = new GenericServiceTemplate(client, jsonHandler, requestFactory);

private final ServiceTemplate serviceTemplate;
serviceTemplate.getProcessor().encode("your_data");
```

### Creating a Custom `JsonbJsonHandler <!-- omit in toc -->

Another method of creating a JSON handler is to create an object of `JsonbJsonHandler` and send the `Jsonb` object to the parameterized constructor.
Initialize your `Config` and assign it a new `JacksonJsonHandler` object as `JsonHandler`.
Set up your `Client` with it.

```java
Jsonb jsonb = JsonbBuilder.create();
private JsonbJsonHandler jsonbHandler = new JsonbJsonHandler(jsonb);
jsonbHandler.encode("your_data");
```

### Custom Client <!-- omit in toc -->
import com.meilisearch.sdk.json.JacksonJsonHandler;

To create a custom `Client` handler, create an object of `Client` and set the required parameters.

A `Config` object should be passed, containing your host URL and your API key.

```java
Config config = new Config("http://localhost:7700", "masterKey");
return new Client(config);
config.setJsonHandler(new JacksonJsonHandler());
Client client = new Client(config);
```

The `Client(config)` constructor sets the config instance to the member variable. It also sets the 3 other instances namely `gson()`, `IndexesHandler(config)` and `DumpHandler(config)`.
#### Use a Custom `JsonHandler` <!-- omit in toc -->

Using the custom `Client`:
To create your own JSON handler, you must conform to the `JsonHandler` interface by implementing its two methods.

```java
Config config = new Config("http://localhost:7700", "masterKey");
HttpAsyncClient client = HttpAsyncClients.createDefault();
ApacheHttpClient customClient = new ApacheHttpClient(config, client);
customClient.index("movies").search("American ninja");
```

#### Custom Http Request <!-- omit in toc -->

To create a custom HTTP request, create an object of `BasicHttpRequest` and set the required parameters.
String encode(Object o) throws Exception;

The supported options are as follows:

1. HTTP method: a `String` that can be set as following values: `HEAD`, `GET`, `POST`, `PUT`, or `DELETE`.
2. Path: a `String` corresponding to the endpoint of the API.
3. Headers: a `Map<String,String>` containing the header parameters in the form of key-value pair.
4. Content: the `String` of your content.

```java
return new BasicHttpRequest(
method,
path,
headers,
content == null ? null : this.jsonHandler.encode(content));
<T> T decode(Object o, Class<?> targetClass, Class<?>... parameters) throws Exception;
```

Alternatively, there is an interface `RequestFactory` which has a method `create`.<br>
In order to call this method, create an object of `RequestFactory` and call the method by passing the required parameters.

Using the custom Http Request:
Then create your client by initializing your `Config` with your new handler.

```java
public interface RequestFactory {
<T> HttpRequest<?> create(
HttpMethod method, String path, Map<String, String> headers, T content);
}

private final RequestFactory requestFactory;
requestFactory.create(HttpMethod.GET, "/health", Collections.emptyMap(), {"id":"3"});
Config config = new Config("http://localhost:7700", "masterKey");
config.setJsonHandler(new myJsonHandler());
Client client = new Client(config);
```

## 🤖 Compatibility with Meilisearch
Expand Down
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ dependencies {
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.json:json:20220320'
// https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5
compileOnly group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.1.3'
compileOnly 'com.squareup.okhttp3:okhttp:4.10.0'

// Use JUnit test framework
Expand All @@ -56,9 +55,7 @@ dependencies {
testImplementation 'org.hamcrest:hamcrest:2.2'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.2.2'
compileOnly group: 'jakarta.json.bind', name: 'jakarta.json.bind-api', version: '2.0.0'
testImplementation group: 'org.eclipse', name: 'yasson', version: '2.0.4'
compileOnly group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'

// Lombok
compileOnly 'org.projectlombok:lombok:1.18.24'
Expand Down
Loading