Skip to content

Commit 4022833

Browse files
committed
Platfrom ready
1 parent ed5361d commit 4022833

11 files changed

Lines changed: 313 additions & 323 deletions

File tree

bin/http/RCHeaders.class

-3.61 KB
Binary file not shown.

bin/http/RCRequest.class

-1.23 KB
Binary file not shown.

bin/http/RCResponse.class

-265 Bytes
Binary file not shown.

bin/platform/Platform$1.class

28 Bytes
Binary file not shown.

bin/platform/Platform.class

3 KB
Binary file not shown.

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ dependencies {
99
testCompile 'junit:junit:4.11'
1010
compile 'org.apache.httpcomponents:httpclient:4.5'
1111
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
12-
compile 'com.google.code.gson:gson:2.3.1'
12+
compile 'com.google.code.gson:gson:2.3.1'
13+
compile 'com.squareup.okhttp:okhttp:2.5.0'
14+
compile 'org.json:json:20141113'
1315

1416

1517
}

src/http/APIResponse.java

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package http;
2+
3+
4+
import com.google.gson.Gson;
5+
import com.google.gson.reflect.TypeToken;
6+
import com.squareup.okhttp.Request;
7+
import com.squareup.okhttp.Response;
8+
import com.squareup.okhttp.ResponseBody;
9+
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
13+
import java.io.IOException;
14+
import java.lang.reflect.Type;
15+
import java.util.HashMap;
16+
17+
18+
public class APIResponse {
19+
20+
protected Response response;
21+
protected Request request;
22+
23+
public APIResponse(Request request, Response response) {
24+
this.request = request;
25+
this.response = response;
26+
}
27+
28+
public Request request() {
29+
return this.response.request();
30+
}
31+
32+
public Response response() {
33+
return this.response;
34+
}
35+
36+
public boolean ok() {
37+
int status = this.response.code();
38+
return (status >= 200 && status < 300);
39+
}
40+
41+
public Response raw() {
42+
return response();
43+
}
44+
45+
public ResponseBody body() {
46+
return this.response.body();
47+
}
48+
49+
public String text() {
50+
51+
String responseAsText = "";
52+
try {
53+
responseAsText = response.body().string();
54+
throw new IOException();
55+
} catch (IOException e) {
56+
System.err.print("IOException occured while converting the HTTP response to string in Class: " + this.getClass().getName() + ": " + e.getMessage());
57+
}
58+
59+
return responseAsText;
60+
}
61+
62+
public JSONObject json_dict() {
63+
64+
JSONObject jObject = new JSONObject();
65+
try {
66+
if (isContentType("application/json"))
67+
jObject = new JSONObject(text());
68+
else {
69+
throw new IOException();
70+
}
71+
} catch (JSONException e) {
72+
System.err.print("JSONException occured while converting the HTTP response to JSON Dictonary in Class: " + this.getClass().getName() + ": " + e.getMessage());
73+
} catch (IOException e) {
74+
System.err.print("IOException occured while converting the HTTP response to JSON Dictonary in Class: " + this.getClass().getName() + ": " + e.getMessage());
75+
}
76+
return jObject;
77+
}
78+
79+
80+
public JSONObject json() {
81+
JSONObject object = new JSONObject();
82+
try {
83+
object = new JSONObject(response.body().string());
84+
throw new IOException();
85+
} catch (JSONException e) {
86+
System.err.print("JSONException occured while converting the HTTP response to JSON in Class: " + this.getClass().getName() + ": " + e.getMessage());
87+
} catch (IOException e) {
88+
System.err.print("IOException occured while converting the HTTP response to JSON in Class: " + this.getClass().getName() + ": " + e.getMessage());
89+
}
90+
return object;
91+
}
92+
93+
public HashMap hashMap() throws IOException {
94+
Gson gson = new Gson();
95+
Type mapType = new TypeToken<HashMap<String, String>>() {
96+
}.getType();
97+
HashMap<String, String> jsonMap = new HashMap<>();
98+
try {
99+
jsonMap = gson.fromJson(this.text(), mapType);
100+
throw new IOException();
101+
} catch (IOException e) {
102+
System.err.print("IOException occured while converting the HTTP response to JSON in Class: " + this.getClass().getName() + ": " + e.getMessage());
103+
}
104+
return jsonMap;
105+
}
106+
107+
108+
public boolean isContentType(String contentType) {
109+
return this.response().body().contentType().toString().equalsIgnoreCase(contentType);
110+
}
111+
112+
public String getContentType() {
113+
return this.response.headers().get("Content-Type");
114+
}
115+
116+
@SuppressWarnings("finally")
117+
public String error() {
118+
if (this.response == null || this.ok()) {
119+
return null;
120+
}
121+
122+
String message = "HTTP" + this.response().code();
123+
124+
JSONObject data;
125+
126+
127+
try {
128+
data = this.json_dict();
129+
if (data.getString("message") != null) message = message + data.getString("message");
130+
else if (data.getString("error_description") != null)
131+
message = message + data.getString("error_description");
132+
else if (data.getString("description") != null)
133+
message = message + data.getString("description");
134+
135+
} catch (JSONException e) {
136+
message = message + "JSONException occured in Class: " + this.getClass().getName() + ": " + e.getMessage();
137+
System.err.print("JSONException occured in Class: " + this.getClass().getName() + ": " + e.getMessage());
138+
} finally {
139+
return message;
140+
}
141+
}
142+
143+
//todo: multipart def
144+
//todo: break_into_parts
145+
146+
147+
}

src/http/RCHeaders.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/http/RCRequest.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/http/RCResponse.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)