-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed names to match reference. Need Client. Working all. Ready for…
… beta 0.9 release.
- Loading branch information
1 parent
150e961
commit bdb915d
Showing
8 changed files
with
491 additions
and
464 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
//} |
Oops, something went wrong.