From 70845edb98302582531dec702045d7c5ddd9c551 Mon Sep 17 00:00:00 2001 From: Sylveon Date: Sat, 7 May 2022 03:00:00 -0400 Subject: [PATCH] All url based strings are now of type Uri, tests have been updated accordingly --- NHentaiAPI.Tests/NHentaiPictureUnitTest.cs | 2 +- NHentaiAPI/NHentaiClient.cs | 81 ++++++++++------------ 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs b/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs index cd37553..b36e379 100644 --- a/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs +++ b/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs @@ -45,7 +45,7 @@ public async Task TestGetGifPictureResult() // Check url var imageUrl = NHentaiClient.GetPictureUrl(book, 22); - Assert.AreEqual(imageUrl, "https://i.nhentai.net/galleries/1504878/22.gif"); + Assert.AreEqual(imageUrl.AbsoluteUri, "https://i.nhentai.net/galleries/1504878/22.gif"); // Make sure image is downloaded var result = await NHentaiClient.GetPictureAsync(book, 22); diff --git a/NHentaiAPI/NHentaiClient.cs b/NHentaiAPI/NHentaiClient.cs index e8121aa..70239bd 100644 --- a/NHentaiAPI/NHentaiClient.cs +++ b/NHentaiAPI/NHentaiClient.cs @@ -33,76 +33,71 @@ public class NHentaiClient : IDisposable #region Data urls - private static string GetHomePageUrl(int pageNum) - => $"{ApiRootUrl}/api/galleries/all?page={pageNum}"; + private static Uri GetHomePageUrl(int pageNum) + => new Uri($"{ApiRootUrl}/api/galleries/all?page={pageNum}"); - private static string GetSearchUrl(string content, int pageNum) - => $"{ApiRootUrl}/api/galleries/search?" + - $"query={content.Replace(" ", "+")}&" + - $"page={pageNum}"; + private static Uri GetSearchUrl(string content, int pageNum) + => new Uri($"{ApiRootUrl}/api/galleries/search?query={content.Replace(" ", "+")}&page={pageNum}"); - private static string GetTagUrl(Tag tag, bool isPopularList, int pageNum) - => $"{ApiRootUrl}/api/galleries/tagged?" + - $"tag_id={tag.Id}" + - $"&page={pageNum}" + - (isPopularList ? "&sort=popular" : ""); + private static Uri GetTagUrl(Tag tag, bool isPopularList, int pageNum) + => new Uri($"{ApiRootUrl}/api/galleries/tagged?tag_id={tag.Id}&page={pageNum}{(isPopularList ? "&sort=popular" : "")}"); - private static string GetBookDetailsUrl(int bookId) - => $"{ApiRootUrl}/api/gallery/{bookId}"; + private static Uri GetBookDetailsUrl(int bookId) + => new Uri($"{ApiRootUrl}/api/gallery/{bookId}"); - private static string GetBookRecommendUrl(int bookId) - => $"{ApiRootUrl}/api/gallery/{bookId}/related"; + private static Uri GetBookRecommendUrl(int bookId) + => new Uri($"{ApiRootUrl}/api/gallery/{bookId}/related"); - private static string GetGalleryUrl(int galleryId) - => $"{ImageRootUrl}/galleries/{galleryId}"; + private static Uri GetGalleryUrl(int galleryId) + => new Uri($"{ImageRootUrl}/galleries/{galleryId}"); - private static string GetThumbGalleryUrl(int galleryId) - => $"{ThumbnailRootUrl}/galleries/{galleryId}"; + private static Uri GetThumbGalleryUrl(int galleryId) + => new Uri($"{ThumbnailRootUrl}/galleries/{galleryId}"); #endregion #region Picture urls - public string GetPictureUrl(Book book, int pageNum) + public static Uri GetPictureUrl(Book book, int pageNum) { var image = GetImage(book, pageNum-1); var fileType = ConvertType(image.Type); return GetPictureUrl(book.MediaId, pageNum, fileType); } - public string GetThumbPictureUrl(Book book, int pageNum) + public static Uri GetThumbPictureUrl(Book book, int pageNum) { var image = GetImage(book, pageNum); var fileType = ConvertType(image.Type); return GetThumbPictureUrl(book.MediaId, pageNum, fileType); } - public string GetBigCoverUrl(Book book) + public Uri GetBigCoverUrl(Book book) => GetBigCoverUrl(book.MediaId); - public string GetOriginPictureUrl(Book book, int pageNum) + public Uri GetOriginPictureUrl(Book book, int pageNum) => GetOriginPictureUrl(book.MediaId, pageNum); - public string GetBookThumbUrl(Book book) + public static Uri GetBookThumbUrl(Book book) { var fileType = ConvertType(book.Images.Cover.Type); return GetBookThumbUrl(book.MediaId, fileType); } - private static string GetPictureUrl(int galleryId, int pageNum, string fileType) - => $"{GetGalleryUrl(galleryId)}/{pageNum}.{fileType}"; + private static Uri GetPictureUrl(int galleryId, int pageNum, string fileType) + => new Uri($"{GetGalleryUrl(galleryId)}/{pageNum}.{fileType}"); - private static string GetThumbPictureUrl(int galleryId, int pageNum, string fileType) - => $"{GetThumbGalleryUrl(galleryId)}/{pageNum}t.{fileType}"; + private static Uri GetThumbPictureUrl(int galleryId, int pageNum, string fileType) + => new Uri($"{GetThumbGalleryUrl(galleryId)}/{pageNum}t.{fileType}"); - private static string GetBigCoverUrl(int galleryId) - => $"{GetThumbGalleryUrl(galleryId)}/cover.jpg"; + private static Uri GetBigCoverUrl(int galleryId) + => new Uri($"{GetThumbGalleryUrl(galleryId)}/cover.jpg"); - private static string GetOriginPictureUrl(int galleryId, int pageNum) + private static Uri GetOriginPictureUrl(int galleryId, int pageNum) => GetPictureUrl(galleryId, pageNum, "jpg"); - private static string GetBookThumbUrl(int galleryId, string fileType = "jpg") - => $"{GetThumbGalleryUrl(galleryId)}/thumb.{fileType ?? "jpg"}"; + private static Uri GetBookThumbUrl(int galleryId, string fileType = "jpg") + => new Uri($"{GetThumbGalleryUrl(galleryId)}/thumb.{fileType ?? "jpg"}"); #endregion @@ -151,19 +146,19 @@ private static string ConvertType(ImageType type) public Task GetHomePageListAsync(int pageNum) { var url = GetHomePageUrl(pageNum); - return GetData(url); + return GetData(url.AbsoluteUri); } public Task GetSearchPageListAsync(string keyword, int pageNum) { var url = GetSearchUrl(keyword, pageNum); - return GetData(url); + return GetData(url.AbsoluteUri); } public Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum) { var url = GetTagUrl(tag, sortBy == SortBy.Popular, pageNum); - return GetData(url); + return GetData(url.AbsoluteUri); } #endregion @@ -173,13 +168,13 @@ public Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageN public async Task GetBookAsync(int bookId) { var url = GetBookDetailsUrl(bookId); - return await GetData(url); + return await GetData(url.AbsoluteUri); } public async Task GetBookRecommendAsync(int bookId) { var url = GetBookRecommendUrl(bookId); - var book = await GetData(url); + var book = await GetData(url.AbsoluteUri); return new BookRecommend { Result = new List { book } @@ -193,31 +188,31 @@ public async Task GetBookRecommendAsync(int bookId) public Task GetPictureAsync(Book book, int pageNum) { var url = GetPictureUrl(book, pageNum); - return GetByteData(url); + return GetByteData(url.AbsoluteUri); } public Task GetThumbPictureAsync(Book book, int pageNum) { var url = GetThumbPictureUrl(book, pageNum); - return GetByteData(url); + return GetByteData(url.AbsoluteUri); } public Task GetBigCoverPictureAsync(Book book) { var url = GetBigCoverUrl(book.MediaId); - return GetByteData(url); + return GetByteData(url.AbsoluteUri); } public Task GetOriginPictureAsync(Book book, int pageNum) { var url = GetOriginPictureUrl(book.MediaId, pageNum); - return GetByteData(url); + return GetByteData(url.AbsoluteUri); } public Task GetBookThumbPictureAsync(Book book) { var url = GetBookThumbUrl(book); - return GetByteData(url); + return GetByteData(url.AbsoluteUri); } #endregion