Skip to content

Commit

Permalink
Add update acl
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Grand committed Sep 12, 2014
1 parent 4c9fa9d commit 2fef3ce
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/main/java/com/algolia/search/saas/APIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ public JSONObject addUserKey(List<String> acls) throws AlgoliaException {
return postRequest("/1/keys", jsonObject.toString());
}

/**
* Update a user key
*
* @param acls the list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
*/
public JSONObject updateUserKey(String key, List<String> acls) throws AlgoliaException {
JSONArray array = new JSONArray(acls);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("acl", array);
} catch (JSONException e) {
throw new RuntimeException(e); // $COVERAGE-IGNORE$
}
return putRequest("/1/keys/" + key, jsonObject.toString());
}

/**
* Create a new user key
*
Expand All @@ -288,6 +311,25 @@ public JSONObject addUserKey(List<String> acls) throws AlgoliaException {
public JSONObject addUserKey(List<String> acls, int validity, int maxQueriesPerIPPerHour, int maxHitsPerQuery) throws AlgoliaException {
return addUserKey(acls, validity, maxQueriesPerIPPerHour, maxHitsPerQuery, null);
}

/**
* Update a user key
*
* @param acls the list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
* @param maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (no rate limit).
* @param maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
*/
public JSONObject updateUserKey(String key, List<String> acls, int validity, int maxQueriesPerIPPerHour, int maxHitsPerQuery) throws AlgoliaException {
return updateUserKey(key, acls, validity, maxQueriesPerIPPerHour, maxHitsPerQuery, null);
}

/**
* Create a new user key
Expand Down Expand Up @@ -322,6 +364,39 @@ public JSONObject addUserKey(List<String> acls, int validity, int maxQueriesPerI
return postRequest("/1/keys", jsonObject.toString());
}

/**
* Update a user key
*
* @param acls the list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
* @param maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (no rate limit).
* @param maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
* @param indexes the list of targeted indexes
*/
public JSONObject updateUserKey(String key, List<String> acls, int validity, int maxQueriesPerIPPerHour, int maxHitsPerQuery, List<String> indexes) throws AlgoliaException {
JSONArray array = new JSONArray(acls);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("acl", array);
jsonObject.put("validity", validity);
jsonObject.put("maxQueriesPerIPPerHour", maxQueriesPerIPPerHour);
jsonObject.put("maxHitsPerQuery", maxHitsPerQuery);
if (indexes != null) {
jsonObject.put("indexes", new JSONArray(indexes));
}
} catch (JSONException e) {
throw new RuntimeException(e); // $COVERAGE-IGNORE$
}
return putRequest("/1/keys/" + key, jsonObject.toString());
}

/**
* Generate a secured and public API Key from a list of tagFilters and an
* optional user token identifying the current user
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/algolia/search/saas/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,29 @@ public JSONObject addUserKey(List<String> acls) throws AlgoliaException {
return client.postRequest("/1/indexes/" + encodedIndexName + "/keys", jsonObject.toString());
}

/**
* Update a user key
*
* @param acls the list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
*/
public JSONObject updateUserKey(String key, List<String> acls) throws AlgoliaException {
JSONArray array = new JSONArray(acls);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("acl", array);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return client.putRequest("/1/indexes/" + encodedIndexName + "/keys/" + key, jsonObject.toString());
}

/**
* Create a new user key
*
Expand Down Expand Up @@ -592,4 +615,33 @@ public JSONObject addUserKey(List<String> acls, int validity, int maxQueriesPerI
}
return client.postRequest("/1/indexes/" + encodedIndexName + "/keys", jsonObject.toString());
}

/**
* Update a user key
*
* @param acls the list of ACL for this key. Defined by an array of strings that
* can contains the following values:
* - search: allow to search (https and http)
* - addObject: allows to add/update an object in the index (https only)
* - deleteObject : allows to delete an existing object (https only)
* - deleteIndex : allows to delete index content (https only)
* - settings : allows to get index settings (https only)
* - editSettings : allows to change index settings (https only)
* @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
* @param maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (no rate limit).
* @param maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
*/
public JSONObject updateUserKey(String key, List<String> acls, int validity, int maxQueriesPerIPPerHour, int maxHitsPerQuery) throws AlgoliaException {
JSONArray array = new JSONArray(acls);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("acl", array);
jsonObject.put("validity", validity);
jsonObject.put("maxQueriesPerIPPerHour", maxQueriesPerIPPerHour);
jsonObject.put("maxHitsPerQuery", maxHitsPerQuery);
} catch (JSONException e) {
throw new RuntimeException(e);
}
return client.putRequest("/1/indexes/" + encodedIndexName + "/keys/" + key, jsonObject.toString());
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/algolia/search/saas/SimpleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ public void test18_user_key_index() throws AlgoliaException, JSONException {
assertTrue(isPresent(res.getJSONArray("keys"), newKey.getString("key"), "value"));
JSONObject getKey = index.getUserKeyACL(newKey.getString("key"));
assertEquals(newKey.getString("key"), getKey.getString("value"));
index.updateUserKey(newKey.getString("key"), Arrays.asList("addObject"));
try { Thread.sleep(2000); } catch (Exception e) {}
getKey = index.getUserKeyACL(newKey.getString("key"));
assertEquals(getKey.getJSONArray("acl").get(0), "addObject");
index.deleteUserKey(getKey.getString("value"));
try { Thread.sleep(2000); } catch (Exception e) {}
JSONObject resAfter = index.listUserKeys();
Expand All @@ -352,6 +356,10 @@ public void test19_user_key() throws AlgoliaException, JSONException {
assertTrue(isPresent(res.getJSONArray("keys"), newKey.getString("key"), "value"));
JSONObject getKey = client.getUserKeyACL(newKey.getString("key"));
assertEquals(newKey.getString("key"), getKey.getString("value"));
client.updateUserKey(newKey.getString("key"), Arrays.asList("addObject"));
try { Thread.sleep(2000); } catch (Exception e) {}
getKey = client.getUserKeyACL(newKey.getString("key"));
assertEquals(getKey.getJSONArray("acl").get(0), "addObject");
client.deleteUserKey(getKey.getString("value"));
try { Thread.sleep(2000); } catch (Exception e) {}
JSONObject resAfter = client.listUserKeys();
Expand Down

0 comments on commit 2fef3ce

Please sign in to comment.