Пример #1
0
        private void SaveTraktAuthorization(ITraktAuthorization authorization, string path)
        {
            string serializedAuthorization = TraktSerializationService.SerializeAsync(authorization).Result;
            string authorizationFilePath   = Path.Combine(path, FileName.Authorization.Value);

            _fileOperations.FileWriteAllText(authorizationFilePath, serializedAuthorization, Encoding.UTF8);
        }
Пример #2
0
        private void SaveLastSyncActivities(ITraktSyncLastActivities syncLastActivities)
        {
            string lastSyncActivitiesPath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.LastActivity.Value);
            string lastSyncActivitiesJson = TraktSerializationService.SerializeAsync(syncLastActivities).Result;

            _fileOperations.FileWriteAllText(lastSyncActivitiesPath, lastSyncActivitiesJson, Encoding.UTF8);
        }
Пример #3
0
        private void SaveLastSyncActivities(ITraktSyncLastActivities traktSyncLastActivities, string path)
        {
            string serializedSyncActivities = TraktSerializationService.SerializeAsync(traktSyncLastActivities).Result;
            string syncActivitiesFilePath   = Path.Combine(path, FileName.LastActivity.Value);

            _fileOperations.FileWriteAllText(syncActivitiesFilePath, serializedSyncActivities, Encoding.UTF8);
        }
        public void TestTraktSerializationServiceSerializeTraktDevice()
        {
            var jsonDevice = TraktSerializationService.Serialize(DEVICE);

            jsonDevice.Should().NotBeNullOrEmpty();
            jsonDevice.Should().Be(DEVICE_JSON);
        }
        public void TestTraktSerializationServiceSerializeTraktAuthorization()
        {
            var jsonAuthorization = TraktSerializationService.Serialize(AUTHORIZATION);

            jsonAuthorization.Should().NotBeNullOrEmpty();
            jsonAuthorization.Should().Be(AUTHORIZATION_JSON);
        }
Пример #6
0
        private void SaveTraktUserSettings(ITraktUserSettings traktUserSettings, string path)
        {
            string serializedSettings = TraktSerializationService.SerializeAsync(traktUserSettings).Result;
            string settingsFilePath   = Path.Combine(path, FileName.UserSettings.Value);

            _fileOperations.FileWriteAllText(settingsFilePath, serializedSettings, Encoding.UTF8);
        }
        public async Task Test_TraktSerializationService_SerializeAsync_ITraktAuthorization_8()
        {
            string json = await TraktSerializationService.SerializeAsync(Authorization8);

            json.Should().NotBeNullOrEmpty();
            json.Should().Be(Authorization8Json);
        }
