Skip to content

Commit

Permalink
Finished meme and subreddit Gallery endpoint methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienDennehy committed Dec 31, 2015
1 parent 964b90c commit bd5f43e
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 20 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Damien Dennehy
Copyright (c) 2015-2016 Damien Dennehy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/Imgur.API/Endpoints/IGalleryEndpoint.cs
Expand Up @@ -151,7 +151,7 @@ public interface IGalleryEndpoint
/// <param name="window">The time period that should be used in filtering requests. Default: Week</param>
/// <param name="page">The data paging number. Default: null</param>
/// <returns></returns>
Task<IEnumerable<IGalleryItem>> GetSubredditGalleriesAsync(string subreddit,
Task<IEnumerable<IGalleryItem>> GetSubredditGalleryAsync(string subreddit,
SubredditGallerySortOrder? sort = SubredditGallerySortOrder.Time, TimeWindow? window = TimeWindow.Week,
int? page = null);

Expand Down
47 changes: 41 additions & 6 deletions src/Imgur.API/Endpoints/Impl/GalleryEndpoint.Memes.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Imgur.API.Enums;
using Imgur.API.Models;
using Imgur.API.Models.Impl;

namespace Imgur.API.Endpoints.Impl
{
Expand All @@ -12,24 +14,57 @@ public partial class GalleryEndpoint
/// View images for memes subgallery.
/// </summary>
/// <param name="sort">The order that the gallery should be sorted by. Default: Viral</param>
/// <param name="window">The time period that should be used in filtering requests. Default: Day</param>
/// <param name="window">The time period that should be used in filtering requests. Default: Week</param>
/// <param name="page">The data paging number. Default: null</param>
/// <exception cref="ArgumentNullException">
/// Thrown when a null reference is passed to a method that does not accept it as a
/// valid argument.
/// </exception>
/// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
/// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
/// <returns></returns>
public async Task<IEnumerable<IGalleryItem>> GetMemesSubGalleryAsync(MemesGallerySortOrder? sort,
TimeWindow? window,
int? page = null)
public async Task<IEnumerable<IGalleryItem>> GetMemesSubGalleryAsync(
MemesGallerySortOrder? sort = MemesGallerySortOrder.Viral,
TimeWindow? window = TimeWindow.Week, int? page = null)
{
throw new NotImplementedException();
sort = sort ?? MemesGallerySortOrder.Viral;
window = window ?? TimeWindow.Week;

var sortValue = $"{sort}".ToLower();
var windowValue = $"{window}".ToLower();

var url = $"g/memes/{sortValue}/{windowValue}/{page}";

using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url))
{
var gallery = await SendRequestAsync<IEnumerable<GalleryItem>>(request);
return gallery;
}
}

/// <summary>
/// View a single image in the memes gallery.
/// </summary>
/// <param name="imageId">The image id.</param>
/// <exception cref="ArgumentNullException">
/// Thrown when a null reference is passed to a method that does not accept it as a
/// valid argument.
/// </exception>
/// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
/// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
/// <returns></returns>
public async Task<IGalleryImage> GetMemesSubGalleryImageAsync(string imageId)
{
throw new NotImplementedException();
if (string.IsNullOrWhiteSpace(imageId))
throw new ArgumentNullException(nameof(imageId));

var url = $"gallery/image/{imageId}";

using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url))
{
var image = await SendRequestAsync<GalleryImage>(request);
return image;
}
}
}
}
50 changes: 46 additions & 4 deletions src/Imgur.API/Endpoints/Impl/GalleryEndpoint.Subreddits.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Imgur.API.Enums;
using Imgur.API.Models;
using Imgur.API.Models.Impl;

