Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@

import com.cloudinary.Api;
import com.cloudinary.Api.HttpMethod;
import com.cloudinary.SmartUrlEncoder;
import com.cloudinary.api.ApiResponse;

import com.cloudinary.utils.StringUtils;
import java.util.Arrays;
import java.util.Map;


public abstract class AbstractApiStrategy {
protected Api api;

public void init(Api api) {
this.api = api;
}

protected String createApiUrl (Iterable<String> uri, String prefix, String cloudName){

String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
for (String component : uri) {
component = SmartUrlEncoder.encode(component);
apiUrl = apiUrl + "/" + component;

}
return apiUrl;
}

@SuppressWarnings("rawtypes")
public abstract ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");
String contentType = ObjectUtils.asString(options.get("content_type"), "urlencoded");
int timeout = ObjectUtils.asInteger(options.get("timeout"), this.api.cloudinary.config.timeout);
String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");

for (String component : uri) {
apiUrl = apiUrl + "/" + component;
}
String apiUrl = createApiUrl(uri, prefix, cloudName);

return getApiResponse(method, params, apiKey, apiSecret, contentType, timeout, apiUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");


String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
for (String component : uri) {
apiUrl = apiUrl + "/" + component;
}
String apiUrl = createApiUrl(uri, prefix, cloudName);
HttpUriRequest request = prepareRequest(method, apiUrl, params, options);

request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String,
if (apiSecret == null) throw new IllegalArgumentException("Must supply api_secret");


String apiUrl = StringUtils.join(Arrays.asList(prefix, "v1_1", cloudName), "/");
for (String component : uri) {
apiUrl = apiUrl + "/" + component;
}
String apiUrl = createApiUrl(uri, prefix, cloudName);
HttpUriRequest request = prepareRequest(method, apiUrl, params, options);

request.setHeader("Authorization", "Basic " + Base64Coder.encodeString(apiKey + ":" + apiSecret));
Expand Down Expand Up @@ -204,4 +201,4 @@ private HttpUriRequest prepareRequest(HttpMethod method, String apiUrl, Map<Stri
setTimeouts(request, options);
return request;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,15 @@ public void testRestore() throws Exception {
}

@Test
public void testEncodeUrlInApiCall() throws Exception {
try {
Map result = api.subFolders("sub^folder test", null);
assertEquals("sub%5Efolder%20test", ((Map) ((org.cloudinary.json.JSONArray) result.get("folders")).get(0)).get("path"));
} catch (Exception ignored) {

}
}
@Test
public void testUploadMapping() throws Exception {
String aptTestUploadMapping = "api_test_upload_mapping" + SUFFIX;
try {
Expand Down Expand Up @@ -958,4 +967,4 @@ public void testCinemagraphAnalysisResource() throws Exception {
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("cinemagraph_analysis", true));
assertNotNull(res.get("cinemagraph_analysis"));
}
}
}