Skip to content

Commit 599c1d0

Browse files
committed
Cleaning and improved FAToken
1 parent 9b90b25 commit 599c1d0

File tree

8 files changed

+148
-80
lines changed

8 files changed

+148
-80
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<project name="CloudKey" default="jar" basedir=".">
3-
<property name="version" value="1.0" />
3+
<property name="version" value="1.1" />
44
<property name="src" location="src" />
55
<property name="build" location="build" />
66
<property name="thirdparty" value="third-party" />

cloudkey-1.0.jar

36 Bytes
Binary file not shown.

examples/Example.class

121 Bytes
Binary file not shown.

examples/Example.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,32 @@ public static void testToken() {
7979
}
8080
}
8181

82+
public static void getApiKey() {
83+
String ck_user_id = "4c1a4d3edede832bfd000000";
84+
String ck_api_key = "59f7d6fca9080ec7b59b83eab00e104e3d3d2789";
85+
CloudKey cloudkey = new CloudKey(ck_user_id, ck_api_key, base_url, CloudKey.CDN_URL, "");
86+
87+
DCObject ck_obj = DCObject.create()
88+
.push("id", user_id)
89+
.push("fields", DCArray.create()
90+
.push("id")
91+
.push("api_key")
92+
.push("streaming_active")
93+
.push("is_active")
94+
.push("permissions")
95+
);
96+
try {
97+
DCObject result = cloudkey.call("user.info", ck_obj);
98+
System.out.println(result.get("api_key"));
99+
} catch (DCException e) {
100+
System.out.println(e);
101+
}
102+
}
103+
82104
public static void main(String[] args) {
83105
//mediaList();
84-
testToken();
106+
//testToken();
107+
getApiKey();
85108
}
86109

87110
}

examples/compile.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
javac -cp ../cloudkey-1.0.jar Example.java
2-
java -cp ../cloudkey-1.0.jar:.:../third-party/commons-httpclient-3.1.jar:../third-party/commons-codec-1.6.jar:../third-party/jackson-core-asl-1.8.1.jar:../third-party/jackson-mapper-asl-1.8.1.jar Example
1+
javac -cp ../cloudkey-1.1.jar Example.java
2+
java -cp ../cloudkey-1.1.jar:.:../third-party/commons-httpclient-3.1.jar:../third-party/commons-codec-1.6.jar:../third-party/jackson-core-asl-1.8.1.jar:../third-party/jackson-mapper-asl-1.8.1.jar Example

src/net/dmcloud/cloudkey/Api.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.dmcloud.cloudkey;
22

3+
import java.io.IOException;
34
import java.util.Map;
45
import net.dmcloud.util.*;
56
import org.codehaus.jackson.map.ObjectMapper;
@@ -14,7 +15,7 @@ public class Api
1415
public static int SECLEVEL_USEONCE = 1 << 4;
1516
public static int SECLEVEL_COUNTRY = 1 << 5;
1617
public static int SECLEVEL_REFERER = 1 << 6;
17-
18+
1819
public static String API_URL = "http://api.dmcloud.net";
1920
public static String CDN_URL = "http://cdn.dmcloud.net";
2021
public static String STATIC_URL = "http://static.dmcloud.net";
@@ -25,37 +26,43 @@ public class Api
2526
protected String cdn_url = null;
2627
protected String proxy = null;
2728

28-
public Api(String _user_id, String _api_key) throws Exception
29+
public Api(String _user_id, String _api_key)
2930
{
3031
this(_user_id, _api_key, CloudKey.API_URL, CloudKey.CDN_URL, "");
3132
}
3233

