public void GalleryItem_CanConvertGalleryImage_IsTrue() { var converter = new GalleryItemConverter(); var canConvert = converter.CanConvert(new GalleryImage().GetType()); Assert.IsTrue(canConvert); }
/// <summary> /// Parses the string response from the endpoint into an expected type T. /// </summary> /// <typeparam name="T">The expected output type, Image, bool, etc.</typeparam> /// <param name="endpointStringResponse">The string response from the endpoint.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="MashapeException"></exception> /// <exception cref="ImgurException"></exception> /// <returns></returns> internal virtual T ProcessEndpointResponse <T>(string endpointStringResponse) { //If no result is found, then we can't proceed if (string.IsNullOrWhiteSpace(endpointStringResponse)) { throw new ImgurException("The response from the endpoint is empty."); } //If the result isn't a json response, then we can't proceed if (endpointStringResponse.StartsWith("<")) { throw new ImgurException("The response from the endpoint is invalid."); } //If the authentication method is Mashape, then an error response //is different to that of Imgur's response. if (ApiClient is IMashapeClient && endpointStringResponse.Contains("{\"message\":")) { var maShapeError = JsonConvert.DeserializeObject <MashapeError>(endpointStringResponse); throw new MashapeException(maShapeError.Message); } //If an error occurs, throw an exception if (endpointStringResponse.Contains("{\"data\":{\"error\":")) { var apiError = JsonConvert.DeserializeObject <Basic <ImgurError> >(endpointStringResponse); throw new ImgurException(apiError.Data.Error); } //If the type being requested is an oAuthToken //Deserialize it immediately and return if (typeof(T) == typeof(IOAuth2Token) || typeof(T) == typeof(OAuth2Token)) { var oAuth2Response = JsonConvert.DeserializeObject <T>(endpointStringResponse); return(oAuth2Response); } //Deserialize the response into a generic Basic<object> type var response = JsonConvert.DeserializeObject <Basic <object> >(endpointStringResponse); //If the response was not a success, then the response type is Basic<ImgurError> //and should be handled as such. if (!response.Success) { var apiError = JsonConvert.DeserializeObject <Basic <ImgurError> >(endpointStringResponse); throw new ImgurException(apiError.Data.Error); } var converters = new JsonConverter[1]; converters[0] = new GalleryItemConverter(); //Deserialize the actual response var finalResponse = JsonConvert.DeserializeObject <Basic <T> >(endpointStringResponse, converters); return(finalResponse.Data); }
public void GalleryItemConverter_ReadJsonGallery_Null() { var converter = new GalleryItemConverter(); var reader = new JsonTextReader(new StringReader("")); reader.Read(); var serializer = new JsonSerializer { Converters = { converter } }; var actual = (GalleryAlbum)converter.ReadJson(reader, typeof(GalleryItem), null, serializer); Assert.Null(actual); }
public void GalleryItemConverter_ReadJsonGalleryImage_AreEqual() { var converter = new GalleryItemConverter(); var reader = new JsonTextReader(new StringReader(AccountEndpointResponses.GetGalleryImage)); reader.Read(); var serializer = new JsonSerializer { Converters = { converter } }; var actual = (GalleryImage)converter.ReadJson(reader, typeof(IGalleryAlbumImageBase), null, serializer); Assert.IsNotNull(actual); Assert.AreEqual(null, actual.AccountId); Assert.AreEqual("YoSoyPablo1", actual.AccountUrl); Assert.AreEqual(false, actual.Animated); Assert.AreEqual(118267772080, actual.Bandwidth); Assert.AreEqual(null, actual.CommentCount); Assert.AreEqual(null, actual.CommentPreview); Assert.AreEqual(new DateTimeOffset(new DateTime(2015, 09, 19, 20, 29, 47, DateTimeKind.Utc)), actual.DateTime); Assert.AreEqual(null, actual.DeleteHash); Assert.AreEqual(null, actual.Description); Assert.AreEqual(225, actual.Downs); Assert.AreEqual(true, actual.Favorite); Assert.AreEqual(null, actual.Gifv); Assert.AreEqual(720, actual.Height); Assert.AreEqual("http://i.imgur.com/l35eOVB.jpg", actual.Link); Assert.AreEqual(false, actual.Looping); Assert.AreEqual(null, actual.Mp4); Assert.AreEqual(null, actual.Nsfw); Assert.AreEqual(null, actual.Score); Assert.AreEqual(null, actual.Section); Assert.AreEqual(111952, actual.Size); Assert.AreEqual("Someone is happy. New gadgets arrived to Ahmed from Microsoft", actual.Title); Assert.AreEqual(null, actual.TopicId); Assert.AreEqual(null, actual.Topic); Assert.AreEqual("image/jpeg", actual.Type); Assert.AreEqual(3567, actual.Ups); Assert.AreEqual(1056415, actual.Views); Assert.AreEqual(null, actual.Vote); Assert.AreEqual(null, actual.Webm); Assert.AreEqual(960, actual.Width); }
public void GalleryItemConverter_ReadJsonGalleryAlbum_AreEqual() { var converter = new GalleryItemConverter(); var reader = new JsonTextReader(new StringReader(AccountEndpointResponses.GetGalleryAlbum)); reader.Read(); var serializer = new JsonSerializer { Converters = { converter } }; var actual = (GalleryAlbum)converter.ReadJson(reader, typeof(IGalleryAlbumImageBase), null, serializer); Assert.IsNotNull(actual); Assert.AreEqual(null, actual.AccountId); Assert.AreEqual("SpaceCowboy02", actual.AccountUrl); Assert.AreEqual(null, actual.CommentCount); Assert.AreEqual(null, actual.CommentPreview); Assert.AreEqual("JsKDPBN", actual.Cover); Assert.AreEqual(240, actual.CoverHeight); Assert.AreEqual(500, actual.CoverWidth); Assert.AreEqual(new DateTimeOffset(new DateTime(2015, 09, 19, 16, 43, 54, DateTimeKind.Utc)), actual.DateTime); Assert.AreEqual(null, actual.Description); Assert.AreEqual(119, actual.Downs); Assert.AreEqual(true, actual.Favorite); Assert.AreEqual("LqLmI", actual.Id); Assert.AreEqual(9, actual.ImagesCount); Assert.AreEqual(0, actual.Images.Count()); Assert.AreEqual(null, actual.Layout); Assert.AreEqual("http://imgur.com/a/LqLmI", actual.Link); Assert.AreEqual(null, actual.Nsfw); Assert.AreEqual(null, actual.Privacy); Assert.AreEqual(null, actual.Score); Assert.AreEqual("Game of Expectation and Reality", actual.Title); Assert.AreEqual(null, actual.Topic); Assert.AreEqual(null, actual.TopicId); Assert.AreEqual(4040, actual.Ups); Assert.AreEqual(96803, actual.Views); Assert.AreEqual(null, actual.Vote); }
/// <summary> /// Parses the string stringResponse from the endpoint into an expected type T. /// </summary> /// <typeparam name="T">The expected output type, Image, bool, etc.</typeparam> /// <param name="response">The response from the endpoint.</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="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <returns></returns> internal virtual T ProcessEndpointResponse <T>(HttpResponseMessage response) { //If no result is found, then we can't proceed if (response == null) { throw new ImgurException("The response from the endpoint is missing."); } var stringResponse = string.Empty; if (response.Content != null) { stringResponse = response.Content.ReadAsStringAsync().GetAwaiter().GetResult().Trim(); } //If no result is found, then we can't proceed if (string.IsNullOrWhiteSpace(stringResponse)) { throw new ImgurException($"The response from the endpoint is missing. {(int)response.StatusCode} {response.ReasonPhrase}"); } //If the result isn't a json response, then we can't proceed if (stringResponse.StartsWith("<")) { throw new ImgurException($"The response from the endpoint is invalid. {(int)response.StatusCode} {response.ReasonPhrase}"); } //If the authentication method is Mashape, then an error response //is different to that of Imgur's error response. if (ApiClient is IMashapeClient && stringResponse.StartsWith("{\"message\":")) { var maShapeError = JsonConvert.DeserializeObject <MashapeError>(stringResponse); throw new MashapeException(maShapeError.Message); } //If an error occurs, throw an exception if (stringResponse.StartsWith("{\"data\":{\"error\":")) { dynamic jsonResponse = JObject.Parse(stringResponse); if (stringResponse.Contains("{\"message\":")) { throw new ImgurException(jsonResponse.data.error.message.ToString()); } else { throw new ImgurException(jsonResponse.data.error.ToString()); } } //If an error occurs, throw an exception if (stringResponse.StartsWith("{\"data\":\"An error occurred")) { dynamic jsonResponse = JObject.Parse(stringResponse); throw new ImgurException(jsonResponse.data.ToString()); } //If the type being requested is an OAuth2Token //Deserialize it immediately and return if (typeof(T) == typeof(IOAuth2Token) || typeof(T) == typeof(OAuth2Token)) { var oAuth2Response = JsonConvert.DeserializeObject <T>(stringResponse); return(oAuth2Response); } //Deserialize the response into a generic Basic<object> type var basicResponse = JsonConvert.DeserializeObject <Basic <object> >(stringResponse); //If the request was not a success, then the basicResponse type is Basic<ImgurError> //and should be handled as such. if (!basicResponse.Success) { var apiError = JsonConvert.DeserializeObject <Basic <ImgurError> >(stringResponse); throw new ImgurException(apiError.Data.Error); } var converters = new JsonConverter[1]; converters[0] = new GalleryItemConverter(); //Deserialize the response to the correct Type var finalResponse = JsonConvert.DeserializeObject <Basic <T> >(stringResponse, converters); return(finalResponse.Data); }
public void CanConvert(Type type, bool canConvert) { var converter = new GalleryItemConverter(); Assert.Equal(converter.CanConvert(type), canConvert); }