11package http ;
22
3-
43import java .io .IOException ;
54
65import org .json .JSONException ;
76import org .json .JSONObject ;
87
9- import platform .Platform .ContentTypeSelection ;
10-
118import com .squareup .okhttp .Request ;
129import com .squareup .okhttp .Response ;
1310import com .squareup .okhttp .ResponseBody ;
1411
15-
1612public 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}
0 commit comments