Пример #8
0
        public void EnterModelContext(NavigationContext oldContext, NavigationContext newContext)
        {
            // clear the PIN code text box, necessary when entering the plugin
            PinCode = string.Empty;

            string authFilePath        = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.Authorization.Value);
            bool   savedAuthFileExists = _fileOperations.FileExists(authFilePath);

            if (!savedAuthFileExists)
            {
                TestStatus       = "[Trakt.NotAuthorized]";
                IsUserAuthorized = false;
            }
            else
            {
                string savedAuthorization         = _fileOperations.FileReadAllText(authFilePath);
                ITraktAuthorization savedAuthFile = TraktSerializationService.DeserializeAsync <ITraktAuthorization>(savedAuthorization).Result;
                if (savedAuthFile.IsRefreshPossible)
                {
                    TestStatus       = "[Trakt.AlreadyAuthorized]";
                    IsUserAuthorized = true;
                }
                else
                {
                    TestStatus       = "[Trakt.SavedAuthIsNotValid]";
                    IsUserAuthorized = false;
                }
            }
        }
        public async Task Test_TraktSerializationService_DeserializeAsync_ITraktAuthorization_ArgumentExceptions()
        {
            Func <Task <ITraktAuthorization> > act = () => TraktSerializationService.DeserializeAsync(null);
            await act.Should().ThrowAsync <ArgumentNullException>();

            act = () => TraktSerializationService.DeserializeAsync(string.Empty);
            await act.Should().ThrowAsync <ArgumentException>();
        }
        static void Main(string[] args)
        {
            TraktClient client = new TraktClient(CLIENT_ID, CLIENT_SECRET);

            TraktAuthorization fakeAuthorization = new TraktAuthorization
            {
                AccessToken      = "FakeAccessToken",
                RefreshToken     = "FakeRefreshToken",
                ExpiresInSeconds = 90 * 24 * 3600,
                AccessScope      = TraktAccessScope.Public,
                TokenType        = TraktAccessTokenType.Bearer
            };

            Console.WriteLine("Fake Authorization:");
            Console.WriteLine($"Created (UTC): {fakeAuthorization.Created}");
            Console.WriteLine($"Access Scope: {fakeAuthorization.AccessScope.DisplayName}");
            Console.WriteLine($"Refresh Possible: {fakeAuthorization.IsRefreshPossible}");
            Console.WriteLine($"Valid: {fakeAuthorization.IsValid}");
            Console.WriteLine($"Token Type: {fakeAuthorization.TokenType.DisplayName}");
            Console.WriteLine($"Access Token: {fakeAuthorization.AccessToken}");
            Console.WriteLine($"Refresh Token: {fakeAuthorization.RefreshToken}");
            Console.WriteLine($"Token Expired: {fakeAuthorization.IsExpired}");
            Console.WriteLine($"Expires in {fakeAuthorization.ExpiresInSeconds / 3600 / 24} days");

            Console.WriteLine("-------------------------------------------------------------");

            //string fakeAuthorizationJson = TraktSerializationService.Serialize(client.Authorization);
            string fakeAuthorizationJson = TraktSerializationService.Serialize(fakeAuthorization);

            if (!string.IsNullOrEmpty(fakeAuthorizationJson))
            {
                Console.WriteLine("Serialized Fake Authorization:");
                Console.WriteLine(fakeAuthorizationJson);

                Console.WriteLine("-------------------------------------------------------------");

                TraktAuthorization deserializedFakeAuthorization = TraktSerializationService.DeserializeAuthorization(fakeAuthorizationJson);

                if (deserializedFakeAuthorization != null)
                {
                    client.Authorization = deserializedFakeAuthorization;

                    Console.WriteLine("Deserialized Fake Authorization:");
                    Console.WriteLine($"Created (UTC): {deserializedFakeAuthorization.Created}");
                    Console.WriteLine($"Access Scope: {deserializedFakeAuthorization.AccessScope.DisplayName}");
                    Console.WriteLine($"Refresh Possible: {deserializedFakeAuthorization.IsRefreshPossible}");
                    Console.WriteLine($"Valid: {deserializedFakeAuthorization.IsValid}");
                    Console.WriteLine($"Token Type: {deserializedFakeAuthorization.TokenType.DisplayName}");
                    Console.WriteLine($"Access Token: {deserializedFakeAuthorization.AccessToken}");
                    Console.WriteLine($"Refresh Token: {deserializedFakeAuthorization.RefreshToken}");
                    Console.WriteLine($"Token Expired: {deserializedFakeAuthorization.IsExpired}");
                    Console.WriteLine($"Expires in {deserializedFakeAuthorization.ExpiresInSeconds / 3600 / 24} days");
                }
            }

            Console.ReadLine();
        }
        public void TestTraktSerializationServiceDeserializeTraktDeviceArgumentExceptions()
        {
            Action act = () => TraktSerializationService.DeserializeDevice(null);

            act.ShouldThrow <ArgumentException>();

            act = () => TraktSerializationService.DeserializeDevice(string.Empty);
            act.ShouldThrow <ArgumentException>();
        }
        public void TestTraktSerializationServiceDeserializeTraktDeviceInvalidJson()
        {
            Action act = () => TraktSerializationService.DeserializeDevice("{ \"invalid\": \"json\" }");

            act.ShouldNotThrow();

            var result = TraktSerializationService.DeserializeDevice("{ \"invalid\": \"json\" }");

            result.Should().BeNull();
        }
