示例#1
0
        public KeyValuePair <EGAHTTPApiResponse, JSONClass> RequestInitReturningDict()
#endif
        {
            JSONClass          json;
            EGAHTTPApiResponse result  = EGAHTTPApiResponse.NoResponse;
            string             gameKey = GAState.GameKey;

            // Generate URL
            string url = baseUrl + "/" + gameKey + "/" + initializeUrlPath;

            GALogger.D("Sending 'init' URL: " + url);

            JSONClass initAnnotations = GAState.GetInitAnnotations();

            // make JSON string from data
            string JSONstring = initAnnotations.ToString();

            if (string.IsNullOrEmpty(JSONstring))
            {
                result = EGAHTTPApiResponse.JsonEncodeFailed;
                json   = null;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
            }

            string         body                = "";
            HttpStatusCode responseCode        = (HttpStatusCode)0;
            string         responseDescription = "";
            string         authorization       = "";

            try
            {
                byte[]         payloadData = CreatePayloadData(JSONstring, useGzip);
                HttpWebRequest request     = CreateRequest(url, payloadData, useGzip);
                authorization = request.Headers[HttpRequestHeader.Authorization];
#if WINDOWS_UWP || WINDOWS_WSA
                using (Stream dataStream = await request.GetRequestStreamAsync())
#else
                using (Stream dataStream = request.GetRequestStream())
#endif
                {
                    dataStream.Write(payloadData, 0, payloadData.Length);
                }

#if WINDOWS_UWP || WINDOWS_WSA
                using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
#else
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
#endif
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            responseCode        = response.StatusCode;
                            responseDescription = response.StatusDescription;

                            // print result
                            body = responseString;
                        }
                    }
                }
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    using (HttpWebResponse response = (HttpWebResponse)e.Response)
                    {
                        using (Stream streamResponse = response.GetResponseStream())
                        {
                            using (StreamReader streamRead = new StreamReader(streamResponse))
                            {
                                string responseString = streamRead.ReadToEnd();

                                responseCode        = response.StatusCode;
                                responseDescription = response.StatusDescription;

                                body = responseString;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                GALogger.E(e.ToString());
            }

            // process the response
            GALogger.D("init request content : " + body);

            JSONNode           requestJsonDict     = JSON.Parse(body);
            EGAHTTPApiResponse requestResponseEnum = ProcessRequestResponse(responseCode, responseDescription, body, "Init");

            // if not 200 result
            if (requestResponseEnum != EGAHTTPApiResponse.Ok && requestResponseEnum != EGAHTTPApiResponse.BadRequest)
            {
                GALogger.D("Failed Init Call. URL: " + url + ", Authorization: " + authorization + ", JSONString: " + JSONstring);
                result = requestResponseEnum;
                json   = null;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
            }

            if (requestJsonDict == null)
            {
                GALogger.D("Failed Init Call. Json decoding failed");
                result = EGAHTTPApiResponse.JsonDecodeFailed;
                json   = null;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
            }

            // print reason if bad request
            if (requestResponseEnum == EGAHTTPApiResponse.BadRequest)
            {
                GALogger.D("Failed Init Call. Bad request. Response: " + requestJsonDict.AsObject.ToString());
                // return bad request result
                result = requestResponseEnum;
                json   = null;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
            }

            // validate Init call values
            JSONClass validatedInitValues = GAValidator.ValidateAndCleanInitRequestResponse(requestJsonDict);

            if (validatedInitValues == null)
            {
                result = EGAHTTPApiResponse.BadResponse;
                json   = null;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
            }

            // all ok
            result = EGAHTTPApiResponse.Ok;
            json   = validatedInitValues;
            return(new KeyValuePair <EGAHTTPApiResponse, JSONClass>(result, json));
        }