-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.java
73 lines (54 loc) · 2.48 KB
/
demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.cloudinary;
import java.util.*;
import com.cloudinary.api.ApiResponse;
import com.cloudinary.api.AuthorizationRequired;
import com.cloudinary.api.exceptions.*;
import com.cloudinary.metadata.MetadataField;
import com.cloudinary.metadata.MetadataDataSource;
import com.cloudinary.strategies.AbstractApiStrategy;
import com.cloudinary.utils.ObjectUtils;
import com.cloudinary.utils.StringUtils;
import org.cloudinary.json.JSONArray;
@SuppressWarnings({"rawtypes", "unchecked"})
public class Api {
public AbstractApiStrategy getStrategy() {
return strategy;
}
public enum HttpMethod {GET, POST, PUT, DELETE;}
public final static Map<Integer, Class<? extends Exception>> CLOUDINARY_API_ERROR_CLASSES = new HashMap<Integer, Class<? extends Exception>>();
static {
CLOUDINARY_API_ERROR_CLASSES.put(400, BadRequest.class);
CLOUDINARY_API_ERROR_CLASSES.put(401, AuthorizationRequired.class);
CLOUDINARY_API_ERROR_CLASSES.put(403, NotAllowed.class);
CLOUDINARY_API_ERROR_CLASSES.put(404, NotFound.class);
CLOUDINARY_API_ERROR_CLASSES.put(409, AlreadyExists.class);
CLOUDINARY_API_ERROR_CLASSES.put(420, RateLimited.class);
CLOUDINARY_API_ERROR_CLASSES.put(500, GeneralError.class);
}
public final Cloudinary cloudinary;
private AbstractApiStrategy strategy;
protected ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
return this.strategy.callApi(method, uri, params, options);
}
public Api(Cloudinary cloudinary, AbstractApiStrategy strategy) {
this.cloudinary = cloudinary;
this.strategy = strategy;
this.strategy.init(this);
}
public ApiResponse ping(Map options) throws Exception {
if (options == null) options = ObjectUtils.emptyMap();
return callApi(HttpMethod.GET, Arrays.asList("ping"), ObjectUtils.emptyMap(), options);
}
public ApiResponse usage(Map options) throws Exception {
if (options == null) options = ObjectUtils.emptyMap();
final List<String> uri = new ArrayList<String>();
uri.add("usage");
Object date = options.get("date");
if (date != null) {
if (date instanceof Date) {
date = ObjectUtils.toUsageApiDateFormat((Date) date);
}
uri.add(date.toString());
}
return callApi(HttpMethod.GET, uri, ObjectUtils.emptyMap(), options);
}