Пример #13
0
        private IList <MovieCollected> GetCollectedMoviesFromOnlineAndSaveItToCache()
        {
            IEnumerable <ITraktCollectionMovie> collectedMovies = _traktClient.GetCollectedMovies();
            string collectedMoviesPath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.CollectedMovies.Value);
            IList <ITraktCollectionMovie> traktCollectionMovies = collectedMovies.ToList();
            string collectedMoviesJson = TraktSerializationService.SerializeCollectionAsync(traktCollectionMovies).Result;

            _fileOperations.FileWriteAllText(collectedMoviesPath, collectedMoviesJson, Encoding.UTF8);

            return(ConvertCollectionMoviesToMovieCollections(traktCollectionMovies));
        }
Пример #14
0
        private IList <EpisodeWatched> GetWatchedEpisodesFromOnlineAndSaveItToCache()
        {
            IEnumerable <ITraktWatchedShow> watchedShows = _traktClient.GetWatchedShows();
            string watchedEpisodesFilePath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.WatchedEpisodes.Value);
            IList <ITraktWatchedShow> traktWatchedShows = watchedShows.ToList();
            string watchedEpisodesJson = TraktSerializationService.SerializeCollectionAsync(traktWatchedShows).Result;

            _fileOperations.FileWriteAllText(watchedEpisodesFilePath, watchedEpisodesJson, Encoding.UTF8);

            return(ConvertWatchedShowsToWatchedEpisodes(traktWatchedShows));
        }
        public void TestTraktSerializationServiceDeserializeTraktDevice()
        {
            var device = TraktSerializationService.DeserializeDevice(DEVICE_JSON);

            device.Should().NotBeNull();
            device.UserCode.Should().Be(DEVICE.UserCode);
            device.DeviceCode.Should().Be(DEVICE.DeviceCode);
            device.VerificationUrl.Should().Be(DEVICE.VerificationUrl);
            device.ExpiresInSeconds.Should().Be(DEVICE.ExpiresInSeconds);
            device.IntervalInSeconds.Should().Be(DEVICE.IntervalInSeconds);
            device.Created.Should().Be(DEVICE.Created);
        }
        public async Task Test_TraktSerializationService_DeserializeAsync_ITraktAuthorization_1()
        {
            ITraktAuthorization authorization = await TraktSerializationService.DeserializeAsync(Authorization1Json);

            authorization.Should().NotBeNull();
            authorization.AccessToken.Should().Be(TestConstants.MOCK_ACCESS_TOKEN);
            authorization.RefreshToken.Should().BeNull();
            authorization.Scope.Should().Be(TraktAccessScope.Public);
            authorization.TokenType.Should().Be(TraktAccessTokenType.Bearer);
            authorization.ExpiresInSeconds.Should().Be(7776000U);
            authorization.IgnoreExpiration.Should().BeTrue();
            authorization.CreatedAtTimestamp.Should().Be(Authorization1.CreatedAtTimestamp);
        }
