Beispiel #1
0
        public void SendSdkErrorEvent(EGASdkErrorType type)
        {
            if (!GAState.IsEventSubmissionEnabled)
            {
                return;
            }

            string gameKey   = GAState.GameKey;
            string secretKey = GAState.GameSecret;

            // Validate
            if (!GAValidator.ValidateSdkErrorEvent(gameKey, secretKey, type))
            {
                return;
            }

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

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

            string payloadJSONString = "";

            JSONObject json = GAState.GetSdkErrorEventAnnotations();

            string typeString = SdkErrorTypeToString(type);

            json.Add("type", typeString);

            List <JSONNode> eventArray = new List <JSONNode>();

            eventArray.Add(json);
            payloadJSONString = GAUtilities.ArrayOfObjectsToJsonString(eventArray);

            if (string.IsNullOrEmpty(payloadJSONString))
            {
                GALogger.W("sendSdkErrorEvent: JSON encoding failed.");
                return;
            }

            GALogger.D("sendSdkErrorEvent json: " + payloadJSONString);
            byte[]       payloadData  = Encoding.UTF8.GetBytes(payloadJSONString);
            SdkErrorTask sdkErrorTask = new SdkErrorTask(type, payloadData, secretKey);

            sdkErrorTask.Execute(url);
        }
Beispiel #2
0
        public KeyValuePair <EGAHTTPApiResponse, JSONNode> SendEventsInArray(List <JSONNode> eventArray)
#endif
        {
            JSONNode json;

            if (eventArray.Count == 0)
            {
                GALogger.D("sendEventsInArray called with missing eventArray");
            }

            EGAHTTPApiResponse result  = EGAHTTPApiResponse.NoResponse;
            string             gameKey = GAState.GameKey;

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

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

            // make JSON string from data
            string JSONstring = GAUtilities.ArrayOfObjectsToJsonString(eventArray);

            if (JSONstring.Length == 0)
            {
                GALogger.D("sendEventsInArray JSON encoding failed of eventArray");
                json   = null;
                result = EGAHTTPApiResponse.JsonEncodeFailed;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONNode>(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());
            }

            GALogger.D("events request content: " + body);

            EGAHTTPApiResponse requestResponseEnum = ProcessRequestResponse(responseCode, responseDescription, body, "Events");

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

            // decode JSON
            JSONNode requestJsonDict = JSON.Parse(body);

            if (requestJsonDict == null)
            {
                json   = null;
                result = EGAHTTPApiResponse.JsonDecodeFailed;
                return(new KeyValuePair <EGAHTTPApiResponse, JSONNode>(result, json));
            }

            // print reason if bad request
            if (requestResponseEnum == EGAHTTPApiResponse.BadRequest)
            {
                GALogger.D("Failed Events Call. Bad request. Response: " + requestJsonDict.ToString());
            }

            // return response
            json   = requestJsonDict;
            result = requestResponseEnum;
            return(new KeyValuePair <EGAHTTPApiResponse, JSONNode>(result, json));
        }