Skip to content

Commit c8135b5

Browse files
daniel cohenAmir Tocker
authored andcommitted
Add Search API
1 parent 6bbae58 commit c8135b5

File tree

13 files changed

+271
-47
lines changed

13 files changed

+271
-47
lines changed

cloudinary-core/src/main/java/com/cloudinary/Api.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.cloudinary.utils.ObjectUtils;
1010
import com.cloudinary.utils.StringUtils;
1111
import org.cloudinary.json.JSONArray;
12-
import com.cloudinary.utils.StringUtils;
1312

1413
@SuppressWarnings({"rawtypes", "unchecked"})
1514
public class Api {

cloudinary-core/src/main/java/com/cloudinary/Cloudinary.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public Api api() {
5656
return new Api(this, apiStrategy);
5757
}
5858

59+
public Search search() {
60+
return new Search(this);
61+
}
62+
5963
public static void registerUploaderStrategy(String className) {
6064
if (!UPLOAD_STRATEGIES.contains(className)) {
6165
UPLOAD_STRATEGIES.add(className);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.cloudinary;
2+
3+
import com.cloudinary.api.ApiResponse;
4+
import com.cloudinary.utils.ObjectUtils;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class Search {
12+
13+
private Cloudinary cloudinary;
14+
private ArrayList<HashMap<String, Object>> sortByParam;
15+
private ArrayList<String> aggregateParam;
16+
private ArrayList<String> withFieldParam;
17+
private HashMap<String, Object> params;
18+
19+
Search(Cloudinary cloudinary) {
20+
this.cloudinary = cloudinary;
21+
this.params = new HashMap<String, Object>();
22+
this.sortByParam = new ArrayList<HashMap<String, Object>>();
23+
this.aggregateParam = new ArrayList<String>();
24+
this.withFieldParam = new ArrayList<String>();
25+
}
26+
27+
public Search expression(String value) {
28+
this.params.put("expression", value);
29+
return this;
30+
}
31+
32+
public Search maxResults(Integer value) {
33+
this.params.put("max_results", value);
34+
return this;
35+
}
36+
37+
public Search nextCursor(String value) {
38+
this.params.put("next_cursor", value);
39+
return this;
40+
}
41+
42+
public Search aggregate(String field) {
43+
aggregateParam.add(field);
44+
return this;
45+
}
46+
47+
public Search withField(String field) {
48+
withFieldParam.add(field);
49+
return this;
50+
}
51+
52+
public Search sortBy(String field, String dir) {
53+
HashMap<String, Object> sortBucket = new HashMap<String, Object>();
54+
sortBucket.put(field, dir);
55+
sortByParam.add(sortBucket);
56+
return this;
57+
}
58+
59+
public HashMap<String, Object> toQuery() {
60+
HashMap<String, Object> queryParams = new HashMap<String, Object>(this.params);
61+
queryParams.put("with_field", withFieldParam);
62+
queryParams.put("sort_by", sortByParam);
63+
queryParams.put("aggregate", aggregateParam);
64+
return queryParams;
65+
}
66+
67+
public ApiResponse execute() throws Exception {
68+
Map<String, String> options = ObjectUtils.asMap("content_type", "json");
69+
return this.cloudinary.api().callApi(Api.HttpMethod.POST, Arrays.asList("resources", "search"), this.toQuery(), options);
70+
}
71+
}

cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package com.cloudinary;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.File;
5-
import java.io.FileInputStream;
6-
import java.io.IOException;
7-
import java.io.InputStream;
3+
import com.cloudinary.strategies.AbstractUploaderStrategy;
4+
import com.cloudinary.utils.ObjectUtils;
5+
import com.cloudinary.utils.StringUtils;
6+
import org.cloudinary.json.JSONObject;
7+
8+
import java.io.*;
89
import java.util.Arrays;
910
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Map;
1213

13-
import org.cloudinary.json.JSONObject;
14-
15-
import com.cloudinary.strategies.AbstractUploaderStrategy;
16-
import com.cloudinary.utils.ObjectUtils;
17-
import com.cloudinary.utils.StringUtils;
18-
1914
@SuppressWarnings({"rawtypes", "unchecked"})
2015
public class Uploader {
2116

cloudinary-core/src/main/java/com/cloudinary/utils/ObjectUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public static Map<String, Object> toMap(JSONObject object) throws JSONException
131131
return map;
132132
}
133133

134+
public static JSONObject toJSON(Map<String, ? extends Object> map) throws JSONException {
135+
JSONObject json = new JSONObject();
136+
for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
137+
String field = entry.getKey();
138+
Object value = entry.getValue();
139+
json.put(field, value);
140+
}
141+
return json;
142+
}
143+
134144
private static Object fromJson(Object json) throws JSONException {
135145
if (json == JSONObject.NULL) {
136146
return null;

cloudinary-http42/src/main/java/com/cloudinary/http42/ApiStrategy.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
import com.cloudinary.strategies.AbstractApiStrategy;
1010
import org.apache.http.HttpResponse;
11-
import org.apache.http.client.methods.HttpDelete;
12-
import org.apache.http.client.methods.HttpGet;
13-
import org.apache.http.client.methods.HttpPost;
14-
import org.apache.http.client.methods.HttpPut;
15-
import org.apache.http.client.methods.HttpUriRequest;
11+
import org.apache.http.client.methods.*;
1612
import org.apache.http.client.utils.URIBuilder;
1713
import org.apache.http.conn.ClientConnectionManager;
14+
import org.apache.http.entity.ContentType;
15+
import org.apache.http.entity.StringEntity;
1816
import org.apache.http.impl.client.DefaultHttpClient;
1917
import org.apache.http.params.HttpConnectionParams;
2018
import org.apache.http.params.HttpParams;
@@ -44,23 +42,27 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
4442
if (apiKey == null) throw new IllegalArgumentException("Must supply api_key");
4543
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.api.cloudinary.config.apiSecret);
4644
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
47-
45+
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
4846
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
49-
5047
String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
48+
5149
for (String component : uri) {
5250
apiUrl = apiUrl + "/" + component;
5351
}
52+
5453
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
55-
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
56-
if (param.getValue() instanceof Iterable) {
57-
for (String single : (Iterable<String>) param.getValue()) {
58-
apiUrlBuilder.addParameter(param.getKey() + "[]", single);
54+
if (!contentType.equals("json")) {
55+
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
56+
if (param.getValue() instanceof Iterable) {
57+
for (String single : (Iterable<String>) param.getValue()) {
58+
apiUrlBuilder.addParameter(param.getKey() + "[]", single);
59+
}
60+
} else {
61+
apiUrlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
5962
}
60-
} else {
61-
apiUrlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
6263
}
6364
}
65+
6466
ClientConnectionManager connectionManager = (ClientConnectionManager) this.api.cloudinary.config.properties.get("connectionManager");
6567

6668
DefaultHttpClient client = new DefaultHttpClient(connectionManager);
@@ -88,6 +90,11 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
8890
}
8991
request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
9092
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
93+
if (contentType.equals("json")) {
94+
JSONObject asJSON = ObjectUtils.toJSON(params);
95+
StringEntity requestEntity = new StringEntity(asJSON.toString(), ContentType.APPLICATION_JSON);
96+
((HttpEntityEnclosingRequestBase) request).setEntity(requestEntity);
97+
}
9198

9299
HttpResponse response = client.execute(request);
93100

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cloudinary.test;
2+
3+
public class SearchTest extends AbstractSearchTest {
4+
}

cloudinary-http43/src/main/java/com/cloudinary/http43/ApiStrategy.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.apache.http.client.methods.*;
1818
import org.apache.http.client.utils.URIBuilder;
1919
import org.apache.http.conn.HttpClientConnectionManager;
20+
import org.apache.http.entity.ContentType;
21+
import org.apache.http.entity.StringEntity;
2022
import org.apache.http.impl.client.CloseableHttpClient;
2123
import org.apache.http.impl.client.HttpClientBuilder;
2224
import org.apache.http.impl.client.HttpClients;
@@ -29,9 +31,9 @@
2931
import java.lang.reflect.Constructor;
3032
import java.net.URI;
3133
import java.net.URISyntaxException;
32-
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.List;
36+
import java.util.HashMap;
3537
import java.util.Map;
3638

3739
import static com.cloudinary.http43.ApiUtils.prepareParams;
@@ -128,6 +130,7 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
128130

129131
/**
130132
* Prepare a request with the URL and parameters based on the HTTP method used
133+
*
131134
* @param method the HTTP method: GET, PUT, POST, DELETE
132135
* @param apiUrl the cloudinary API URI
133136
* @param params the parameters to pass to the server
@@ -137,12 +140,14 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
137140
*/
138141
private HttpUriRequest prepareRequest(HttpMethod method, String apiUrl, Map<String, ?> params, Map options) throws URISyntaxException, UnsupportedEncodingException {
139142
URI apiUri;
140-
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
141-
List<NameValuePair> parameters;
142143
HttpRequestBase request;
143-
parameters = prepareParams(params);
144-
if(method == HttpMethod.GET) {
145-
apiUrlBuilder.setParameters(parameters);
144+
145+
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
146+
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
147+
HashMap<String,Object> unboxedParams = new HashMap<String,Object>(params);
148+
149+
if (method == HttpMethod.GET) {
150+
apiUrlBuilder.setParameters(prepareParams(params));
146151
apiUri = apiUrlBuilder.build();
147152
request = new HttpGet(apiUri);
148153
} else {
@@ -152,15 +157,21 @@ private HttpUriRequest prepareRequest(HttpMethod method, String apiUrl, Map<Stri
152157
request = new HttpPut(apiUri);
153158
break;
154159
case DELETE: //uses HttpPost instead of HttpDelete
155-
parameters.add(new BasicNameValuePair("_method", "delete"));
160+
unboxedParams.put("_method","delete");
156161
//continue with POST
157162
case POST:
158163
request = new HttpPost(apiUri);
159164
break;
160165
default:
161166
throw new IllegalArgumentException("Unknown HTTP method");
162167
}
163-
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
168+
if (contentType.equals("json")) {
169+
JSONObject asJSON = ObjectUtils.toJSON(unboxedParams);
170+
StringEntity requestEntity = new StringEntity(asJSON.toString(), ContentType.APPLICATION_JSON);
171+
((HttpEntityEnclosingRequestBase) request).setEntity(requestEntity);
172+
} else {
173+
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(prepareParams(unboxedParams), Consts.UTF_8));
174+
}
164175
}
165176

166177
setTimeouts(request, options);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cloudinary.test;
2+
3+
public class SearchTest extends AbstractSearchTest {
4+
}

cloudinary-http44/src/main/java/com/cloudinary/http44/ApiStrategy.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
import org.apache.http.client.methods.*;
1818
import org.apache.http.client.utils.URIBuilder;
1919
import org.apache.http.conn.HttpClientConnectionManager;
20+
import org.apache.http.entity.ContentType;
21+
import org.apache.http.entity.StringEntity;
2022
import org.apache.http.impl.client.CloseableHttpClient;
2123
import org.apache.http.impl.client.HttpClientBuilder;
2224
import org.apache.http.impl.client.HttpClients;
23-
import org.apache.http.message.BasicNameValuePair;
2425
import org.cloudinary.json.JSONException;
2526
import org.cloudinary.json.JSONObject;
2627

@@ -30,9 +31,13 @@
3031
import java.net.URI;
3132
import java.net.URISyntaxException;
3233
import java.util.Arrays;
34+
import java.util.HashMap;
3335
import java.util.List;
3436
import java.util.Map;
3537

38+
import static com.cloudinary.http44.ApiUtils.prepareParams;
39+
import static com.cloudinary.http44.ApiUtils.setTimeouts;
40+
3641
public class ApiStrategy extends com.cloudinary.strategies.AbstractApiStrategy {
3742

3843
private CloseableHttpClient client = null;
@@ -124,6 +129,7 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
124129

125130
/**
126131
* Prepare a request with the URL and parameters based on the HTTP method used
132+
*
127133
* @param method the HTTP method: GET, PUT, POST, DELETE
128134
* @param apiUrl the cloudinary API URI
129135
* @param params the parameters to pass to the server
@@ -133,34 +139,42 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
133139
*/
134140
private HttpUriRequest prepareRequest(HttpMethod method, String apiUrl, Map<String, ?> params, Map options) throws URISyntaxException, UnsupportedEncodingException {
135141
URI apiUri;
136-
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
137-
List<NameValuePair> parameters;
138142
HttpRequestBase request;
139-
parameters = ApiUtils.prepareParams(params);
140-
if(method == HttpMethod.GET) {
141-
apiUrlBuilder.setParameters(parameters);
143+
144+
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
145+
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
146+
List<NameValuePair> urlEncodedParams = prepareParams(params);
147+
148+
if (method == HttpMethod.GET) {
149+
apiUrlBuilder.setParameters(prepareParams(params));
142150
apiUri = apiUrlBuilder.build();
143151
request = new HttpGet(apiUri);
144152
} else {
153+
Map<String,Object> paramsCopy = new HashMap<String, Object>((Map<String,Object>) params);
145154
apiUri = apiUrlBuilder.build();
146155
switch (method) {
147156
case PUT:
148157
request = new HttpPut(apiUri);
149158
break;
150159
case DELETE: //uses HttpPost instead of HttpDelete
151-
parameters.add(new BasicNameValuePair("_method", "delete"));
160+
paramsCopy.put("_method", "delete");
152161
//continue with POST
153162
case POST:
154163
request = new HttpPost(apiUri);
155164
break;
156165
default:
157166
throw new IllegalArgumentException("Unknown HTTP method");
158167
}
159-
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
168+
if (contentType.equals("json")) {
169+
JSONObject asJSON = ObjectUtils.toJSON(paramsCopy);
170+
StringEntity requestEntity = new StringEntity(asJSON.toString(), ContentType.APPLICATION_JSON);
171+
((HttpEntityEnclosingRequestBase) request).setEntity(requestEntity);
172+
} else {
173+
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(prepareParams(paramsCopy), Consts.UTF_8));
174+
}
160175
}
161176

162-
ApiUtils.setTimeouts(request, options);
177+
setTimeouts(request, options);
163178
return request;
164179
}
165-
166-
}
180+
}

0 commit comments

Comments
 (0)