コード例 #1
0
        private string GetRandomWordsUrl(GetRandomWordsRequest getRandomWordsRequest)
        {
            StringBuilder urlBuilder = new StringBuilder();

            urlBuilder.Append(_getWordnikBaseUrlQuery.Query());
            urlBuilder.Append(RandomWordsPath);

            var args = GetArgs(getRandomWordsRequest).ToList();

            if (args.Any())
            {
                urlBuilder.Append("?");
                urlBuilder.Append(string.Join("&", args));
            }

            var url = urlBuilder.ToString();

            return(url);
        }
コード例 #2
0
        private IEnumerable <string> GetArgs(GetRandomWordsRequest getRandomWordsRequest)
        {
            yield return(WordnikUrlHelper.GetMinCorpusCountArgument(getRandomWordsRequest.MinCorpusCount));

            yield return(WordnikUrlHelper.GetMaxCorpusCountArgument(getRandomWordsRequest.MaxCorpusCount));

            foreach (var arg in WordnikUrlHelper.GetWithDictionaryDefinitionArguments(getRandomWordsRequest.WithDictionaryDefinitions))
            {
                yield return(arg);
            }
            yield return(WordnikUrlHelper.GetMinimumDictionaryCountArgument(getRandomWordsRequest.MinimumDictionaryCount));

            yield return(WordnikUrlHelper.GetMaximumDictionaryCountArgument(getRandomWordsRequest.MaximumDictionaryCount));

            yield return(WordnikUrlHelper.GetMaximumLengthArgument(getRandomWordsRequest.MaximumLength));

            yield return(WordnikUrlHelper.GetMinimumLengthArgument(getRandomWordsRequest.MinimumLength));

            foreach (var arg in WordnikUrlHelper.GetSortByArguments(getRandomWordsRequest.SortBy))
            {
                yield return(arg);
            }
            foreach (var arg in WordnikUrlHelper.GetSortOrderArguments(getRandomWordsRequest.SortOrder))
            {
                yield return(arg);
            }
            foreach (var arg in WordnikUrlHelper.GetExpludePartsOfSpeechArguments(getRandomWordsRequest.ExcludePartsOfSpeech))
            {
                yield return(arg);
            }
            foreach (var arg in WordnikUrlHelper.GetIncludePartsOfSpeechArguments(getRandomWordsRequest.IncludePartsOfSpeech))
            {
                yield return(arg);
            }
            yield return(WordnikUrlHelper.GetLimitArgument(getRandomWordsRequest.Limit));

            yield return(WordnikUrlHelper.GetApiKeyArgument(getRandomWordsRequest.ApiKey));
        }
コード例 #3
0
        public GetRandomWordsResponse GetRandomWords(GetRandomWordsRequest getRandomWordsRequest)
        {
            var url = GetRandomWordsUrl(getRandomWordsRequest);

            WebRequest request = WebRequest.Create(url);

            request.Method      = "GET";
            request.ContentType = "application/json";
            using (WebResponse webResponse = request.GetResponse())
            {
                using (Stream stream = webResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream);
                    string       responseFromWordnik = reader.ReadToEnd();
                    var          listified           = "{\"list\":" + responseFromWordnik + "}";
                    var          wordnikWordList     = JsonConvert.DeserializeObject <WordnikWordList>(listified);

                    return(new GetRandomWordsResponse(wordnikWordList.List.Select(w => new WordResponse()
                    {
                        Id = w.Id, Word = w.Word
                    })));
                }
            }
        }