Exemple #1
0
        /// <summary>
        /// The translate API draws on search, but uses the GIPHY special sauce
        /// to handle translating from one vocabulary to another. In this case,
        /// words and phrases to GIFs.
        /// </summary>
        /// <param name="parameter">Specifies search parameters/param>
        /// <returns>Root object</returns>
        public async Task <GiphySingle> Translate(TranslateParameter parameter)
        {
            if (parameter is null)
            {
                throw new NullReferenceException("Paramter cannot be null");
            }

            if (string.IsNullOrEmpty(parameter.Query))
            {
                throw new FormatException("Query paramter cannot be null or empty.");
            }

            if (parameter.Weirdness < 0 || parameter.Weirdness > 10)
            {
                throw new FormatException("Weirdness paramter must be a value between 0 - 10");
            }

            /* Finish exception checks */

            string url = $@"{ _translateUrl }?api_key={ _apiKey }&s={ parameter.Query }&weirdness={ parameter.Weirdness }";

            var response = await _httpHandler.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(response.ReasonPhrase);
            }

            return(await response.Content.ReadAsAsync <GiphySingle>());
        }
Exemple #2
0
        public async Task <GiphyIdResult> TranslateIntoSticker(TranslateParameter translateParameter)
        {
            if (string.IsNullOrEmpty(translateParameter.Phrase))
            {
                throw new FormatException("Must set phrase or term in order to translate.");
            }

            var nvc = new NameValueCollection();

            nvc.Add("api_key", _authKey);
            nvc.Add("s", translateParameter.Phrase);
            if (translateParameter.Rating != Rating.None)
            {
                nvc.Add("rating", translateParameter.Rating.ToFriendlyString());
            }
            if (!string.IsNullOrEmpty(translateParameter.Format))
            {
                nvc.Add("fmt", translateParameter.Format);
            }
            var result =
                await _webManager.GetData(new Uri($"{BaseUrl}{BaseSticker}/translate{UriExtensions.ToQueryString(nvc)}"));

            if (!result.IsSuccess)
            {
                throw new WebException($"Failed to get Sticker: {result.ResultJson}");
            }

            return(JsonConvert.DeserializeObject <GiphyIdResult>(result.ResultJson));
        }
Exemple #3
0
        public async Task Translate_ParameterIsNull_ThrowsNullReferenceException()
        {
            var giphy = new Giphy("test");

            TranslateParameter search = null;

            _ = await Assert.ThrowsAsync <NullReferenceException>(() => giphy.Translate(search));
        }
        public void Weirdness_DefaultValue_Is10()
        {
            var param = new TranslateParameter();

            var expected = 10;
            var actual   = param.Weirdness;

            Assert.IsType <int>(actual);
            Assert.Equal(expected, actual);
        }
Exemple #5
0
        public async Task Translate_NotSuccessStatusCode_ThrowsHttpRequestException()
        {
            var mockHttpHandler = HttpHandler.GetMockFailedHttpHandlerObject();
            var giphy           = new Giphy(mockHttpHandler);
            var search          = new TranslateParameter {
                Query = "test"
            };

            _ = await Assert.ThrowsAsync <HttpRequestException>(() => giphy.Translate(search));
        }
Exemple #6
0
        public async Task Translate_QueryNotSpecified_ThrowsFormatException()
        {
            var giphy = new Giphy("test");

            //  The query variable was not set in the paramter model
            //  which is required to run a query on giphy's api
            var search = new TranslateParameter();

            _ = await Assert.ThrowsAsync <FormatException>(() => giphy.Translate(search));
        }
Exemple #7
0
        public async Task Translate_WeirdnessOutOfRange_ThrowsFormatException(int value)
        {
            var giphy = new Giphy("test");

            var search = new TranslateParameter
            {
                Query     = "test",
                Weirdness = value
            };

            _ = await Assert.ThrowsAsync <FormatException>(() => giphy.Translate(search));
        }
Exemple #8
0
        public async Task Translate_WhenCalled_ReturnsGiphySingle()
        {
            var mockHttpHandler = HttpHandler.GetMockSuccessHttpHandlerObject();
            var giphy           = new Giphy(mockHttpHandler);
            var search          = new TranslateParameter {
                Query = "test"
            };

            var actual = await giphy.Translate(search);

            Assert.NotNull(actual);
            Assert.IsType <GiphySingle>(actual);
        }
        public void Query_WhenSet_EscapesUriString()
        {
            var param = new TranslateParameter
            {
                Query = "This is a unit test!"
            };

            var expected = "This%20is%20a%20unit%20test!";
            var actual   = param.Query;

            Assert.NotNull(actual);
            Assert.IsType <string>(actual);
            Assert.Equal(expected, actual);
        }