Пример #17
0
        private IList <MovieCollected> CachedCollectedMovies()
        {
            IEnumerable <ITraktCollectionMovie> collectedMovies = new List <ITraktCollectionMovie>();

            string collectedMoviesPath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.CollectedMovies.Value);

            if (_fileOperations.FileExists(collectedMoviesPath))
            {
                string collectedMoviesJson = _fileOperations.FileReadAllText(collectedMoviesPath);
                collectedMovies = TraktSerializationService.DeserializeCollectionAsync <ITraktCollectionMovie>(collectedMoviesJson).Result;
            }
            return(ConvertCollectionMoviesToMovieCollections(collectedMovies));
        }
        public async Task Test_TraktSerializationService_DeserializeAsync_ITraktAuthorization_8()
        {
            ITraktAuthorization authorization = await TraktSerializationService.DeserializeAsync(Authorization8Json);

            authorization.Should().NotBeNull();
            authorization.AccessToken.Should().Be(TestConstants.MOCK_ACCESS_TOKEN);
            authorization.RefreshToken.Should().Be(TestConstants.MOCK_REFRESH_TOKEN);
            authorization.Scope.Should().Be(TraktAccessScope.Public);
            authorization.TokenType.Should().Be(TraktAccessTokenType.Bearer);
            authorization.ExpiresInSeconds.Should().Be(EXPIRES_IN_SECONDS);
            authorization.IgnoreExpiration.Should().BeFalse();
            authorization.CreatedAtTimestamp.Should().Be(s_createdAtTimestamp);
        }
        public void TestTraktSerializationServiceDeserializeTraktAuthorization()
        {
            var authorization = TraktSerializationService.DeserializeAuthorization(AUTHORIZATION_JSON);

            authorization.Should().NotBeNull();
            authorization.AccessToken.Should().Be(AUTHORIZATION.AccessToken);
            authorization.RefreshToken.Should().Be(AUTHORIZATION.RefreshToken);
            authorization.ExpiresInSeconds.Should().Be(AUTHORIZATION.ExpiresInSeconds);
            authorization.AccessScope.Should().Be(AUTHORIZATION.AccessScope);
            authorization.TokenType.Should().Be(AUTHORIZATION.TokenType);
            authorization.Created.Should().Be(AUTHORIZATION.Created);
            authorization.IgnoreExpiration.Should().Be(AUTHORIZATION.IgnoreExpiration);
        }
Пример #20
0
        private IEnumerable <EpisodeWatched> GetWatchedEpisodesFromCache()
        {
            IEnumerable <ITraktWatchedShow> traktWatchedShows = new List <ITraktWatchedShow>();

            string watchedEpisodesPath = Path.Combine(_mediaPortalServices.GetTraktUserHomePath(), FileName.WatchedEpisodes.Value);

            if (_fileOperations.FileExists(watchedEpisodesPath))
            {
                string watchedEpisodesJson = _fileOperations.FileReadAllText(watchedEpisodesPath);
                traktWatchedShows = TraktSerializationService.DeserializeCollectionAsync <ITraktWatchedShow>(watchedEpisodesJson).Result;
            }

            return(ConvertWatchedShowsToWatchedEpisodes(traktWatchedShows));
        }
Пример #21
0
        private void ValidateAuthorization(ITraktClient _traktClient, IFileOperations _fileOperations)
        {
            if (!_traktClient.TraktAuthorization.IsValid)
            {
                string authFilePath           = Path.Combine(HomeUserPath, FileName.Authorization.Value);
                string savedAuthorization     = _fileOperations.FileReadAllText(authFilePath);
                ITraktAuthorization savedAuth = TraktSerializationService.DeserializeAsync <ITraktAuthorization>(savedAuthorization).Result;

                if (!savedAuth.IsRefreshPossible)
                {
                    throw new Exception("Saved authorization is not valid.");
                }

                ITraktAuthorization refreshedAuth = _traktClient.RefreshAuthorization(savedAuth.RefreshToken);
                string serializedAuth             = TraktSerializationService.SerializeAsync(refreshedAuth).Result;
                _fileOperations.FileWriteAllText(authFilePath, serializedAuth, Encoding.UTF8);
            }
        }
Пример #22
0
        private ITraktSyncLastActivities SavedLastSyncActivities()
        {
            string traktUserHomePath           = _mediaPortalServices.GetTraktUserHomePath();
            string savedSyncActivitiesFilePath = Path.Combine(traktUserHomePath, FileName.LastActivity.Value);

            if (!_fileOperations.FileExists(savedSyncActivitiesFilePath))
            {
                return(new TraktSyncLastActivities
                {
                    Movies = new TraktSyncMoviesLastActivities(),
                    Shows = new TraktSyncShowsLastActivities(),
                    Episodes = new TraktSyncEpisodesLastActivities()
                });
            }
            string savedSyncActivitiesJson = _fileOperations.FileReadAllText(savedSyncActivitiesFilePath);

            return(TraktSerializationService.DeserializeAsync <ITraktSyncLastActivities>(savedSyncActivitiesJson).Result);
        }