namespace Imgur.API.Endpoints.Impl
{
Expand All @@ -15,22 +17,62 @@ public partial class GalleryEndpoint
/// <param name="sort">The order that the gallery should be sorted by. Default: Time</param>
/// <param name="window">The time period that should be used in filtering requests. Default: Week</param>
/// <param name="page">The data paging number. Default: null</param>
/// <exception cref="ArgumentNullException">
/// Thrown when a null reference is passed to a method that does not accept it as a
/// valid argument.
/// </exception>
/// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
/// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
/// <returns></returns>
public async Task<IEnumerable<IGalleryItem>> GetSubredditGalleriesAsync(string subreddit,
SubredditGallerySortOrder? sort, TimeWindow? window, int? page = null)
public async Task<IEnumerable<IGalleryItem>> GetSubredditGalleryAsync(string subreddit,
SubredditGallerySortOrder? sort = SubredditGallerySortOrder.Time, TimeWindow? window = TimeWindow.Week,
int? page = null)
{
throw new NotImplementedException();
if (string.IsNullOrWhiteSpace(subreddit))
throw new ArgumentNullException(nameof(subreddit));

sort = sort ?? SubredditGallerySortOrder.Time;
window = window ?? TimeWindow.Week;

var sortValue = $"{sort}".ToLower();
var windowValue = $"{window}".ToLower();

var url = $"gallery/r/{subreddit}/{sortValue}/{windowValue}/{page}";

using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url))
{
var gallery = await SendRequestAsync<IEnumerable<GalleryItem>>(request);
return gallery;
}
}

