/* * Method that gets specific joke based on it's ID * Creates within private scope another ChuckJokeModel for getting specific joke - avoids deleting list of id's previously gained */ private async Task LoadSpecificJoke(ChuckJokeModel joke, int index) { string id = ""; id = joke.Result[index].ID; ChuckJokeModel joke2 = await Process.GetJokesFromList(id); txtShowJoke.Text = joke2.Value; }
// operations /* * Requests from API total for specific word * If total > 0, it also gets list of ID's for all these jokes * Uses first ID from list to call LoadSpecificJoke that loads first joke */ private async Task LoadSearchResult(string word) { joke = await Process.SearchChuckJokeWord(word); text = joke.Total.ToString(); txtShowResult.Text = "There are " + text + " jokes"; if (joke.Total > 0) { btnNext.Content = "Connecting..."; await LoadSpecificJoke(joke, index); btnNext.IsEnabled = true; btnNext.Content = "Next"; } }
// operations /* * index is set to 0 - when opening application - connects to endpoint for random joke * loadOnce - makes sure joke is not loaded without pressing button * loads random joke or specific joke - based on index */ private async Task LoadJoke(int index) { joke = await Process.LoadRandomChuckJoke(index); btnGenerate.IsEnabled = true; btnGenerate.Content = "Get a Joke"; btnSearch.IsEnabled = true; if (loadOnce) { text = joke.Value; JokeArea.Text = text; } else { loadOnce = true; } }
public static async Task <ChuckJokeModel> LoadRandomChuckJoke(int index) { string url = ""; if (index == 0) { url = $"https://api.chucknorris.io/jokes/random"; } else if (index == 1) { url = $"https://api.chucknorris.io/jokes/random?category={ "animal" }"; } else if (index == 2) { url = $"https://api.chucknorris.io/jokes/random?category={ "celebrity" }"; } else if (index == 3) { url = $"https://api.chucknorris.io/jokes/random?category={ "sport" }"; } else if (index == 4) { url = $"https://api.chucknorris.io/jokes/random?category={ "dev" }"; } else if (index == 5) { url = $"https://api.chucknorris.io/jokes/random?category={ "food" }"; } ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (HttpResponseMessage respone = await ConnectionAPI.Client.GetAsync(url)) { if (respone.IsSuccessStatusCode) { ChuckJokeModel chuck = await respone.Content.ReadAsAsync <ChuckJokeModel>(); return(chuck); } else { throw new Exception(respone.ReasonPhrase); } } }
public static async Task <ChuckJokeModel> GetJokesFromList(string key) { string url = ""; url = $"https://api.chucknorris.io/jokes/" + key; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (HttpResponseMessage respone = await ConnectionAPI.Client.GetAsync(url)) { if (respone.IsSuccessStatusCode) { ChuckJokeModel chuck = await respone.Content.ReadAsAsync <ChuckJokeModel>(); return(chuck); } else { throw new Exception(respone.ReasonPhrase); } } }