Пример #23
0
        public void TestTraktSerializationServiceDeserializeTraktAuthorizationInvalidJson()
        {
            Action act = () => TraktSerializationService.DeserializeAuthorization("{ \"invalid\": \"json\" }");

            act.ShouldNotThrow();

            var result = TraktSerializationService.DeserializeAuthorization("{ \"invalid\": \"json\" }");

            result.Should().BeNull();

            act = () => TraktSerializationService.DeserializeAuthorization("invalid\": \"json\" }");
            act.ShouldThrow <ArgumentException>();

            string invalidAuthorizationJson =
                "{" +
                $"\"AccessToken\":\"\"," +
                $"\"RefreshToken\":\"\"," +
                $"\"ExpiresIn\":\"stringvalue\"," +
                $"\"Scope\":\"public\"," +
                $"\"TokenType\":\"bearer\"," +
                $"\"CreatedAtTicks\":0," +
                $"\"IgnoreExpiration\":false" +
                "}";

            act = () => TraktSerializationService.DeserializeAuthorization(invalidAuthorizationJson);
            act.ShouldThrow <ArgumentException>();

            invalidAuthorizationJson =
                "{" +
                $"\"AccessToken\":\"\"," +
                $"\"RefreshToken\":\"\"," +
                $"\"ExpiresIn\":0," +
                $"\"Scope\":\"public\"," +
                $"\"TokenType\":\"bearer\"," +
                $"\"CreatedAtTicks\":\"stringvalue\"," +
                $"\"IgnoreExpiration\":false" +
                "}";

            act = () => TraktSerializationService.DeserializeAuthorization(invalidAuthorizationJson);
            act.ShouldThrow <ArgumentException>();
        }
        public void TestTraktSerializationServiceSerializeEmptyTraktDevice()
        {
            var emptyDevice = new TraktDevice();

            string emptyDeviceJson =
                "{" +
                $"\"UserCode\":\"\"," +
                $"\"DeviceCode\":\"\"," +
                $"\"VerificationUrl\":\"\"," +
                $"\"ExpiresInSeconds\":0," +
                $"\"IntervalInSeconds\":0," +
                $"\"CreatedAtTicks\":{emptyDevice.Created.Ticks}" +
                "}";

            Action act = () => TraktSerializationService.Serialize(emptyDevice);

            act.ShouldNotThrow();

            var jsonDevice = TraktSerializationService.Serialize(emptyDevice);

            jsonDevice.Should().NotBeNullOrEmpty();
            jsonDevice.Should().Be(emptyDeviceJson);
        }
        public void TestTraktSerializationServiceSerializeEmptyTraktAuthorization()
        {
            var emptyAuthorization = new TraktAuthorization();

            string emptyAuthorizationJson =
                "{" +
                $"\"AccessToken\":\"\"," +
                $"\"RefreshToken\":\"\"," +
                $"\"ExpiresIn\":0," +
                $"\"Scope\":\"public\"," +
                $"\"TokenType\":\"bearer\"," +
                $"\"CreatedAtTicks\":{emptyAuthorization.Created.Ticks}," +
                $"\"IgnoreExpiration\":false" +
                "}";

            Action act = () => TraktSerializationService.Serialize(emptyAuthorization);

            act.ShouldNotThrow();

            var jsonAuthorization = TraktSerializationService.Serialize(emptyAuthorization);

            jsonAuthorization.Should().NotBeNullOrEmpty();
            jsonAuthorization.Should().Be(emptyAuthorizationJson);
        }
        public void TestTraktSerializationServiceSerializeTraktAuthorizationArgumentExceptions()
        {
            Action act = () => TraktSerializationService.Serialize(default(TraktAuthorization));

            act.ShouldThrow <ArgumentNullException>();
        }
        public void Test_TraktSerializationService_SerializeAsync_ITraktAuthorization_ArgumentExceptions()
        {
            Func <Task <string> > act = () => TraktSerializationService.SerializeAsync(null);

            act.Should().Throw <ArgumentNullException>();
        }