Skip to content

Commit 388b200

Browse files
author
Amir Tocker
committed
Sent params as entities for PUT, POST
1 parent 501559f commit 388b200

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed

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

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

33
import java.io.InputStream;
4+
import java.io.UnsupportedEncodingException;
45
import java.lang.reflect.Constructor;
56
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
import java.util.ArrayList;
69
import java.util.Arrays;
10+
import java.util.List;
711
import java.util.Map;
8-
import java.util.concurrent.TimeUnit;
912

1013
import org.apache.http.HttpHost;
11-
import org.apache.http.client.methods.CloseableHttpResponse;
14+
import org.apache.http.NameValuePair;
15+
import org.apache.http.client.entity.UrlEncodedFormEntity;
16+
import org.apache.http.client.methods.*;
1217
import org.apache.http.client.config.RequestConfig;
13-
import org.apache.http.client.methods.HttpDelete;
14-
import org.apache.http.client.methods.HttpGet;
15-
import org.apache.http.client.methods.HttpPost;
16-
import org.apache.http.client.methods.HttpPut;
17-
import org.apache.http.client.methods.HttpUriRequest;
1818
import org.apache.http.client.utils.URIBuilder;
1919
import org.apache.http.conn.HttpClientConnectionManager;
2020
import org.apache.http.impl.client.CloseableHttpClient;
2121
import org.apache.http.impl.client.HttpClientBuilder;
2222
import org.apache.http.impl.client.HttpClients;
23+
import org.apache.http.message.BasicNameValuePair;
2324
import org.cloudinary.json.JSONException;
2425
import org.cloudinary.json.JSONObject;
2526

