コード例 #1
0
        public static string Request(string lexicalCategory, string word)
        {
            string cache = "";

            if (CacheWord.Check(word, "Definitions"))
            {
                return(CacheWord.Read(word, "Definitions", lexicalCategory));
            }
            string              definitions = "";                                                                                                                         // variable to store the result
            string              url         = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/" + word + "/definitions;regions=" + TestGeneratorForm.region; // URL for the request
            HttpClient          client      = new HttpClient();                                                                                                           // creates an HTTP Client
            HttpResponseMessage response    = new HttpResponseMessage();                                                                                                  // used to get the API Response

            client.BaseAddress = new Uri(url);                                                                                                                            // sets the client address to the specified url
            client.DefaultRequestHeaders.Add("app_id", TestGeneratorForm.appId);                                                                                          // adds the id to the headers
            client.DefaultRequestHeaders.Add("app_key", TestGeneratorForm.appKey);                                                                                        // adds the key to the headers
            try { response = client.GetAsync(url).Result; } // gets the respone headers
            catch (Exception) { MessageBox.Show("Unable to connect to the internet. Restart the program with internet connectivity at least once!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            if (response.IsSuccessStatusCode)                                                                       // checks if the response code is equal to 200
            {
                string content = response.Content.ReadAsStringAsync().Result;                                       // receives the API response
                var    result  = JsonConvert.DeserializeObject <GetResponse>(content);                              // Converts the API response to the format that the program can understand
                for (int i = 0; i < result.Results.First().LexicalEntries.Length; i++)                              // i = all entries from the API response
                {
                    for (int j = 0; j < result.Results.First().LexicalEntries[i].Entries.Length; j++)               // j = all senses from the API response
                    {
                        for (int k = 0; k < result.Results.First().LexicalEntries[i].Entries[j].Senses.Length; k++) // k = all definitions from the API response
                        {
                            for (int l = 0; result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions != null && l < result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions.Length; l++)
                            {
                                if (result.Results.First().LexicalEntries[i].LexicalCategory.ToLower() == lexicalCategory || lexicalCategory == "") // checks if the current lexicalCategory matches the one designated by the user
                                {
                                    definitions += "[" + result.Results.First().LexicalEntries[i].LexicalCategory.ToUpper() + " - DEFINITIONS]\n"
                                                   + char.ToUpper(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions[l][0]) + result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions[l].Substring(1) + " \n"; // adds the definition to the variable
                                }
                                cache += "[" + result.Results.First().LexicalEntries[i].LexicalCategory.ToUpper() + " - DEFINITIONS]\n"
                                         + char.ToUpper(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions[l][0]) + result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Definitions[l].Substring(1) + " \n";
                            }
                            if (result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses != null)                                        // checks if there is at least one subsense in the current sense
                            {
                                for (int l = 0; l < result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses.Length; l++)                // l = all subsense definitions from the API response
                                {
                                    if (result.Results.First().LexicalEntries[i].LexicalCategory.ToLower() == lexicalCategory || lexicalCategory == "") // checks if the current lexicalCategory matches the one designated by the user
                                    {
                                        definitions += "[" + result.Results.First().LexicalEntries[i].LexicalCategory.ToUpper() + " - DEFINITIONS]\n"
                                                       + char.ToUpper(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Definitions.First()[0]) + result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Definitions.First().Substring(1) + " \n"; // adds the definition to the variable
                                    }
                                    cache += "[" + result.Results.First().LexicalEntries[i].LexicalCategory.ToUpper() + " - DEFINITIONS]\n"
                                             + char.ToUpper(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Definitions.First()[0]) + result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Definitions.First().Substring(1) + " \n";
                                }
                            }
                        }
                    }
                }
                CacheWord.Write(word, "Definitions", cache);
                return(definitions.Trim()); // returns the result
            }
            else // if the response code is different than 200
            {
                if (response.StatusCode.ToString() == "Forbidden")
                {
                    Utility.GetNewCredentials(); Get(lexicalCategory, word);
                }
                return("ERROR \nCouldn't find " + word + " Status: " + response.StatusCode); // error while trying to access the API
            }
        }
コード例 #2
0
        public static string Request(string word, out char answer)
        {
            List <string> cache = new List <string> {
                word
            };

            answer = 'A';
            if (CacheWord.Check(word, "Synonyms"))
            {
                return(AnswerSheet.GenerateChoices((Utility.ShuffleElements(CacheWord.Read(word, "Synonyms"))), word, out answer));
            }
            string              url      = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/" + word + "/synonyms"; // URL for the request
            HttpClient          client   = new HttpClient();                                                                    // creates an HTTP Client
            HttpResponseMessage response = new HttpResponseMessage();                                                           // used to get the API Response

            client.BaseAddress = new Uri(url);                                                                                  // sets the client address to the specified url
            client.DefaultRequestHeaders.Add("app_id", TestGeneratorForm.appId);                                                // adds the id to the headers
            client.DefaultRequestHeaders.Add("app_key", TestGeneratorForm.appKey);                                              // adds the key to the headers
            try { response = client.GetAsync(url).Result; }// gets the respone headers
            catch (Exception) { MessageBox.Show("Unable to connect to the internet. Restart the program with internet connectivity at least once!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            if (response.IsSuccessStatusCode)
            {
                string content = response.Content.ReadAsStringAsync().Result;                                       // receives the API response
                var    result  = JsonConvert.DeserializeObject <GetResponse>(content);                              // Converts the API response to the format that the program can understand
                for (int i = 0; i < result.Results.First().LexicalEntries.Length; i++)                              // i = all entries from the API response
                {
                    for (int j = 0; j < result.Results.First().LexicalEntries[i].Entries.Length; j++)               // j = all senses from the API response
                    {
                        for (int k = 0; k < result.Results.First().LexicalEntries[i].Entries[j].Senses.Length; k++) // k = all examples from the API response
                        {
                            if (result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Synonyms != null)
                            {
                                for (int l = 0; l < result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Synonyms.Length; l++)
                                {
                                    cache.Add(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Synonyms[l].Text);
                                }
                            }
                            if (result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses != null) // checks if there is at least one subsense in the current sense
                            {
                                for (int l = 0; l < result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses.Length; l++)
                                {
                                    for (int m = 0; m < result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Synonyms.Length; m++)
                                    {
                                        cache.Add(result.Results.First().LexicalEntries[i].Entries[j].Senses[k].Subsenses[l].Synonyms[m].Text);
                                    }
                                }
                            }
                        }
                    }
                }
                int remove = Math.Max(1, cache.Count() - 4);
                cache.RemoveRange(1, remove);
                cache = Utility.ShuffleElements(cache);
                CacheWord.Write(word, "Synonyms", cache);
                return(AnswerSheet.GenerateChoices(cache, word, out answer));
            }
            else // if the response code is different than 200
            {
                if (response.StatusCode.ToString() == "Forbidden")
                {
                    Utility.GetNewCredentials(); Request(word, out answer);
                }
                return("ERROR \nCouldn't find " + word + " Status: " + response.StatusCode); // error while trying to access the API
            }
        }