예제 #1
0
        public async Task Test_DeviceObjectJsonReader_ReadObject_From_Stream_Not_Valid_1()
        {
            var objectJsonReader = new DeviceObjectJsonReader();

            using (var stream = JSON_NOT_VALID_1.ToStream())
            {
                ITraktDevice traktDevice = await objectJsonReader.ReadObjectAsync(stream);

                traktDevice.Should().NotBeNull();
                traktDevice.DeviceCode.Should().BeNull();
                traktDevice.UserCode.Should().Be("mockUserCode");
                traktDevice.VerificationUrl.Should().Be("mockUrl");
                traktDevice.ExpiresInSeconds.Should().Be(7200U);
                traktDevice.IntervalInSeconds.Should().Be(600U);
            }
        }
예제 #2
0
        public async Task Test_DeviceObjectJsonReader_ReadObject_From_Stream_Incomplete_10()
        {
            var objectJsonReader = new DeviceObjectJsonReader();

            using (var stream = JSON_INCOMPLETE_10.ToStream())
            {
                ITraktDevice traktDevice = await objectJsonReader.ReadObjectAsync(stream);

                traktDevice.Should().NotBeNull();
                traktDevice.DeviceCode.Should().BeNull();
                traktDevice.UserCode.Should().BeNull();
                traktDevice.VerificationUrl.Should().BeNull();
                traktDevice.ExpiresInSeconds.Should().Be(0);
                traktDevice.IntervalInSeconds.Should().Be(600U);
            }
        }
예제 #3
0
        public Task <TraktResponse <ITraktAuthorization> > PollForAuthorizationAsync(ITraktDevice device, string clientId, string clientSecret, CancellationToken cancellationToken = default)
        {
            var request = new AuthorizationPollRequest
            {
                RequestBody = new AuthorizationPollRequestBody
                {
                    ClientId     = clientId,
                    ClientSecret = clientSecret,
                    Device       = device
                }
            };

            var requestHandler = new AuthenticationRequestHandler(Client);

            return(requestHandler.PollForAuthorizationAsync(request, cancellationToken));
        }
예제 #4
0
        public async Task Test_DeviceObjectJsonReader_ReadObject_From_JsonReader_Incomplete_4()
        {
            var objectJsonReader = new DeviceObjectJsonReader();

            using (var reader = new StringReader(JSON_INCOMPLETE_4))
                using (var jsonReader = new JsonTextReader(reader))
                {
                    ITraktDevice traktDevice = await objectJsonReader.ReadObjectAsync(jsonReader);

                    traktDevice.Should().NotBeNull();
                    traktDevice.DeviceCode.Should().Be("mockDeviceCode");
                    traktDevice.UserCode.Should().Be("mockUserCode");
                    traktDevice.VerificationUrl.Should().Be("mockUrl");
                    traktDevice.ExpiresInSeconds.Should().Be(0);
                    traktDevice.IntervalInSeconds.Should().Be(600U);
                }
        }
