Ejemplo n.º 1
0
        public override async Task <ITraktCheckinPostErrorResponse> ReadObjectAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
        {
            if (jsonReader == null)
            {
                return(await Task.FromResult(default(ITraktCheckinPostErrorResponse)));
            }

            if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartObject)
            {
                ITraktCheckinPostErrorResponse checkinErrorResponse = new TraktCheckinPostErrorResponse();

                while (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.PropertyName)
                {
                    var propertyName = jsonReader.Value.ToString();

                    switch (propertyName)
                    {
                    case JsonProperties.CHECKIN_POST_ERROR_RESPONSE_PROPERTY_NAME_EXPIRES_AT:
                    {
                        var value = await JsonReaderHelper.ReadDateTimeValueAsync(jsonReader, cancellationToken);

                        if (value.First)
                        {
                            checkinErrorResponse.ExpiresAt = value.Second;
                        }

                        break;
                    }

                    default:
                        await JsonReaderHelper.ReadAndIgnoreInvalidContentAsync(jsonReader, cancellationToken);

                        break;
                    }
                }

                return(checkinErrorResponse);
            }

            return(await Task.FromResult(default(ITraktCheckinPostErrorResponse)));
        }
        public void Test_TraktCheckinPostErrorResponse_Default_Constructor()
        {
            var checkinErrorResponse = new TraktCheckinPostErrorResponse();

            checkinErrorResponse.ExpiresAt.Should().BeNull();
        }
Ejemplo n.º 3
0
        private async Task ErrorHandling(HttpResponseMessage response)
        {
            var responseContent = string.Empty;

            if (response.Content != null)
            {
                responseContent = await response.Content.ReadAsStringAsync();
            }

            var code = response.StatusCode;

            switch (code)
            {
            case HttpStatusCode.NotFound:
            {
                if (RequestObjectType.HasValue)
                {
                    switch (RequestObjectType.Value)
                    {
                    case TraktRequestObjectType.Episodes:
                        throw new TraktEpisodeNotFoundException(Id, Season, Episode)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Seasons:
                        throw new TraktSeasonNotFoundException(Id, Season)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Shows:
                        throw new TraktShowNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Movies:
                        throw new TraktMovieNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.People:
                        throw new TraktPersonNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Comments:
                        throw new TraktCommentNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Lists:
                        throw new TraktListNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };

                    case TraktRequestObjectType.Unspecified:
                    default:
                        throw new TraktObjectNotFoundException(Id)
                              {
                                  RequestUrl         = Url,
                                  RequestBody        = RequestBodyJson,
                                  Response           = responseContent,
                                  ServerReasonPhrase = response.ReasonPhrase
                              };
                    }
                }

                throw new TraktNotFoundException($"Resource not found - Reason Phrase: {response.ReasonPhrase}");
            }

            case HttpStatusCode.BadRequest:
                throw new TraktBadRequestException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.Unauthorized:
                throw new TraktAuthorizationException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.Forbidden:
                throw new TraktForbiddenException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.MethodNotAllowed:
                throw new TraktMethodNotFoundException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.Conflict:
                if (IsCheckinRequest)
                {
                    TraktCheckinPostErrorResponse errorResponse = null;

                    if (!string.IsNullOrEmpty(responseContent))
                    {
                        errorResponse = Json.Deserialize <TraktCheckinPostErrorResponse>(responseContent);
                    }

                    throw new TraktCheckinException("checkin is already in progress")
                          {
                              RequestUrl         = Url,
                              RequestBody        = RequestBodyJson,
                              Response           = responseContent,
                              ServerReasonPhrase = response.ReasonPhrase,
                              ExpiresAt          = errorResponse?.ExpiresAt
                          };
                }

                throw new TraktConflictException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.InternalServerError:
                throw new TraktServerException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case HttpStatusCode.BadGateway:
                throw new TraktBadGatewayException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case (HttpStatusCode)412:
                throw new TraktPreconditionFailedException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case (HttpStatusCode)422:
                throw new TraktValidationException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case (HttpStatusCode)429:
                throw new TraktRateLimitException()
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case (HttpStatusCode)503:
            case (HttpStatusCode)504:
                throw new TraktServerUnavailableException("Service Unavailable - server overloaded (try again in 30s)")
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          StatusCode         = HttpStatusCode.ServiceUnavailable,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };

            case (HttpStatusCode)520:
            case (HttpStatusCode)521:
            case (HttpStatusCode)522:
                throw new TraktServerUnavailableException("Service Unavailable - Cloudflare error")
                      {
                          RequestUrl         = Url,
                          RequestBody        = RequestBodyJson,
                          StatusCode         = HttpStatusCode.ServiceUnavailable,
                          Response           = responseContent,
                          ServerReasonPhrase = response.ReasonPhrase
                      };
            }

            TraktError error = null;

            try
            {
                error = Json.Deserialize <TraktError>(responseContent);
            }
            catch (Exception ex)
            {
                throw new TraktException("json convert exception", ex);
            }

            var errorMessage = (error == null || string.IsNullOrEmpty(error.Description))
                                    ? $"Trakt API error without content. Response status code was {(int)code}"
                                    : error.Description;

            throw new TraktException(errorMessage)
                  {
                      RequestUrl         = Url,
                      RequestBody        = RequestBodyJson,
                      Response           = responseContent,
                      ServerReasonPhrase = response.ReasonPhrase
                  };
        }
        public void TestTraktCheckinPostErrorResponseDefaultConstructor()
        {
            var checkinErrorResponse = new TraktCheckinPostErrorResponse();

            checkinErrorResponse.ExpiresAt.Should().NotHaveValue();
        }