internal PlayFabException(PlayFabJsonError jsonError)
     : base(jsonError.errorMessage)
 {
     HttpCode     = jsonError.code;
     HttpStatus   = jsonError.status;
     Error        = (PlayFabErrorCode)jsonError.errorCode;
     ErrorDetails = jsonError.errorDetails;
 }
Exemple #2
0
 internal PlayFabException(PlayFabJsonError jsonError)
     : base(jsonError.GenerateErrorReport())
 {
     HttpCode     = jsonError.code;
     HttpStatus   = jsonError.status;
     Error        = (PlayFabErrorCode)jsonError.errorCode;
     ErrorDetails = jsonError.errorDetails;
 }
        internal static async Task <string> DoPost(this PlayFabSettings settings, string apiName, string url, object request, string authType, string authKey)
        {
            string bodyString = null;
            var    serializer = JsonSerializer.Create(settings.JsonSettings);

            if (request == null)
            {
                bodyString = "{}";
            }
            else if (request is String)
            {
                bodyString = (String)request;
            }
            else
            {
                StringWriter jsonString = new StringWriter();
                using (var writer = new JsonTextWriter(jsonString)
                {
                    Formatting = settings.JsonFormatting
                })
                {
                    serializer.Serialize(writer, request);
                    bodyString = jsonString.ToString();
                }
            }

            if (settings.ApiCallback != null)
            {
                settings.ApiCallback(PlayFabApiEvent.SendingRequest, settings, apiName, "POST", url, bodyString);
            }

            HttpResponseMessage httpResponse = null;
            String httpResponseString        = null;

            using (HttpClient client = settings.CreateClient())
                using (ByteArrayContent postBody = new ByteArrayContent(Encoding.UTF8.GetBytes(bodyString)))
                {
                    postBody.Headers.Add("Content-Type", "application/json");
                    if (authType != null)
                    {
                        postBody.Headers.Add(authType, authKey);
                    }
                    postBody.Headers.Add("X-PlayFabSDK", PlayFabVersion.getVersionString());

                    httpResponse = await client.PostAsync(url, postBody);

                    httpResponseString = await httpResponse.Content.ReadAsStringAsync();
                }

            if (settings.ApiCallback != null)
            {
                settings.ApiCallback(PlayFabApiEvent.ReceivedReply, settings, apiName, "POST", url, httpResponseString);
            }

            if (!httpResponse.IsSuccessStatusCode)
            {
                if (String.IsNullOrEmpty(httpResponseString) || httpResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new PlayFabHttpException(httpResponse.StatusCode, httpResponse.ReasonPhrase);
                }

                PlayFabJsonError errorResult = null;

                errorResult = serializer.Deserialize <PlayFabJsonError>(new JsonTextReader(new StringReader(httpResponseString)));

                throw new PlayFabException(errorResult);
            }

            if (String.IsNullOrEmpty(httpResponseString))
            {
                throw new PlayFabException(PlayFabErrorCode.Unknown, "Internal server error");
            }

            return(httpResponseString);
        }
Exemple #4
0
        public static async Task <object> DoPost(string url, object request, string authType, string authKey)
        {
            string bodyString = null;
            var    serializer = JsonSerializer.Create(PlayFabSettings.JsonSettings);

            if (request == null)
            {
                bodyString = "{}";
            }
            else if (request is String)
            {
                bodyString = (String)request;
            }
            else
            {
                StringWriter jsonString = new StringWriter();
                var          writer     = new JsonTextWriter(jsonString)
                {
                    Formatting = PlayFabSettings.JsonFormatting
                };
                serializer.Serialize(writer, request);
                bodyString = jsonString.ToString();
            }

            HttpClient       client   = new HttpClient();
            ByteArrayContent postBody = new ByteArrayContent(Encoding.UTF8.GetBytes(bodyString));

            postBody.Headers.Add("Content-Type", "application/json");
            if (authType != null)
            {
                postBody.Headers.Add(authType, authKey);
            }
            postBody.Headers.Add("X-PlayFabSDK", PlayFabVersion.getVersionString());

            HttpResponseMessage httpResponse = null;
            String httpResponseString        = null;

            try
            {
                httpResponse = await client.PostAsync(url, postBody);

                httpResponseString = await httpResponse.Content.ReadAsStringAsync();
            }
            catch (HttpRequestException e)
            {
                PlayFabError error = new PlayFabError();
                error.Error        = PlayFabErrorCode.ConnectionError;
                error.ErrorMessage = e.InnerException.Message;
                return(error);
            }
            catch (Exception e)
            {
                PlayFabError error = new PlayFabError();
                error.Error        = PlayFabErrorCode.ConnectionError;
                error.ErrorMessage = e.Message;
                return(error);
            }

            if (!httpResponse.IsSuccessStatusCode)
            {
                PlayFabError error = new PlayFabError();

                if (String.IsNullOrEmpty(httpResponseString) || httpResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    error.HttpCode   = (int)httpResponse.StatusCode;
                    error.HttpStatus = httpResponse.StatusCode.ToString();
                    return(error);
                }


                PlayFabJsonError errorResult = null;
                try
                {
                    errorResult = serializer.Deserialize <PlayFabJsonError>(new JsonTextReader(new StringReader(httpResponseString)));
                }
                catch (Exception e)
                {
                    error.HttpCode   = (int)httpResponse.StatusCode;
                    error.HttpStatus = httpResponse.StatusCode.ToString();

                    error.Error        = PlayFabErrorCode.JsonParseError;
                    error.ErrorMessage = e.Message;

                    return(error);
                }

                error.HttpCode     = errorResult.code;
                error.HttpStatus   = errorResult.status;
                error.Error        = (PlayFabErrorCode)errorResult.errorCode;
                error.ErrorMessage = errorResult.errorMessage;
                error.ErrorDetails = errorResult.errorDetails;
                return(error);
            }

            if (String.IsNullOrEmpty(httpResponseString))
            {
                PlayFabError error = new PlayFabError();
                error.Error        = PlayFabErrorCode.Unknown;
                error.ErrorMessage = "Internal server error";
                return(error);
            }

            return(httpResponseString);
        }