Skip to content

Update Keys API for Meilisearch v.28 #517

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 9 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 19 additions & 6 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 @@ -263,7 +265,7 @@ public void waitForTask(int uid) throws MeilisearchException {
* https://docs.meilisearch.com/reference/api/keys.html#get-one-key
*
* @param uid Identifier of the requested Key
* @return Key Instance
* @return Meilisearch API response as Key Instance
* @throws MeilisearchException if an error occurs
*/
public Key getKey(String uid) throws MeilisearchException {
Expand All @@ -273,18 +275,29 @@ public Key getKey(String uid) throws MeilisearchException {
/**
* Retrieves list of keys https://docs.meilisearch.com/reference/api/keys.html#get-all-keys
*
* @return List of keys in the Meilisearch client
* @return Results containing a list of Key from the Meilisearch API
* @throws MeilisearchException if an error occurs
*/
public Results<Key> getKeys() throws MeilisearchException {
return this.keysHandler.getKeys();
}

/**
* Get list of all API keys https://docs.meilisearch.com/reference/api/keys.html#get-all-keys
*
* @param params query parameters accepted by the get keys route
* @return Results containing a list of Key from the Meilisearch API
* @throws MeilisearchException if an error occurs
*/
public Results<Key> getKeys(KeysQuery params) throws MeilisearchException {
return this.keysHandler.getKeys(params);
}

/**
* Creates a key https://docs.meilisearch.com/reference/api/keys.html#create-a-key
*
* @param options Key containing the options of the key
* @return Key Instance
* @return Meilisearch API response as Key Instance
* @throws MeilisearchException if an error occurs
*/
public Key createKey(Key options) throws MeilisearchException {
Expand All @@ -296,10 +309,10 @@ 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
* @return Meilisearch API response as Key Instance
* @throws MeilisearchException if an error occurs
*/
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
31 changes: 20 additions & 11 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,20 +31,29 @@ 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);
}

/**
* Retrieves keys from the client
*
* @return List of key instance
* @return Results containing a list of Key instance
* @throws MeilisearchException if client request causes an error
*/
Results<Key> getKeys() throws MeilisearchException {
String urlPath = "/keys";
Results<Key> result = httpClient.get(urlPath, Results.class, Key.class);
return result;
return httpClient.get(urlPath, Results.class, Key.class);
}

/**
* Retrieves keys from the client
*
* @param params accept by the keys route
* @return Results containing a list of Key instance
* @throws MeilisearchException if client request causes an error
*/
Results<Key> getKeys(KeysQuery params) throws MeilisearchException {
return httpClient.get(params.toQuery(params), Results.class, Key.class);
}

/**
Expand All @@ -63,11 +74,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);
}

/**
Expand All @@ -77,7 +87,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);
}
}
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();
}
}
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 for updating a Key
*
* <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 the query parameters for the keys routes
*
* <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