public IEnumerator <ITask> DeleteGameProfile(string @namespace, string userId, string accessToken,
                                                     string profileId, ResultCallback callback)
        {
            Assert.IsNotNull(@namespace, "Can't delete a game profile! namespace parameter is null!");
            Assert.IsNotNull(userId, "Can't delete a game profile! userId parameter is null!");
            Assert.IsNotNull(accessToken, "Can't delete a game profile! accessToken parameter is null!");
            Assert.IsNotNull(profileId, "Can't delete a game profile! fileSection parameter is null!");


            var request = HttpRequestBuilder
                          .CreateDelete(this.baseUrl + "/soc-profile/public/namespaces/{namespace}/users/{userId}/profiles/{profileId}")
                          .WithPathParam("namespace", @namespace)
                          .WithPathParam("userId", userId)
                          .WithPathParam("profileId", profileId)
                          .Accepts(MediaType.ApplicationJson)
                          .WithBearerAuth(accessToken)
                          .ToRequest();

            HttpWebResponse response = null;

            yield return(Task.Await(request, rsp => response = rsp));

            if (response == null)
            {
                callback.Try(Result.CreateError(ErrorCode.NetworkError, "There is no response"));
                yield break;
            }

            var responseText = response.GetBodyText();

            response.Close();
            Result result;

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
            case HttpStatusCode.NoContent:
                result = Result.CreateOk();
                break;

            case HttpStatusCode.NotFound:
                result = Result.CreateError(ErrorCode.NotFound,
                                            "Delete game profile failed due to the resource not found");
                break;

            default:
                result = Result.CreateError((ErrorCode)response.StatusCode,
                                            "Delete game profile failed with status: " + response.StatusCode);
                break;
            }

            callback.Try(result);
        }
        public IEnumerator <ITask> GetAllGameProfiles(string @namespace, string userId, string accessToken,
                                                      ResultCallback <GameProfile[]> callback)
        {
            Assert.IsNotNull(@namespace, "Can't get all game profiles! namespace parameter is null!");
            Assert.IsNotNull(userId, "Can't get all game profiles! userId parameter is null!");
            Assert.IsNotNull(accessToken, "Can't all game profiles! accessToken parameter is null!");

            var request = HttpRequestBuilder
                          .CreateGet(this.baseUrl + "/soc-profile/public/namespaces/{namespace}/users/{userId}/profiles")
                          .WithPathParam("namespace", @namespace)
                          .WithPathParam("userId", userId)
                          .WithBearerAuth(accessToken)
                          .WithContentType(MediaType.ApplicationJson)
                          .Accepts(MediaType.ApplicationJson)
                          .ToRequest();

            HttpWebResponse response = null;

            yield return(Task.Await(request, rsp => response = rsp));

            if (response == null)
            {
                callback.Try(Result <GameProfile[]> .CreateError(ErrorCode.NetworkError, "There is no response"));
                yield break;
            }

            var responseText = response.GetBodyText();

            response.Close();
            Result <GameProfile[]> result;

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                try
                {
                    GameProfile[] gameProfiles = SimpleJson.SimpleJson.DeserializeObject <GameProfile[]>(responseText);
                    result = Result <GameProfile[]> .CreateOk(gameProfiles);
                }
                catch (ArgumentException ex)
                {
                    result = Result <GameProfile[]> .CreateError(ErrorCode.InvalidResponse,
                                                                 "Get game profiles failed to deserialize response body: " + ex.Message);
                }

                break;

            case HttpStatusCode.NotFound:
                result = Result <GameProfile[]> .CreateError(ErrorCode.CategoryNotFound,
                                                             "Get game profiles failed due to the resource not found");

                break;

            default:
                result = Result <GameProfile[]> .CreateError((ErrorCode)response.StatusCode,
                                                             "Get game profiles failed with status: " + response.StatusCode);

                break;
            }

            callback.Try(result);
        }
Exemplo n.º 3
0
        public static Result <T> TryParseJsonBody <T>(this HttpWebResponse response)
        {
            if (response == null)
            {
                return(Result <T> .CreateError(ErrorCode.NetworkError, "There is no response."));
            }

            var responseText = response.GetBodyText();

            response.Close();

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
            case HttpStatusCode.Created:
                try
                {
                    var createResponse = SimpleJson.SimpleJson.DeserializeObject <T>(responseText);
                    return(Result <T> .CreateOk(createResponse));
                }
                catch (Exception ex)
                {
                    return(Result <T> .CreateError(ErrorCode.ErrorFromException, ex.Message));
                }

            case HttpStatusCode.Accepted:
            case HttpStatusCode.NoContent:
            case HttpStatusCode.ResetContent:
            case HttpStatusCode.PartialContent:
            case HttpStatusCode.Ambiguous:
            case HttpStatusCode.Moved:
            case HttpStatusCode.Redirect:
            case HttpStatusCode.SeeOther:
            case HttpStatusCode.NotModified:
            case HttpStatusCode.UseProxy:
            case HttpStatusCode.Unused:
            case HttpStatusCode.RedirectKeepVerb:
                return(Result <T> .CreateError(ErrorCode.InvalidResponse, "JSON response body expected but instead " +
                                               "found HTTP Response with Status " + response.StatusCode));

            case HttpStatusCode.BadRequest:
            case HttpStatusCode.Unauthorized:
            case HttpStatusCode.PaymentRequired:
            case HttpStatusCode.Forbidden:
            case HttpStatusCode.NotFound:
            case HttpStatusCode.MethodNotAllowed:
            case HttpStatusCode.NotAcceptable:
            case HttpStatusCode.ProxyAuthenticationRequired:
            case HttpStatusCode.RequestTimeout:
            case HttpStatusCode.Conflict:
            case HttpStatusCode.Gone:
            case HttpStatusCode.LengthRequired:
            case HttpStatusCode.PreconditionFailed:
            case HttpStatusCode.RequestEntityTooLarge:
            case HttpStatusCode.RequestUriTooLong:
            case HttpStatusCode.UnsupportedMediaType:
            case HttpStatusCode.RequestedRangeNotSatisfiable:
            case HttpStatusCode.ExpectationFailed:
                if (string.IsNullOrEmpty(responseText))
                {
                    return(Result <T> .CreateError((ErrorCode)response.StatusCode));
                }

                try
                {
                    var error = SimpleJson.SimpleJson.DeserializeObject <ServiceError>(responseText);

                    if (error.numericErrorCode == 0)
                    {
                        return(Result <T> .CreateError((ErrorCode)response.StatusCode));
                    }

                    return(Result <T> .CreateError((ErrorCode)error.numericErrorCode, error.errorMessage));
                }
                catch (Exception ex)
                {
                    return(Result <T> .CreateError(ErrorCode.ErrorFromException, ex.Message));
                }

            default:
                if (!string.IsNullOrEmpty(responseText))
                {
                    return(Result <T> .CreateError((ErrorCode)response.StatusCode, "Unknown Service Error: " +
                                                   responseText));
                }
                else
                {
                    return(Result <T> .CreateError((ErrorCode)response.StatusCode));
                }
            }
        }