33-
public Api(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
34+
public Api(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy)
3435
{
35-
if (_user_id == null || _api_key == null)
36-
{
37-
throw new DCException("You must provide valid user_id and api_key parameters");
38-
}
3936
this.user_id = _user_id;
4037
this.api_key = _api_key;
4138
this.base_url = _base_url;
4239
this.cdn_url = _cdn_url;
4340
this.proxy = _proxy;
4441
}
4542

46-
public DCObject call(String call, DCObject args) throws Exception
43+
public DCObject call(String call, DCObject args) throws DCException
4744
{
4845
ObjectMapper mapper = new ObjectMapper();
4946

5047
DCObject jo = DCObject.create()
5148
.push("call", call)
5249
.push("args", args);
53-
50+
5451
jo.push("auth", this.user_id + ":" + Helpers.md5(this.user_id + Helpers.normalize(jo) + this.api_key));
55-
56-
String json_encoded = mapper.writeValueAsString(jo);
52+
53+
String json_encoded = null;
54+
DCObject json_response = null;
55+
try {
56+
json_encoded = mapper.writeValueAsString(jo);
57+
} catch(IOException e) {
58+
throw new DCException("JSON serialization error: " + e.getMessage());
59+
}
5760
String response = Helpers.curl(this.base_url + "/api", json_encoded);
58-
DCObject json_response = DCObject.create(mapper.readValue(response, Map.class));
61+
try {
62+
json_response = DCObject.create(mapper.readValue(response, Map.class));
63+
} catch(IOException e) {
64+
throw new DCException("JSON deserialization error: " + e.getMessage());
65+
}
5966
if (json_response.containsKey("error"))
6067
{
6168
String message = "";
@@ -67,17 +74,17 @@ public DCObject call(String call, DCObject args) throws Exception
6774
case 400 : message = "AuthenticationErrorException"; break;
6875
case 410 : message = "RateLimitExceededException"; break;
6976
case 500 : message = "SerializerException"; break;
70-
77+
7178
case 600 : message = "InvalidRequestException"; break;
7279
case 610 : message = "InvalidObjectException"; break;
7380
case 620 : message = "InvalidMethodException"; break;
7481
case 630 : message = "InvalidParamException"; break;
75-
82+
7683
case 1000 : message = "ApplicationException"; break;
7784
case 1010 : message = "NotFoundException"; break;
7885
case 1020 : message = "ExistsException"; break;
7986
case 1030 : message = "LimitExceededException"; break;
80-
87+
8188
default : message = "RPCException (error:" + json_response.pull("error.code").toString() + ")"; break;
8289
}
8390
message += " : " + json_response.pull("error.message").toString();

src/net/dmcloud/cloudkey/CloudKey.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515

1616
public class CloudKey extends Api
1717
{
18-
public CloudKey(String _user_id, String _api_key) throws Exception
18+
public CloudKey(String _user_id, String _api_key)
1919
{
2020
super(_user_id, _api_key, CloudKey.API_URL, CloudKey.CDN_URL, "");
2121
}
2222

23-
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
23+
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy)
2424
{
2525
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
2626
}
27-
27+
2828
public String mediaGetEmbedUrl(String id) throws DCException
2929
{
3030
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0);
3131
}
32-
33-
public String mediaGetEmbedUrl(String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
32+
33+
public String mediaGetEmbedUrl(String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
3434
{
3535
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, seclevel, asnum, ip, useragent, countries, referers, expires);
3636
}
3737

38-
public String mediaGetEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
38+
public String mediaGetEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
3939
{
4040
String _url = url + "/embed/" + this.user_id + "/" + id;
4141
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
@@ -55,7 +55,7 @@ public String mediaGetStreamUrl(String id, String asset_name, int seclevel, Stri
5555
{
5656
return this.mediaGetStreamUrl(CloudKey.API_URL, id, asset_name, seclevel, asnum, ip, useragent, countries, referers, expires, extension, download);
5757
}
58-
58+
5959
public String mediaGetStreamUrl(String url, String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws DCException
6060
{
6161
if (extension == "")
@@ -73,17 +73,17 @@ public String mediaGetStreamUrl(String url, String id, String asset_name, int se
7373
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
7474
}
7575
}
76-
76+
7777
public String mediaCreate() throws Exception
7878
{
7979
return mediaCreate("");
8080
}
81-
81+
8282
public String mediaCreate(String url) throws Exception
8383
{
8484
return mediaCreate(url, null, null);
8585
}
86-
86+
8787
public String mediaCreate(String url, DCArray assets_names, DCObject meta) throws Exception
8888
{
8989
DCObject args = DCObject.create().push("url", url);
@@ -98,30 +98,30 @@ public String mediaCreate(String url, DCArray assets_names, DCObject meta) throw
9898
DCObject result = this.call("media.create", args);
9999
return result.pull("id");
100100
}
101-
102-
101+
102+
103103
public String mediaCreate(File f) throws Exception
104104
{
105105
return this.mediaCreate(f, null, null);
106106
}
107-
107+
108108
public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Exception
109109
{
110110
String upload_url = this.fileUpload();
111-
111+
112112
PostMethod filePost = null;
113113
try
114114
{
115115
filePost = new PostMethod(upload_url);
116-
116+
117117
Part[] parts = {
118118
new FilePart("file", f)
119119
};
120-
120+
121121
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
122122
HttpClient client = new HttpClient();
123123
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
124-
124+
125125
int status = client.executeMethod(filePost);
126126
if (status == HttpStatus.SC_OK)
127127
{
@@ -146,18 +146,18 @@ public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Ex
146146
}
147147
}
148148
}
149-
149+
150150
public void mediaDelete(String id) throws Exception
151151
{
152152
this.call("media.delete", DCObject.create().push("id", id));
153153
}
154-
154+
155155
public String fileUpload() throws Exception
156156
{
157157
DCObject result = fileUpload(false, "", "");
158158
return result.pull("url");
159159
}
160-
160+
161161
public DCObject fileUpload(Boolean status, String jsonp_cb, String target) throws Exception
162162
{
163163
DCObject args = DCObject.create();

0 commit comments

Comments
 (0)