2627
import com.cloudinary.Api;
27-
import com.cloudinary.Uploader;
2828
import com.cloudinary.Api.HttpMethod;
2929
import com.cloudinary.Cloudinary;
3030
import com.cloudinary.api.ApiResponse;
@@ -70,6 +70,7 @@ public void init(Api api) {
7070

7171
@SuppressWarnings({"rawtypes", "unchecked"})
7272
public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
73+
URI apiUri;
7374
if (options == null)
7475
options = ObjectUtils.emptyMap();
7576

@@ -87,29 +88,26 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
8788
apiUrl = apiUrl + "/" + component;
8889
}
8990
URIBuilder apiUrlBuilder = new URIBuilder(apiUrl);
90-
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
91-
if (param.getValue() instanceof Iterable) {
92-
for (String single : (Iterable<String>) param.getValue()) {
93-
apiUrlBuilder.addParameter(param.getKey() + "[]", single);
94-
}
95-
} else {
96-
apiUrlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
97-
}
98-
}
99-
100-
URI apiUri = apiUrlBuilder.build();
10191
HttpUriRequest request = null;
92+
10293
switch (method) {
10394
case GET:
95+
apiUri = prepareUrlParams(apiUrlBuilder, params);
10496
request = new HttpGet(apiUri);
10597
break;
10698
case PUT:
107-
request = new HttpPut(apiUri);
99+
apiUri = apiUrlBuilder.build();
100+
HttpEntityEnclosingRequestBase put = new HttpPut(apiUri);
101+
request = perpareEntityParams(put, params);
102+
108103
break;
109104
case POST:
110-
request = new HttpPost(apiUri);
105+
apiUri = apiUrlBuilder.build();
106+
HttpEntityEnclosingRequestBase post = new HttpPost(apiUri);
107+
request = perpareEntityParams(post, params);
111108
break;
112109
case DELETE:
110+
apiUri = prepareUrlParams(apiUrlBuilder, params);
113111
request = new HttpDelete(apiUri);
114112
break;
115113
}
@@ -148,4 +146,35 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
148146
throw exceptionConstructor.newInstance(message);
149147
}
150148
}
149+
150+
private HttpUriRequest perpareEntityParams(HttpEntityEnclosingRequestBase put, Map<String, ? extends Object> params) throws UnsupportedEncodingException {
151+
HttpUriRequest request;List<NameValuePair> entities = new ArrayList<NameValuePair>(params.size());
152+
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
153+
if (param.getValue() instanceof Iterable) {
154+
for (String single : (Iterable<String>) param.getValue()) {
155+
entities.add(new BasicNameValuePair(param.getKey() + "[]", single));
156+
}
157+
} else {
158+
entities.add(new BasicNameValuePair(param.getKey(), ObjectUtils.asString(param.getValue())));
159+
}
160+
}
161+
put.setEntity(new UrlEncodedFormEntity(entities));
162+
request = put;
163+
return request;
164+
}
165+
166+
private URI prepareUrlParams(URIBuilder urlBuilder, Map<String, ? extends Object> params) throws URISyntaxException {
167+
URI apiUri;
168+
for (Map.Entry<String, ? extends Object> param : params.entrySet()) {
169+
if (param.getValue() instanceof Iterable) {
170+
for (String single : (Iterable<String>) param.getValue()) {
171+
urlBuilder.addParameter(param.getKey() + "[]", single);
172+
}
173+
} else {
174+
urlBuilder.addParameter(param.getKey(), ObjectUtils.asString(param.getValue()));
175+
}
176+
}
177+
apiUri = urlBuilder.build();
178+
return apiUri;
179+
}
151180
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
abstract public class AbstractApiTest {
3939

4040
public static final String SRC_TEST_IMAGE = "../cloudinary-test-common/src/main/resources/old_logo.png";
41+
public static final String API_TEST = "api_test";
42+
public static final String API_TEST_1 = "api_test1";
43+
public static final String API_TEST_2 = "api_test2";
44+
public static final String API_TEST_3 = "api_test3";
45+
public static final String API_TEST_5 = "api_test5";
4146
private Cloudinary cloudinary;
4247
protected Api api;
4348
private static String uniqueTag = String.format("api_test_tag_%d", new java.util.Date().getTime());
@@ -51,7 +56,7 @@ public static void setUpClass() throws IOException {
5156
}
5257
Api api = cloudinary.api();
5358
try {
54-
api.deleteResources(Arrays.asList("api_test", "api_test1", "api_test2", "api_test3", "api_test5"), ObjectUtils.emptyMap());
59+
api.deleteResources(Arrays.asList(API_TEST, API_TEST_1, API_TEST_2, API_TEST_3, API_TEST_5), ObjectUtils.emptyMap());
5560
} catch (Exception e) {
5661
}
5762
try {
@@ -82,10 +87,10 @@ public static void setUpClass() throws IOException {
8287
api.deleteUploadPreset("api_test_upload_preset4", ObjectUtils.emptyMap());
8388
} catch (Exception e) {
8489
}
85-
Map options = ObjectUtils.asMap("public_id", "api_test", "tags", new String[]{"api_test_tag", uniqueTag}, "context", "key=value", "eager",
90+
Map options = ObjectUtils.asMap("public_id", API_TEST, "tags", new String[]{"api_test_tag", uniqueTag}, "context", "key=value", "eager",
8691
Collections.singletonList(new Transformation().width(100).crop("scale")));
8792
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
88-
options.put("public_id", "api_test1");
93+
options.put("public_id", API_TEST_1);
8994
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
9095
}
9196

@@ -122,7 +127,7 @@ public void test01ResourceTypes() throws Exception {
122127
public void test02Resources() throws Exception {
123128
// should allow listing resources
124129
Map result = api.resources(ObjectUtils.emptyMap());
125-
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", "api_test");
130+
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", API_TEST);
126131
assertNotNull(resource);
127132
assertEquals(resource.get("type"), "upload");
128133
}
@@ -133,7 +138,7 @@ public void testTimeoutParameter() throws Exception {
133138
Map<String, Object> options = new HashMap<String, Object>();
134139
options.put("timeout", Integer.valueOf(5000));
135140
Map result = api.resources(options);
136-
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", "api_test");
141+
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", API_TEST);
137142
assertNotNull(resource);
138143
assertEquals(resource.get("type"), "upload");
139144
}
@@ -161,17 +166,17 @@ public void test03ResourcesCursor() throws Exception {
161166
public void test04ResourcesByType() throws Exception {
162167
// should allow listing resources by type
163168
Map result = api.resources(ObjectUtils.asMap("type", "upload"));
164-
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", "api_test");
169+
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", API_TEST);
165170
assertNotNull(resource);
166171
}
167172

168173
@Test
169174
public void test05ResourcesByPrefix() throws Exception {
170175
// should allow listing resources by prefix
171-
Map result = api.resources(ObjectUtils.asMap("type", "upload", "prefix", "api_test", "tags", true, "context", true));
176+
Map result = api.resources(ObjectUtils.asMap("type", "upload", "prefix", API_TEST, "tags", true, "context", true));
172177
List<Map> resources = (List<Map>) result.get("resources");
173-
assertNotNull(findByAttr(resources, "public_id", "api_test"));
174-
assertNotNull(findByAttr(resources, "public_id", "api_test1"));
178+
assertNotNull(findByAttr(resources, "public_id", API_TEST));
179+
assertNotNull(findByAttr(resources, "public_id", API_TEST_1));
175180
assertNotNull(findByAttr((List<Map>) result.get("resources"), "context", ObjectUtils.asMap("custom", ObjectUtils.asMap("key", "value"))));
176181
boolean found = false;
177182
for (Map r : resources) {
@@ -208,11 +213,11 @@ public void testResourcesListingStartAt() throws Exception {
208213
@Test
209214
public void testResourcesByPublicIds() throws Exception {
210215
// should allow listing resources by public ids
211-
Map result = api.resourcesByIds(Arrays.asList("api_test", "api_test1", "bogus"), ObjectUtils.asMap("type", "upload", "tags", true, "context", true));
216+
Map result = api.resourcesByIds(Arrays.asList(API_TEST, API_TEST_1, "bogus"), ObjectUtils.asMap("type", "upload", "tags", true, "context", true));
212217
List<Map> resources = (List<Map>) result.get("resources");
213218
assertEquals(2, resources.size());
214-
assertNotNull(findByAttr(resources, "public_id", "api_test"));
215-
assertNotNull(findByAttr(resources, "public_id", "api_test1"));
219+
assertNotNull(findByAttr(resources, "public_id", API_TEST));
220+
assertNotNull(findByAttr(resources, "public_id", API_TEST_1));
216221
assertNotNull(findByAttr((List<Map>) result.get("resources"), "context", ObjectUtils.asMap("custom", ObjectUtils.asMap("key", "value"))));
217222
boolean found = false;
218223
for (Map r : resources) {
@@ -226,7 +231,7 @@ public void testResourcesByPublicIds() throws Exception {
226231
public void test06ResourcesTag() throws Exception {
227232
// should allow listing resources by tag
228233
Map result = api.resourcesByTag("api_test_tag", ObjectUtils.asMap("tags", true, "context", true));
229-
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", "api_test");
234+
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", API_TEST);
230235
assertNotNull(resource);
231236
resource = findByAttr((List<Map>) result.get("resources"), "context", ObjectUtils.asMap("custom", ObjectUtils.asMap("key", "value")));
232237
assertNotNull(resource);
@@ -242,9 +247,9 @@ public void test06ResourcesTag() throws Exception {
242247
@Test
243248
public void test07ResourceMetadata() throws Exception {
244249
// should allow get resource metadata
245-
Map resource = api.resource("api_test", ObjectUtils.emptyMap());
250+
Map resource = api.resource(API_TEST, ObjectUtils.emptyMap());
246251
assertNotNull(resource);
247-
assertEquals(resource.get("public_id"), "api_test");
252+
assertEquals(resource.get("public_id"), API_TEST);
248253
assertEquals(resource.get("bytes"), 3381);
249254
assertEquals(((List) resource.get("derived")).size(), 1);
250255
}
@@ -253,14 +258,14 @@ public void test07ResourceMetadata() throws Exception {
253258
public void test08DeleteDerived() throws Exception {
254259
// should allow deleting derived resource
255260
cloudinary.uploader().upload(SRC_TEST_IMAGE,
256-
ObjectUtils.asMap("public_id", "api_test3", "eager", Collections.singletonList(new Transformation().width(101).crop("scale"))));
257-
Map resource = api.resource("api_test3", ObjectUtils.emptyMap());
261+
ObjectUtils.asMap("public_id", API_TEST_3, "eager", Collections.singletonList(new Transformation().width(101).crop("scale"))));
262+
Map resource = api.resource(API_TEST_3, ObjectUtils.emptyMap());
258263
assertNotNull(resource);
259264
List<Map> derived = (List<Map>) resource.get("derived");
260265
assertEquals(derived.size(), 1);
261266
String derived_resource_id = (String) derived.get(0).get("id");
262267
api.deleteDerivedResources(Arrays.asList(derived_resource_id), ObjectUtils.emptyMap());
263-
resource = api.resource("api_test3", ObjectUtils.emptyMap());
268+
resource = api.resource(API_TEST_3, ObjectUtils.emptyMap());
264269
assertNotNull(resource);
265270
derived = (List<Map>) resource.get("derived");
266271
assertEquals(derived.size(), 0);
@@ -269,11 +274,12 @@ public void test08DeleteDerived() throws Exception {
269274
@Test(expected = NotFound.class)
270275
public void test09DeleteResources() throws Exception {
271276
// should allow deleting resources
272-
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "api_test3"));
273-
Map resource = api.resource("api_test3", ObjectUtils.emptyMap());
277+
String public_id = "api_,test3";
278+
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", public_id));
279+
Map resource = api.resource(public_id, ObjectUtils.emptyMap());
274280
assertNotNull(resource);
275-
api.deleteResources(Arrays.asList("apit_test", "api_test2", "api_test3"), ObjectUtils.emptyMap());
276-
api.resource("api_test3", ObjectUtils.emptyMap());
281+
api.deleteResources(Arrays.asList(API_TEST, API_TEST_2, public_id), ObjectUtils.emptyMap());
282+
api.resource(public_id, ObjectUtils.emptyMap());
277283
}
278284

279285
@Test(expected = NotFound.class)
@@ -308,7 +314,7 @@ public void test10Tags() throws Exception {
308314
@Test
309315
public void test11TagsPrefix() throws Exception {
310316
// should allow listing tag by prefix
311-
Map result = api.tags(ObjectUtils.asMap("prefix", "api_test"));
317+
Map result = api.tags(ObjectUtils.asMap("prefix", API_TEST));
312318
List<String> tags = (List<String>) result.get("tags");
313319
assertContains("api_test_tag", tags);
314320
result = api.tags(ObjectUtils.asMap("prefix", "api_test_no_such_tag"));
@@ -420,11 +426,11 @@ public void test19Ping() throws Exception {
420426
public void testDeleteAllResources() throws Exception {
421427
// should allow deleting all resources
422428
cloudinary.uploader().upload(SRC_TEST_IMAGE,
423-
ObjectUtils.asMap("public_id", "api_test5", "eager", Collections.singletonList(new Transformation().crop("scale").width(2.0))));
424-
Map result = api.resource("api_test5", ObjectUtils.emptyMap());
429+
ObjectUtils.asMap("public_id", API_TEST_5, "eager", Collections.singletonList(new Transformation().crop("scale").width(2.0))));
430+
Map result = api.resource(API_TEST_5, ObjectUtils.emptyMap());
425431
assertEquals(1, ((org.cloudinary.json.JSONArray) result.get("derived")).length());
426432
api.deleteAllResources(ObjectUtils.asMap("keep_original", true));
427-
result = api.resource("api_test5", ObjectUtils.emptyMap());
433+
result = api.resource(API_TEST_5, ObjectUtils.emptyMap());
428434
// assertEquals(0, ((org.cloudinary.json.JSONArray)
429435
// result.get("derived")).size());
430436
}

0 commit comments

Comments
 (0)