Skip to content

Commit

Permalink
Changed names to match reference. Need Client. Working all. Ready for…
Browse files Browse the repository at this point in the history
… beta 0.9 release.
  • Loading branch information
vyshakhbabji committed Oct 19, 2015
1 parent 150e961 commit bdb915d
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 464 deletions.
Binary file modified bin/platform/Auth.class
Binary file not shown.
Binary file modified bin/platform/Platform$1.class
Binary file not shown.
Binary file modified bin/platform/Platform.class
Binary file not shown.
221 changes: 110 additions & 111 deletions src/http/APIResponse.java
Original file line number Diff line number Diff line change
@@ -1,129 +1,128 @@
package http;


import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import platform.Platform.ContentTypeSelection;

import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;


public class APIResponse {

protected Request request;
protected Response response;

public APIResponse(Request request, Response response) {
this.request = request;
this.response = response;
}


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

public ResponseBody raw() {
return this.body();
}

public ResponseBody body() {
return this.response.body();
}

public String text() throws IOException {

String responseAsText = "";
try {
responseAsText = response.body().string();
return responseAsText;
} catch (IOException e) {
throw e;
// System.err.print("IOException occured while converting the HTTP response to string in Class: " + this.getClass().getName() + ": " + e.getMessage());
}

}

//json_dict not necessary

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

// public APIResponse multipart() throws Exception{
//
// if(this.isContentType(ContentTypeSelection.MULTIPART_TYPE_MARKDOWN.value.toString())){
// throw new Exception("Exception occured.Response is not Batch (Multipart) ");
// }
//
//
//
// }




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

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

JSONObject data;

protected Request request;
protected Response response;

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");
public APIResponse(Response response) {
this.request = response.request();
this.response = response;
}

} 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;
}
}

public Request request() {
return this.response.request();
}
public ResponseBody body() {
return this.response.body();
}

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


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

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


//todo: multipart def
//todo: break_into_parts
@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;
}
}

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

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

// json_dict not necessary

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

// public APIResponse multipart() throws Exception{
//
// if(this.isContentType(ContentTypeSelection.MULTIPART_TYPE_MARKDOWN.value.toString())){
// throw new
// Exception("Exception occured.Response is not Batch (Multipart) ");
// }
//
//
//
// }

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

public ResponseBody raw() {
return this.body();
}

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

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;
// System.err.print("IOException occured while converting the HTTP response to string in Class: "
// + this.getClass().getName() + ": " + e.getMessage());
}

}

// todo: multipart def
// todo: break_into_parts

}
81 changes: 81 additions & 0 deletions src/http/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//package http;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map.Entry;
//
//import platform.Auth;
//import platform.Platform;
//
//import com.squareup.okhttp.OkHttpClient;
//import com.squareup.okhttp.Request;
//import com.squareup.okhttp.RequestBody;
//import com.squareup.okhttp.Response;
//import com.squareup.okhttp.Request.Builder;
//
//public class Client {
//
// Request request;
// Response response;
// protected Platform p;
// protected Auth auth;
//
// public Client(Platform platform, Auth auth){
// this.auth= auth;
// this.p=platform;
// }
//
//
//
// protected String getAuthHeader() {
// return this.auth.tokenType() + " " + p.getAccessToken();
// }
//
// protected Builder requestBuilder(HashMap<String, String> hm) {
//
// if (hm == null) {
// hm = new HashMap<String, String>();
// }
// hm.put("Authorization", getAuthHeader());
//
//
// Builder requestBuilder = new Request.Builder();
// for (Entry<String, String> entry : hm.entrySet()) {
// requestBuilder.addHeader(entry.getKey(), entry.getValue());
// }
// return requestBuilder;
// }
//
// public APIResponse send(String method, String apiURL, RequestBody body,
// HashMap<String, String> headerMap) throws IOException {
//
// // this.isAuthorized();
// String URL = server.value + apiURL;
// OkHttpClient client = new OkHttpClient();
//
// try {
// System.out.println(getAuthHeader());
// if (method.equalsIgnoreCase("get")) {
// request = requestBuilder(headerMap).url(URL).build();
// } else if (method.equalsIgnoreCase("delete")) {
// request = requestBuilder(headerMap).url(URL).delete().build();
// } else {
// if (method.equalsIgnoreCase("post")) {
// request = requestBuilder(headerMap).url(URL).post(body)
// .build();
// } else if (method.equalsIgnoreCase("put")) {
// request = requestBuilder(headerMap).url(URL).put(body)
// .build();
// }
// }
//
// } catch (Exception e) {
// System.err.print("Failed APICall. Exception occured in Class: "
// + this.getClass().getName() + ": " + e.getMessage()
// + l.getClassName() + "/" + l.getMethodName() + ":"
// + l.getLineNumber());
// }
// response = client.newCall(request).execute();
// return new APIResponse(response);
// }
//}
Loading

0 comments on commit bdb915d

Please sign in to comment.