/// <summary>
/// View a single image in the subreddit.
/// </summary>
/// <param name="imageId">The image id.</param>
/// <param name="subreddit">A valid subreddit name. Example: pics, gaming</param>
/// <exception cref="ArgumentNullException">
/// Thrown when a null reference is passed to a method that does not accept it as a
/// valid argument.
/// </exception>
/// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
/// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
/// <returns></returns>
public async Task<IGalleryImage> GetSubredditImageAsync(string imageId, string subreddit)
{
throw new NotImplementedException();
if (string.IsNullOrWhiteSpace(imageId))
throw new ArgumentNullException(nameof(imageId));

if (string.IsNullOrWhiteSpace(subreddit))
throw new ArgumentNullException(nameof(subreddit));

var url = $"gallery/r/{subreddit}/{imageId}";

using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url))
{
var image = await SendRequestAsync<GalleryImage>(request);
return image;
}
}
}
}
Expand Up @@ -57,7 +57,7 @@ public async Task GetImageAsync_IsNotNull()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
[ExpectedException(typeof (ArgumentNullException))]
public async Task GetImageAsync_WithIdNull_ThrowsArgumentNullException()
{
var client = new ImgurClient("123", "1234");
Expand Down
103 changes: 103 additions & 0 deletions tests/Imgur.API.Tests/Endpoints/GalleryEndpointTests.Memes.cs
@@ -0,0 +1,103 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Imgur.API.Authentication.Impl;
using Imgur.API.Endpoints.Impl;
using Imgur.API.Enums;
using Imgur.API.Tests.FakeResponses;
using Imgur.API.Tests.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Imgur.API.Tests.Endpoints
{
public partial class GalleryEndpointTests
{
[TestMethod]
public async Task GetGalleryAsync_WithTopYearPage7_Any()
{
var fakeUrl = "https://api.imgur.com/3/g/memes/top/year/7";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetMemesSubGalleryAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client,
new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var gallery = await endpoint.GetMemesSubGalleryAsync(MemesGallerySortOrder.Top, TimeWindow.Year, 7);

Assert.IsTrue(gallery.Any());
}

[TestMethod]
public async Task GetMemesSubGalleryAsync_DefaultParameters_Any()
{
var fakeUrl = "https://api.imgur.com/3/g/memes/viral/week/";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetMemesSubGalleryAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client,
new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var gallery = await endpoint.GetMemesSubGalleryAsync();

Assert.IsTrue(gallery.Any());
}

[TestMethod]
public async Task GetMemesSubGalleryImageAsync_IsNotNull()
{
var fakeUrl = "https://api.imgur.com/3/gallery/image/rNdMhHm";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetGalleryImageAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client, new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var image = await endpoint.GetMemesSubGalleryImageAsync("rNdMhHm");

Assert.IsNotNull(image);
Assert.AreEqual("rNdMhHm", image.Id);
Assert.AreEqual("wanna make money quickly? follow the instruction below", image.Title);
Assert.AreEqual("i am not lying", image.Description);
Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1451524552), image.DateTime);
Assert.AreEqual("image/png", image.Type);
Assert.AreEqual(false, image.Animated);
Assert.AreEqual(610, image.Width);
Assert.AreEqual(558, image.Height);
Assert.AreEqual(564133, image.Size);
Assert.AreEqual(410285, image.Views);
Assert.AreEqual(231455307905, image.Bandwidth);
Assert.AreEqual(null, image.Vote);
Assert.AreEqual(false, image.Favorite);
Assert.AreEqual(false, image.Nsfw);
Assert.AreEqual("pics", image.Section);
Assert.AreEqual("Calasin", image.AccountUrl);
Assert.AreEqual(22349254, image.AccountId);
Assert.AreEqual(352, image.CommentCount);
Assert.AreEqual(10, image.CommentPreview.Count());
Assert.AreEqual("No Topic", image.Topic);
Assert.AreEqual(29, image.TopicId);
Assert.AreEqual("http://i.imgur.com/rNdMhHm.png", image.Link);
Assert.AreEqual(352, image.CommentCount);
Assert.AreEqual(15226, image.Ups);
Assert.AreEqual(2339, image.Downs);
Assert.AreEqual(12887, image.Points);
Assert.AreEqual(13092, image.Score);
}

[TestMethod]
[ExpectedException(typeof (ArgumentNullException))]
public async Task GetMemesSubGalleryImageAsync_WithIdNull_ThrowsArgumentNullException()
{
var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client);
await endpoint.GetMemesSubGalleryImageAsync(null);
}
}
}
95 changes: 95 additions & 0 deletions tests/Imgur.API.Tests/Endpoints/GalleryEndpointTests.Subreddits.cs
@@ -0,0 +1,95 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Imgur.API.Authentication.Impl;
using Imgur.API.Endpoints.Impl;
using Imgur.API.Enums;
using Imgur.API.Tests.FakeResponses;
using Imgur.API.Tests.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Imgur.API.Tests.Endpoints
{
public partial class GalleryEndpointTests
{
[TestMethod]
public async Task GetSubredditGalleryAsync_DefaultParameters_Any()
{
var fakeUrl = "https://api.imgur.com/3/gallery/r/pics/time/week/";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetSubredditGalleryAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client,
new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var gallery = await endpoint.GetSubredditGalleryAsync("pics");

Assert.IsTrue(gallery.Any());
}

[TestMethod]
[ExpectedException(typeof (ArgumentNullException))]
public async Task GetSubredditGalleryAsync_WithSubRedditNull_ThrowsArgumentNullException()
{
var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client);
await endpoint.GetSubredditGalleryAsync(null);
}

[TestMethod]
public async Task GetSubredditGalleryAsync_WithTopYearPage7_Any()
{
var fakeUrl = "https://api.imgur.com/3/gallery/r/pics/time/week/7";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetMemesSubGalleryAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client,
new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var gallery =
await endpoint.GetSubredditGalleryAsync("pics", SubredditGallerySortOrder.Time, TimeWindow.Week, 7);

Assert.IsTrue(gallery.Any());
}

[TestMethod]
public async Task GetSubredditImageAsync_IsNotNull()
{
var fakeUrl = "https://api.imgur.com/3/gallery/r/pics/xyP";
var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GalleryEndpointResponses.GetSubredditImageAsync)
};

var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client, new HttpClient(new FakeHttpMessageHandler(fakeUrl, fakeResponse)));
var image = await endpoint.GetSubredditImageAsync("xyP", "pics");

Assert.IsNotNull(image);
}

[TestMethod]
[ExpectedException(typeof (ArgumentNullException))]
public async Task GetSubredditImageAsync_WithIdNull_ThrowsArgumentNullException()
{
var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client);
await endpoint.GetSubredditImageAsync(null, "test");
}

[TestMethod]
[ExpectedException(typeof (ArgumentNullException))]
public async Task GetSubredditImageAsync_WithSubRedditNull_ThrowsArgumentNullException()
{
var client = new ImgurClient("123", "1234");
var endpoint = new GalleryEndpoint(client);
await endpoint.GetSubredditImageAsync("hhjkh", null);
}
}
}

0 comments on commit bd5f43e

Please sign in to comment.