Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
17 changes: 15 additions & 2 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.Key;
import com.meilisearch.sdk.model.KeyUpdate;
import com.meilisearch.sdk.model.KeysQuery;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.Stats;
import com.meilisearch.sdk.model.Task;
Expand Down Expand Up @@ -280,6 +282,17 @@ public Results<Key> getKeys() throws MeilisearchException {
return this.keysHandler.getKeys();
}

/**
* Retrieves list of keys https://docs.meilisearch.com/reference/api/keys.html#get-all-keys
Comment thread
alallema marked this conversation as resolved.
Resolved
*
* @param param accept by the keys route
Comment thread
alallema marked this conversation as resolved.
Resolved
* @return List of keys in the Meilisearch client
Comment thread
alallema marked this conversation as resolved.
Resolved
* @throws MeilisearchException if an error occurs
*/
public Results<Key> getKeys(KeysQuery param) throws MeilisearchException {
return this.keysHandler.getKeys(param);
}

/**
* Creates a key https://docs.meilisearch.com/reference/api/keys.html#create-a-key
*
Expand All @@ -297,9 +310,9 @@ public Key createKey(Key options) throws MeilisearchException {
* @param key String containing the key
* @param options String containing the options to update
* @return Key Instance
* @throws Exception if client request causes an error
* @throws MeilisearchException if client request causes an error
Comment thread
alallema marked this conversation as resolved.
Resolved
*/
public Key updateKey(String key, Key options) throws Exception {
public Key updateKey(String key, KeyUpdate options) throws MeilisearchException {
return this.keysHandler.updateKey(key, options);
}

Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/meilisearch/sdk/KeysHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.Key;
import com.meilisearch.sdk.model.KeyUpdate;
import com.meilisearch.sdk.model.KeysQuery;
import com.meilisearch.sdk.model.Results;

/**
Expand Down Expand Up @@ -29,8 +31,7 @@ public class KeysHandler {
* @throws MeilisearchException if client request causes an error
*/
Key getKey(String uid) throws MeilisearchException {
String urlPath = "/keys/" + uid;
return httpClient.get(urlPath, Key.class);
return httpClient.get(new KeysQuery().toQuery(uid), Key.class);
Comment thread
alallema marked this conversation as resolved.
Resolved
}

/**
Expand All @@ -45,6 +46,18 @@ Results<Key> getKeys() throws MeilisearchException {
return result;
}

/**
* Retrieves keys from the client
Comment thread
alallema marked this conversation as resolved.
Resolved
*
* @param param accept by the keys route
* @return List of key instance
Comment thread
alallema marked this conversation as resolved.
Resolved
* @throws MeilisearchException if client request causes an error
*/
Results<Key> getKeys(KeysQuery param) throws MeilisearchException {
Results<Key> result = httpClient.get(param.toQuery(param), Results.class, Key.class);
return result;
Comment thread
alallema marked this conversation as resolved.
Resolved
}

/**
* Creates a key
*
Expand All @@ -63,11 +76,10 @@ Key createKey(Key options) throws MeilisearchException {
* @param key String containing the key
* @param options String containing the options of the key
* @return Key Instance
* @throws Exception if client request causes an error
* @throws MeilisearchException if client request causes an error
*/
Key updateKey(String key, Key options) throws Exception {
String urlPath = "/keys/" + key;
return httpClient.patch(urlPath, options, Key.class);
Key updateKey(String key, KeyUpdate options) throws MeilisearchException {
return httpClient.patch(new KeysQuery().toQuery(key), options, Key.class);
Comment thread
alallema marked this conversation as resolved.
Resolved
}

/**
Expand All @@ -77,7 +89,6 @@ Key updateKey(String key, Key options) throws Exception {
* @throws MeilisearchException if client request causes an error
*/
void deleteKey(String key) throws MeilisearchException {
String urlPath = "/keys/" + key;
httpClient.delete(urlPath, String.class);
httpClient.delete(new KeysQuery().toQuery(key), String.class);
Comment thread
alallema marked this conversation as resolved.
Resolved
}
}
18 changes: 16 additions & 2 deletions src/main/java/com/meilisearch/sdk/json/GsonJsonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.meilisearch.sdk.exceptions.JsonDecodingException;
Expand All @@ -26,9 +29,20 @@ public String encode(Object o) throws MeilisearchException {
return (String) o;
}
// TODO: review later
// This is a workaround to encode the Key class properly
if (o != null && o.getClass() == Key.class) {
GsonBuilder builder = new GsonBuilder();
this.gson = builder.serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
this.gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();

Key key = (Key) o;
if (key.getExpiresAt() == null) {
JsonElement jsonElement = gson.toJsonTree(o);
JsonObject jsonObject = jsonElement.getAsJsonObject();
jsonObject.add("expiresAt", JsonNull.INSTANCE);
o = jsonObject;
this.gson =
builder.serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
Comment thread
bidoubiwa marked this conversation as resolved.
Resolved
}
}
try {
return gson.toJson(o);
Expand All @@ -52,7 +66,7 @@ public <T> T decode(Object o, Class<?> targetClass, Class<?>... parameters)
return gson.<T>fromJson((String) o, targetClass);
} else {
TypeToken<?> parameterized = TypeToken.getParameterized(targetClass, parameters);
return gson.fromJson((String) o, parameterized.getType());
return gson.<T>fromJson((String) o, parameterized.getType());
}
} catch (JsonSyntaxException e) {
throw new JsonDecodingException(e);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/meilisearch/sdk/model/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
*/
@Getter
public class Key {
@Setter
@Accessors(chain = true)
protected String name = null;

@Setter
@Accessors(chain = true)
protected String description = null;

protected String key = "";
protected String uid = null;

protected String key = null;

@Setter
@Accessors(chain = true)
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/KeyUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.meilisearch.sdk.model;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
* Data structure of Meilisearch response for a Key
Comment thread
alallema marked this conversation as resolved.
Resolved
*
* <p>https://docs.meilisearch.com/reference/api/keys.html
*/
@Getter
public class KeyUpdate {
@Setter
@Accessors(chain = true)
protected String name = null;

@Setter
@Accessors(chain = true)
protected String description = null;

public KeyUpdate() {}
}
35 changes: 35 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/KeysQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.meilisearch.sdk.model;

import com.meilisearch.sdk.http.URLBuilder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
* Data structure of a query parameter for keys route
Comment thread
alallema marked this conversation as resolved.
Resolved
*
* <p>https://docs.meilisearch.com/reference/api/keys.html#query-parameters
*/
@Setter
@Getter
@Accessors(chain = true)
public class KeysQuery {
private int offset = -1;
private int limit = -1;

public KeysQuery() {}

public String toQuery(String key) {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("keys").addSubroute(key);
return urlb.getURL();
}

public String toQuery(KeysQuery param) {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("keys")
.addParameter("limit", param.getLimit())
.addParameter("offset", param.getOffset());
return urlb.getURL();
}
}
Loading