Skip to content

Commit

Permalink
major changes to Platform and APIResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
vyshakhbabji committed Nov 4, 2015
1 parent 135786e commit c5b746a
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 284 deletions.
102 changes: 47 additions & 55 deletions src/http/APIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
Expand All @@ -23,93 +27,81 @@ public ResponseBody body() {
return this.response.body();
}

@SuppressWarnings("finally")
public String error() {
if (this.response == null || this.ok()) {
return null;
}

String message = "HTTP" + this.response().code();

JSONObject data;

try {
data = this.json();
if (data.getString("message") != null)
message = message + data.getString("message");
else if (data.getString("error_description") != null)
message = message + data.getString("error_description");
else if (data.getString("description") != null)
message = message + data.getString("description");

} catch (JSONException e) {
message = message + "JSONException occured in Class: "
+ this.getClass().getName() + ": " + e.getMessage();
System.err.print("JSONException occured in Class: "
+ this.getClass().getName() + ": " + e.getMessage());
} finally {
return message;
}
String message="";
if (statusCode() >= 300) {
message = "HTTP error code: " + statusCode() + "\n";

try {
JSONObject data = new JSONObject(this.response.body().string());
if (data.getString("message") != null)
message = message + data.getString("message");
else if (data.getString("error_description") != null)
message = message + data.getString("error_description");
else if (data.getString("description") != null)
message = message + data.getString("description");

} catch (JSONException | IOException e) {
message = message + "JSONException occured in Class: "
+ this.getClass().getName() + "\n" + e.getMessage();
}
}

return message;
}

protected String getContentType() {
return this.response.headers().get("Content-Type");
}

protected boolean isContentType(String contentType) {
return this.response().body().contentType().toString()
.equalsIgnoreCase(contentType);
return getContentType().toString().equalsIgnoreCase(contentType);
}

public JSONObject json() {
JSONObject jObject = new JSONObject();
public JsonElement json() {
JsonElement jObject = new JsonObject();
try {
jObject = new JSONObject(response.body().string());
throw new IOException();
JsonParser parser = new JsonParser();
jObject = parser.parse(response.body().string());
return jObject;
} catch (JSONException e) {
System.err
.print("JSONException occured while converting the HTTP response to JSON in Class: "
+ this.getClass().getName() + ": " + e.getMessage());
.print("JSONException occured while converting the HTTP response to JSON in Class: "+ e.getStackTrace());
} catch (IOException e) {
System.err
.print("IOException occured while converting the HTTP response to JSON in Class: "
+ this.getClass().getName() + ": " + e.getMessage());
.print("IOException occured while converting the HTTP response to JSON in Class: "
+ this.getClass().getName() + ": " + e.getStackTrace());
}
return jObject;
}

public boolean ok() {
int status = this.response.code();
return (status >= 200 && status < 300);
}

public ResponseBody raw() {
return this.body();
return (statusCode() >= 200 && statusCode() < 300);
}

public Request request() {
return this.request;
}

public Headers requestHeaders() {
return request.headers();
}

public Response response() {
return this.response;
}

public String text() throws IOException {
String responseAsText = "";
try {
responseAsText = response.body().string();
return responseAsText;
} catch (IOException e) {
throw e;
}

public Headers responseHeaders() {
return response.headers();
}
public String headers(){
return response.headers().toString();

public int statusCode() {
return this.response.code();
}

// todo: multipart def
// todo: break_into_parts
public String text() throws IOException {
return body().string();

}
}
10 changes: 4 additions & 6 deletions src/platform/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

public class Auth {

Expand Down Expand Up @@ -45,14 +44,13 @@ public String refreshToken() {
return this.refresh_token;
}

public boolean refreshTokenValid(){
public boolean refreshTokenValid() {
GregorianCalendar cal = new GregorianCalendar();
if (this.refresh_token_expire_time != null)
cal.setTime(this.refresh_token_expire_time);
return this.isTokenDateValid(cal);
}


public void reset() {
this.token_type = "";
this.remember = "";
Expand All @@ -69,9 +67,9 @@ public void reset() {
this.owner_id = "";
}

public Auth setData(Map<String, String> authData) {
public Auth setData(HashMap<String, String> authData) {

if(authData==null)
if (authData == null)
return this;

if (authData.containsKey("remember"))
Expand Down Expand Up @@ -101,7 +99,7 @@ public Auth setData(Map<String, String> authData) {

}

//refresh token
// refresh token

if (authData.containsKey("refresh_token")) {
this.refresh_token = authData.get("refresh_token");
Expand Down
Loading

0 comments on commit c5b746a

Please sign in to comment.