Beispiel #1
0
        public static async Task <IAccount> GetAccountAsync()
        {
            var endpoint = new AccountEndpoint(Client);
            var account  = await endpoint.GetAccountAsync();

            return(account);
        }
Beispiel #2
0
        public async Task GetAccountAsync_WithUsername_AreEqual()
        {
            var client   = new MashapeClient(ClientId, ClientSecret, MashapeKey);
            var endpoint = new AccountEndpoint(client);

            var account = await endpoint.GetAccountAsync("sarah");

            Assert.AreEqual("sarah", account.Url.ToLower());
        }
        public async Task GetAccountAsync_WithUsername_AreEqual()
        {
            var client   = new MashapeClient(ClientId, ClientSecret, MashapeKey, OAuth2Token);
            var endpoint = new AccountEndpoint(client);

            var account = await endpoint.GetAccountAsync("sarah");

            Assert.IsTrue("sarah".Equals(account.Url, StringComparison.OrdinalIgnoreCase));
        }
        public async Task GetAccountAsync_WithDefaultUsername_AreEqual()
        {
            var client   = new ImgurClient(ClientId, ClientSecret, OAuth2Token);
            var endpoint = new AccountEndpoint(client);

            var account = await endpoint.GetAccountAsync();

            Assert.IsTrue(account.Url.Equals("ImgurAPIDotNet", StringComparison.OrdinalIgnoreCase));
        }
        public async Task GetAccountAsync_WithDefaultUsername_AreEqual()
        {
            var client   = new MashapeClient(ClientId, ClientSecret, MashapeKey, await GetOAuth2Token());
            var endpoint = new AccountEndpoint(client);

            var account = await endpoint.GetAccountAsync();

            Assert.AreEqual("ImgurAPIDotNet".ToLower(), account.Url.ToLower());
        }
Beispiel #6
0
        public async Task GetAccountAsync_WithDefaultUsernameAndOAuth2Null_ThrowsArgumentNullException()
        {
            var client   = new ImgurClient("123", "1234");
            var endpoint = new AccountEndpoint(client);

            var exception = await Record.ExceptionAsync(
                async() => await endpoint.GetAccountAsync().ConfigureAwait(false))
                            .ConfigureAwait(false);

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);
        }
Beispiel #7
0
        public async Task GetAccountAsync_Equal()
        {
            var mockUrl      = "https://api.imgur.com/3/account/bob";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockAccountEndpointResponses.GetAccount)
            };

            var client   = new ImgurClient("123", "1234");
            var endpoint = new AccountEndpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));
            var account  = await endpoint.GetAccountAsync("bob").ConfigureAwait(false);

            Assert.NotNull(account);
            Assert.Equal(12456, account.Id);
            Assert.Equal("Bob", account.Url);
            Assert.Equal(null, account.Bio);
            Assert.Equal(4343, account.Reputation);
            Assert.Equal(NotorietyLevel.Idolized, account.Notoriety);
            Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1229591601), account.Created);
        }
Beispiel #8
0
        public async Task GetAccountAsync_AreEqual()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse           = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(AccountEndpointResponses.GetAccountResponse)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/3/account/bob"), fakeResponse);

            var client   = new ImgurClient("123", "1234");
            var endpoint = new AccountEndpoint(client, new HttpClient(fakeHttpMessageHandler));
            var account  = await endpoint.GetAccountAsync("bob");

            Assert.IsNotNull(account);
            Assert.AreEqual(12456, account.Id);
            Assert.AreEqual("Bob", account.Url);
            Assert.AreEqual(null, account.Bio);
            Assert.AreEqual(4343, account.Reputation);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1229591601), account.Created);
        }
Beispiel #9
0
 public async Task GetAccountAsync_WithUsernameNull_ThrowsArgumentNullException()
 {
     var client   = new ImgurClient("123", "1234");
     var endpoint = new AccountEndpoint(client);
     await endpoint.GetAccountAsync(null);
 }
Beispiel #10
0
        public async Task Sync(object context)
        {
            await Task.Run(async() =>
            {
                // get account
                AccountEndpoint endpoint = new AccountEndpoint(await ImgurHelper.GetClient());
                ImgurUser = await endpoint.GetAccountAsync(Name);
                bool me   = (await ImgurHelper.GetToken()).AccountId == ImgurUser.Id.ToString();

                // get local albums
                Album[] local = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => Album.Get(it.FullName, "", null, true)).ToArray();

                // get remote albums
                IAlbum[] remote = (await endpoint.GetAlbumsAsync(Name)).ToArray();

                // (A) filter for albums with id only in local (not in remote)
                Album[] onlyLocal = local.Where(l => l.Id != null && !remote.Any(r => r.Id == l.Id)).ToArray();

                // (B) filter for albums only in remote (not in local)
                IAlbum[] onlyRemote = remote.Where(r => !local.Any(l => l.Id == r.Id)).ToArray();

                if (me)
                {
                    // download albums (B) where not in json
                    IAlbum[] download = onlyRemote.Where(r => !Albums.Any(a => a.Id == r.Id)).ToArray();
                    foreach (IAlbum album in download)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                    // delete albums (A) from local where in json
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        Album[] deleteLocal = onlyLocal.Where(l => Albums.Any(a => a.Id == l.Id && a.Synchronize)).ToArray();
                        foreach (Album album in deleteLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                Directory.Delete(album.Root, true);
                            }
                        }
                    }
                    // delete albums (B) from remote where in json
                    if (Properties.Settings.Default.DeleteRemoteFolder)
                    {
                        IAlbum[] deleteRemote = onlyRemote.Where(r => Albums.Any(a => a.Id == r.Id && a.Synchronize)).ToArray();
                        foreach (IAlbum album in deleteRemote)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteRemoteFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete Imgur album?", album.Title, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                await endpoint.DeleteAlbumAsync(album.Id, Name);
                            }
                        }
                    }
                    // create albums without id
                    Album[] upload = local.Where(l => l.Id == null && l.Synchronize).ToArray();
                    foreach (Album album in upload)
                    {
                        AlbumEndpoint ep = new AlbumEndpoint(await ImgurHelper.GetClient());
                        IAlbum rAlbum    = await ep.CreateAlbumAsync(album.Title);
                        album.ImgurAlbum = rAlbum;
                        await album.Sync(context);
                    }
                }
                else
                {
                    // remove albums (A)
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        foreach (Album album in onlyLocal.Where(it => it.Synchronize))
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                album.Remove();
                            }
                        }
                    }
                    // download albums (B)
                    foreach (IAlbum album in onlyRemote)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                }

                // udpate json
                AlbumRoots     = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => it.FullName).ToList();
                Album[] albums = AlbumRoots.Select(it => Album.Get(it, "")).ToArray();

                App.Current.Dispatcher.Invoke((Action) delegate
                {
                    Albums.Clear();
                    foreach (Album album in albums)
                    {
                        Albums.Add(album);
                    }
                });
                await Save();

                // synchronize album images
                foreach (Album album in albums)
                {
                    await album.Sync(context);
                }
            });
        }