Skip to content

Commit bdb915d

Browse files
committed
Changed names to match reference. Need Client. Working all. Ready for beta 0.9 release.
1 parent 150e961 commit bdb915d

8 files changed

Lines changed: 491 additions & 464 deletions

File tree

bin/platform/Auth.class

-790 Bytes
Binary file not shown.

bin/platform/Platform$1.class

0 Bytes
Binary file not shown.

bin/platform/Platform.class

269 Bytes
Binary file not shown.

src/http/APIResponse.java

Lines changed: 110 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,128 @@
11
package http;
22

3-
43
import java.io.IOException;
54

65
import org.json.JSONException;
76
import org.json.JSONObject;
87

9-
import platform.Platform.ContentTypeSelection;
10-
118
import com.squareup.okhttp.Request;
129
import com.squareup.okhttp.Response;
1310
import com.squareup.okhttp.ResponseBody;
1411

15-
1612
public class APIResponse {
1713

18-
protected Request request;
19-
protected Response response;
20-
21-
public APIResponse(Request request, Response response) {
22-
this.request = request;
23-
this.response = response;
24-
}
25-
26-
27-
public boolean ok() {
28-
int status = this.response.code();
29-
return (status >= 200 && status < 300);
30-
}
31-
32-
public ResponseBody raw() {
33-
return this.body();
34-
}
35-
36-
public ResponseBody body() {
37-
return this.response.body();
38-
}
39-
40-
public String text() throws IOException {
41-
42-
String responseAsText = "";
43-
try {
44-
responseAsText = response.body().string();
45-
return responseAsText;
46-
} catch (IOException e) {
47-
throw e;
48-
// System.err.print("IOException occured while converting the HTTP response to string in Class: " + this.getClass().getName() + ": " + e.getMessage());
49-
}
50-
51-
}
52-
53-
//json_dict not necessary
54-
55-
public JSONObject json() {
56-
JSONObject object = new JSONObject();
57-
try {
58-
object = new JSONObject(response.body().string());
59-
throw new IOException();
60-
} catch (JSONException e) {
61-
System.err.print("JSONException occured while converting the HTTP response to JSON in Class: " + this.getClass().getName() + ": " + e.getMessage());
62-
} catch (IOException e) {
63-
System.err.print("IOException occured while converting the HTTP response to JSON in Class: " + this.getClass().getName() + ": " + e.getMessage());
64-
}
65-
return object;
66-
}
67-
68-
// public APIResponse multipart() throws Exception{
69-
//
70-
// if(this.isContentType(ContentTypeSelection.MULTIPART_TYPE_MARKDOWN.value.toString())){
71-
// throw new Exception("Exception occured.Response is not Batch (Multipart) ");
72-
// }
73-
//
74-
//
75-
//
76-
// }
77-
78-
79-
80-
81-
@SuppressWarnings("finally")
82-
public String error() {
83-
if (this.response == null || this.ok()) {
84-
return null;
85-
}
86-
87-
String message = "HTTP" + this.response().code();
88-
89-
JSONObject data;
90-
14+
protected Request request;
15+
protected Response response;
9116

92-
try {
93-
data = this.json();
94-
if (data.getString("message") != null) message = message + data.getString("message");
95-
else if (data.getString("error_description") != null)
96-
message = message + data.getString("error_description");
97-
else if (data.getString("description") != null)
98-
message = message + data.getString("description");
17+
public APIResponse(Response response) {
18+
this.request = response.request();
19+
this.response = response;
20+
}
9921

100-
} catch (JSONException e) {
101-
message = message + "JSONException occured in Class: " + this.getClass().getName() + ": " + e.getMessage();
102-
System.err.print("JSONException occured in Class: " + this.getClass().getName() + ": " + e.getMessage());
103-
} finally {
104-
return message;
105-
}
106-
}
107-
108-
public Request request() {
109-
return this.response.request();
110-
}
22+
public ResponseBody body() {
23+
return this.response.body();
24+
}
11125

112-
public Response response() {
113-
return this.response;
114-
}
115-
116-
117-
protected String getContentType() {
118-
return this.response.headers().get("Content-Type");
119-
}
120-
121-
protected boolean isContentType(String contentType) {
122-
return this.response().body().contentType().toString().equalsIgnoreCase(contentType);
123-
}
124-
125-
126-
//todo: multipart def
127-
//todo: break_into_parts
26+
@SuppressWarnings("finally")
27+
public String error() {
28+
if (this.response == null || this.ok()) {
29+
return null;
30+
}
31+
32+
String message = "HTTP" + this.response().code();
33+
34+
JSONObject data;
35+
36+
try {
37+
data = this.json();
38+
if (data.getString("message") != null)
39+
message = message + data.getString("message");
40+
else if (data.getString("error_description") != null)
41+
message = message + data.getString("error_description");
42+
else if (data.getString("description") != null)
43+
message = message + data.getString("description");
44+
45+
} catch (JSONException e) {
46+
message = message + "JSONException occured in Class: "
47+
+ this.getClass().getName() + ": " + e.getMessage();
48+
System.err.print("JSONException occured in Class: "
49+
+ this.getClass().getName() + ": " + e.getMessage());
50+
} finally {
51+
return message;
52+
}
53+
}
54+
55+
protected String getContentType() {
56+
return this.response.headers().get("Content-Type");
57+
}
58+
59+
protected boolean isContentType(String contentType) {
60+
return this.response().body().contentType().toString()
61+
.equalsIgnoreCase(contentType);
62+
}
63+
64+
// json_dict not necessary
65+
66+
public JSONObject json() {
67+
JSONObject jObject = new JSONObject();
68+
try {
69+
jObject = new JSONObject(response.body().string());
70+
throw new IOException();
71+
} catch (JSONException e) {
72+
System.err
73+
.print("JSONException occured while converting the HTTP response to JSON in Class: "
74+
+ this.getClass().getName() + ": " + e.getMessage());
75+
} catch (IOException e) {
76+
System.err
77+
.print("IOException occured while converting the HTTP response to JSON in Class: "
78+
+ this.getClass().getName() + ": " + e.getMessage());
79+
}
80+
return jObject;
81+
}
82+
83+
// public APIResponse multipart() throws Exception{
84+
//
85+
// if(this.isContentType(ContentTypeSelection.MULTIPART_TYPE_MARKDOWN.value.toString())){
86+
// throw new
87+
// Exception("Exception occured.Response is not Batch (Multipart) ");
88+
// }
89+
//
90+
//
91+
//
92+
// }
93+
94+
public boolean ok() {
95+
int status = this.response.code();
96+
return (status >= 200 && status < 300);
97+
}
98+
99+
public ResponseBody raw() {
100+
return this.body();
101+
}
102+
103+
public Request request() {
104+
return this.request;
105+
}
106+
107+
public Response response() {
108+
return this.response;
109+
}
110+
111+
public String text() throws IOException {
112+
113+
String responseAsText = "";
114+
try {
115+
responseAsText = response.body().string();
116+
return responseAsText;
117+
} catch (IOException e) {
118+
throw e;
119+
// System.err.print("IOException occured while converting the HTTP response to string in Class: "
120+
// + this.getClass().getName() + ": " + e.getMessage());
121+
}
122+
123+
}
124+
125+
// todo: multipart def
126+
// todo: break_into_parts
128127

129128
}

