public static ComicSearchResult SearchCharactersByName(string charName)
        {
            ComicSearchResult searchResult = new ComicSearchResult();

            // If not already downloaded then download and return a copy
            using (var client = new System.Net.Http.HttpClient())
            {
                //Passing service base url
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllEmployees using HttpClient
                System.Net.Http.HttpResponseMessage Res = client.GetAsync("search/" + charName).Result;

                //Checking the response is successful or not which is sent using HttpClient
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    var ComicCharacterSearchResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list
                    searchResult = Newtonsoft.Json.JsonConvert.DeserializeObject <ComicSearchResult>(ComicCharacterSearchResponse);

                    if (searchResult.results != null && searchResult.results.Count() > 0)
                    {
                        foreach (var comicCharacter in searchResult.results)
                        {
                            // Check if the character image is available, if not then replace it with default image
                            if (comicCharacter != null && comicCharacter.image != null && !IsFileExistOnRemoteServer(comicCharacter.image.url))
                            {
                                comicCharacter.image.url = DefaultThumbnailUrl;
                            }

                            // Check if the requested character is already downloaded
                            if (!DownloadedComicCharacters.Any(x => x.id.Equals(comicCharacter.id)))
                            {
                                // Add the downloaded character in downloaded list
                                DownloadedComicCharacters.Add(comicCharacter);
                            }
                        }
                    }
                }
                //returning the result
                return(searchResult);
            }
        }
Exemple #2
0
        public ActionResult SearchCharacters(string searchTerm)
        {
            ComicSearchResult searchResult = SuperheroAPIHandler.SearchCharactersByName(searchTerm);

            return(View(searchResult));
        }