예제 #5
0
        public async Task Test_DeviceObjectJsonReader_ReadObject_From_JsonReader_Not_Valid_6()
        {
            var objectJsonReader = new DeviceObjectJsonReader();

            using (var reader = new StringReader(JSON_NOT_VALID_6))
                using (var jsonReader = new JsonTextReader(reader))
                {
                    ITraktDevice traktDevice = await objectJsonReader.ReadObjectAsync(jsonReader);

                    traktDevice.Should().NotBeNull();
                    traktDevice.DeviceCode.Should().BeNull();
                    traktDevice.UserCode.Should().BeNull();
                    traktDevice.VerificationUrl.Should().BeNull();
                    traktDevice.ExpiresInSeconds.Should().Be(0);
                    traktDevice.IntervalInSeconds.Should().Be(0);
                }
        }
        public async Task Test_TraktAuthenticationModule_GenerateDevice_With_ClientId()
        {
            string deviceJson = await TestUtility.SerializeObject(MockDevice);

            deviceJson.Should().NotBeNullOrEmpty();

            string      postContent = $"{{ \"client_id\": \"{TestConstants.TRAKT_CLIENT_ID}\" }}";
            TraktClient client      = TestUtility.GetAuthenticationMockClient(GET_DEVICE_URI, postContent, deviceJson);
            TraktResponse <ITraktDevice> response = await client.Authentication.GenerateDeviceAsync(TestConstants.TRAKT_CLIENT_ID);

            response.Should().NotBeNull();
            response.IsSuccess.Should().BeTrue();
            response.HasValue.Should().BeTrue();
            response.Value.Should().NotBeNull();

            ITraktDevice responseDevice = response.Value;

            responseDevice.Should().NotBeNull();
            responseDevice.DeviceCode.Should().Be(MockDevice.DeviceCode);
            responseDevice.UserCode.Should().Be(MockDevice.UserCode);
            responseDevice.VerificationUrl.Should().Be(MockDevice.VerificationUrl);
            responseDevice.ExpiresInSeconds.Should().Be(MockDevice.ExpiresInSeconds);
            responseDevice.IntervalInSeconds.Should().Be(MockDevice.IntervalInSeconds);
            responseDevice.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(3600));
            responseDevice.IsExpiredUnused.Should().BeFalse();
            responseDevice.IsValid.Should().BeTrue();

            ITraktDevice clientDevice = client.Authentication.Device;

            clientDevice.Should().NotBeNull();
            clientDevice.DeviceCode.Should().Be(responseDevice.DeviceCode);
            clientDevice.UserCode.Should().Be(responseDevice.UserCode);
            clientDevice.VerificationUrl.Should().Be(responseDevice.VerificationUrl);
            clientDevice.ExpiresInSeconds.Should().Be(responseDevice.ExpiresInSeconds);
            clientDevice.IntervalInSeconds.Should().Be(responseDevice.IntervalInSeconds);
            clientDevice.CreatedAt.Should().Be(responseDevice.CreatedAt);
            clientDevice.IsExpiredUnused.Should().BeFalse();
            clientDevice.IsValid.Should().BeTrue();
        }
        public async Task <TraktResponse <ITraktAuthorization> > PollForAuthorizationAsync(AuthorizationPollRequest request, CancellationToken cancellationToken = default)
        {
            try
            {
                request.Validate();

                ExtendedHttpRequestMessage requestMessage =
                    await _requestMessageBuilder.Reset(request)
                    .WithRequestBody(request.RequestBody)
                    .DisableAPIVersionHeader()
                    .DisableAPIClientIdHeader()
                    .Build().ConfigureAwait(false);

                HttpResponseMessage responseMessage;
                Stream         responseContentStream;
                HttpStatusCode responseCode;
                string         reasonPhrase        = string.Empty;
                uint           totalExpiredSeconds = 0;
                ITraktDevice   device = request.RequestBody.Device;
                IObjectJsonReader <ITraktAuthorization> objectJsonReader = JsonFactoryContainer.CreateObjectReader <ITraktAuthorization>();

                while (totalExpiredSeconds < device.ExpiresInSeconds)
                {
                    responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);

                    responseCode = responseMessage.StatusCode;
                    reasonPhrase = responseMessage.ReasonPhrase;

                    if (responseCode == HttpStatusCode.OK) // Success
                    {
                        responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false);

                        ITraktAuthorization traktAuthorization = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false);

                        var response = new TraktResponse <ITraktAuthorization>()
                        {
                            Value     = traktAuthorization,
                            HasValue  = traktAuthorization != null,
                            IsSuccess = traktAuthorization != null
                        };

                        if (responseMessage.Headers != null)
                        {
                            ResponseHeaderParser.ParseResponseHeaderValues(response, responseMessage.Headers);
                        }

                        _client.Authentication.Authorization = traktAuthorization;
                        return(response);
                    }
                    else if (responseCode == HttpStatusCode.BadRequest) // Pending
                    {
                        await Task.Delay((int)device.IntervalInMilliseconds).ConfigureAwait(false);

                        totalExpiredSeconds += device.IntervalInSeconds;
                        requestMessage       = await _requestMessageBuilder.Reset(request).WithRequestBody(request.RequestBody).Build().ConfigureAwait(false);

                        continue;
                    }

                    await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isInAuthorizationPolling : true, cancellationToken : cancellationToken).ConfigureAwait(false);

                    break;
                }

                throw new TraktAuthenticationDeviceException("unknown exception")
                      {
                          RequestUrl         = requestMessage.Url,
                          RequestBody        = requestMessage.RequestBodyJson,
                          ServerReasonPhrase = reasonPhrase
                      };
            }
            catch (Exception ex)
            {
                if (_client.Configuration.ThrowResponseExceptions)
                {
                    throw;
                }

                return(new TraktResponse <ITraktAuthorization> {
                    IsSuccess = false, Exception = ex
                });
            }
        }
예제 #8
0
 public Task <TraktResponse <ITraktAuthorization> > PollForAuthorizationAsync(ITraktDevice device, string clientId, CancellationToken cancellationToken = default)
 => PollForAuthorizationAsync(device, clientId, ClientSecret, cancellationToken);