src/http/Client.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//package http;
2+
//
3+
//import java.io.IOException;
4+
//import java.util.HashMap;
5+
//import java.util.Map.Entry;
6+
//
7+
//import platform.Auth;
8+
//import platform.Platform;
9+
//
10+
//import com.squareup.okhttp.OkHttpClient;
11+
//import com.squareup.okhttp.Request;
12+
//import com.squareup.okhttp.RequestBody;
13+
//import com.squareup.okhttp.Response;
14+
//import com.squareup.okhttp.Request.Builder;
15+
//
16+
//public class Client {
17+
//
18+
// Request request;
19+
// Response response;
20+
// protected Platform p;
21+
// protected Auth auth;
22+
//
23+
// public Client(Platform platform, Auth auth){
24+
// this.auth= auth;
25+
// this.p=platform;
26+
// }
27+
//
28+
//
29+
//
30+
// protected String getAuthHeader() {
31+
// return this.auth.tokenType() + " " + p.getAccessToken();
32+
// }
33+
//
34+
// protected Builder requestBuilder(HashMap<String, String> hm) {
35+
//
36+
// if (hm == null) {
37+
// hm = new HashMap<String, String>();
38+
// }
39+
// hm.put("Authorization", getAuthHeader());
40+
//
41+
//
42+
// Builder requestBuilder = new Request.Builder();
43+
// for (Entry<String, String> entry : hm.entrySet()) {
44+
// requestBuilder.addHeader(entry.getKey(), entry.getValue());
45+
// }
46+
// return requestBuilder;
47+
// }
48+
//
49+
// public APIResponse send(String method, String apiURL, RequestBody body,
50+
// HashMap<String, String> headerMap) throws IOException {
51+
//
52+
// // this.isAuthorized();
53+
// String URL = server.value + apiURL;
54+
// OkHttpClient client = new OkHttpClient();
55+
//
56+
// try {
57+
// System.out.println(getAuthHeader());
58+
// if (method.equalsIgnoreCase("get")) {
59+
// request = requestBuilder(headerMap).url(URL).build();
60+
// } else if (method.equalsIgnoreCase("delete")) {
61+
// request = requestBuilder(headerMap).url(URL).delete().build();
62+
// } else {
63+
// if (method.equalsIgnoreCase("post")) {
64+
// request = requestBuilder(headerMap).url(URL).post(body)
65+
// .build();
66+
// } else if (method.equalsIgnoreCase("put")) {
67+
// request = requestBuilder(headerMap).url(URL).put(body)
68+
// .build();
69+
// }
70+
// }
71+
//
72+
// } catch (Exception e) {
73+
// System.err.print("Failed APICall. Exception occured in Class: "
74+
// + this.getClass().getName() + ": " + e.getMessage()
75+
// + l.getClassName() + "/" + l.getMethodName() + ":"
76+
// + l.getLineNumber());
77+
// }
78+
// response = client.newCall(request).execute();
79+
// return new APIResponse(response);
80+
// }
81+
//}

0 commit comments

Comments
 (0)