55import java .net .*;
66import java .io .*;
77import com .google .gson .*;
8- import playfab .*;
9- import playfab .PlayFabErrors .*;
8+
9+ import playfab .PlayFabErrors .PlayFabError ;
10+ import playfab .PlayFabErrors .PlayFabErrorCode ;
11+ import playfab .PlayFabErrors .PlayFabJsonError ;
12+ import playfab .PlayFabErrors .PlayFabJsonSuccess ;
1013
1114public class PlayFabHTTP {
12-
13- private static Gson gson = new GsonBuilder ().setDateFormat ("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'" ).create ();
14-
15- public static class PlayFabJsonError {
16- public int Code ;
17- public String Status ;
18- public String Error ;
19- public int ErrorCode ;
20- public String ErrorMessage ;
21- public Map <String , String []> ErrorDetails = null ;
22- }
15+ private static Gson gson = new GsonBuilder ().setDateFormat ("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'" ).create ();
2316
24- public static class PlayFabJsonSuccess <ResultT >{
25- public int Code ;
26- public String Status ;
27- public ResultT Data ;
28- }
29-
30- public static FutureTask <Object > doPost (final String url , final Object request , final String authType , final String authKey ) {
31- return new FutureTask <Object >(new Callable <Object >() {
32- public Object call () throws Exception {
33- return doPostPrivate (url , request , authType , authKey );
34- }
35- });
36- }
37-
17+ public static FutureTask <Object > doPost (final String url , final Object request , final String authType , final String authKey ) {
18+ return new FutureTask <Object >(new Callable <Object >() {
19+ public Object call () throws Exception {
20+ return doPostPrivate (url , request , authType , authKey );
21+ }
22+ });
23+ }
24+
3825 private static Object doPostPrivate (String url , Object request , String authType , String authKey ) throws Exception {
3926 String bodyString = null ;
40-
27+
4128 if (request == null ) {
42- bodyString = "{}" ;
29+ bodyString = "{}" ;
4330 }
4431 else if (request instanceof String ) {
45- bodyString = (String )request ;
32+ bodyString = (String )request ;
4633 }
4734 else {
48- bodyString = gson .toJson (request );
35+ bodyString = gson .toJson (request );
4936 }
5037
5138 // System.out.println("Sending: " + bodyString);
@@ -54,77 +41,71 @@ else if (request instanceof String) {
5441 con .setRequestMethod ("POST" );
5542 con .setRequestProperty ("Content-Type" , "application/json" );
5643 if (authType != null ) {
57- con .setRequestProperty (authType , authKey );
44+ con .setRequestProperty (authType , authKey );
5845 }
5946 con .setRequestProperty ("X-PlayFabSDK" , PlayFabVersion .getVersionString ());
6047 con .setDoOutput (true );
6148 con .setDoInput (true );
49+
50+ // Make the API-Call and get the normal response httpCode
51+ int httpCode = 503 ; // default to SERVICE_UNAVAILABLE
6252 try {
63- OutputStreamWriter writer = new OutputStreamWriter (con .getOutputStream ());
64- writer .write (bodyString );
65- writer .close ();
66-
67- String responseString = null ;
68- try {
69- responseString = receive (con .getInputStream ());
70- } catch (IOException e ) {
71- responseString = receive (con .getErrorStream ());
72- }
73-
74- if (con .getResponseCode () != 200 ) {
75- PlayFabError error = new PlayFabError ();
76- if (responseString == null || responseString .isEmpty () || con .getResponseCode () == 404 ) {
77- error .HttpCode = con .getResponseCode ();
78- return error ;
79- }
80- PlayFabErrors .PlayFabJsonError errorResult = null ;
81-
82- try {
83- errorResult = gson .fromJson (responseString , PlayFabErrors .PlayFabJsonError .class );
84- } catch (Exception e ) {
85- error .HttpCode = con .getResponseCode ();
86- error .Error = PlayFabErrorCode .JsonParseError ;
87- error .ErrorMessage = e .getLocalizedMessage ();
88- return error ;
89- }
90-
91- error .HttpCode = errorResult .code ;
92- error .HttpStatus = errorResult .status ;
93- error .Error = PlayFabErrorCode .getFromCode (errorResult .errorCode );
94- error .ErrorMessage = errorResult .errorMessage ;
95- error .ErrorDetails = errorResult .errorDetails ;
96- return error ;
97-
98- }
99-
100- if (responseString == null || responseString .length () == 0 ) {
101- PlayFabError error = new PlayFabError ();
102- error .Error = PlayFabErrorCode .Unknown ;
103- error .ErrorMessage = "Internal server error" ;
104- return error ;
53+ OutputStreamWriter writer = new OutputStreamWriter (con .getOutputStream ());
54+ writer .write (bodyString );
55+ writer .close ();
56+ httpCode = con .getResponseCode ();
57+ } catch (Exception e ) {
58+ return GeneratePfError (httpCode , PlayFabErrorCode .ServiceUnavailable , "Failed to post to server: " + url );
59+ }
60+
61+ // Get the response string
62+ String responseString = null ;
63+ try {
64+ responseString = receive (con .getInputStream ());
65+ } catch (IOException e ) {
66+ responseString = receive (con .getErrorStream ());
67+ }
68+
69+ // Check for normal error results
70+ if (httpCode != 200 || responseString == null || responseString .isEmpty ()) {
71+ if (responseString == null || responseString .isEmpty () || httpCode == 404 )
72+ return GeneratePfError (httpCode , PlayFabErrorCode .ServiceUnavailable , "Empty server response" );
73+
74+ PlayFabJsonError errorResult = null ;
75+ try {
76+ errorResult = gson .fromJson (responseString , PlayFabJsonError .class );
77+ } catch (Exception e ) {
78+ return GeneratePfError (httpCode , PlayFabErrorCode .JsonParseError , "Server response not proper json :" + responseString );
10579 }
80+
81+ httpCode = errorResult .code ;
82+ return GeneratePfError (httpCode , PlayFabErrorCode .getFromCode (errorResult .errorCode ), errorResult .errorMessage );
83+ }
10684
107- return responseString ;
108-
109- } catch (Exception e ) {
110- PlayFabError error = new PlayFabError ();
111- error .Error = PlayFabErrorCode .ConnectionError ;
112- error .ErrorMessage = e .getLocalizedMessage ();
113- return error ;
85+ return responseString ;
86+ }
87+
88+ public static String receive (InputStream in ) throws IOException {
89+ StringBuilder recieved = new StringBuilder ();
90+ BufferedReader reader = new BufferedReader (new InputStreamReader (in ));
91+
92+ String line = null ;
93+ while ((line = reader .readLine ()) != null ) {
94+ recieved .append (line );
95+ recieved .append ('\n' );
11496 }
97+
98+ return recieved .toString ();
11599 }
100+
101+ public static PlayFabError GeneratePfError (int httpCode , PlayFabErrorCode pfErrorCode , String errorMessage ) {
102+ PlayFabError output = new PlayFabError ();
116103
117- public static String receive (InputStream in ) throws IOException {
118- StringBuilder recieved = new StringBuilder ();
119- BufferedReader reader = new BufferedReader (new InputStreamReader (in ));
120-
121- String line = null ;
122- while ((line = reader .readLine ()) != null ) {
123- recieved .append (line );
124- recieved .append ('\n' );
125- }
126-
127- return recieved .toString ();
104+ output .httpCode = httpCode ;
105+ output .httpStatus = "" + httpCode ; // TODO: Convert this to the right string-name
106+ output .pfErrorCode = pfErrorCode ;
107+ output .errorMessage = errorMessage ;
108+
109+ return output ;
128110 }
129-
130111}
0 commit comments