Exemplo n.º 1
0
        private string GenerateBySearchRequestParameters(BySearchRequest bySearchRequest)
        {
            string parameters = "?apikey=" + this.OMDB_API_Key;

            parameters += "&" + BY_SEARCH_PARAM_TITLE + "=" + bySearchRequest.Title.ToLower().Trim();

            if (bySearchRequest.VideoType.HasValue)
            {
                parameters += "&" + BY_SEARCH_PARAM_RESULT_TYPE + "=" + bySearchRequest.VideoType.ToString().ToLower();
            }

            if (bySearchRequest.Year.HasValue)
            {
                parameters += "&" + BY_SEARCH_PARAM_YEAR_OF_RELEASE + "=" + bySearchRequest.Year.ToString();
            }

            if (bySearchRequest.Page.HasValue)
            {
                parameters += "&" + BY_SEARCH_PARAM_RESULT_PAGE + "=" + bySearchRequest.Page.ToString();
            }

            parameters += "&" + PARAM_RESPONSE_DATA_TYPE;

            return(parameters);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs a BySearchRequest asynchronously.
        /// </summary>
        /// <param name="bySearchRequest">Requires a BySearchRequest object that will be used to specify GET request parameters.</param>
        /// <returns>A BySearchResponse object.</returns>
        public async Task <BySearchResponse> BySearchRequestAsync(BySearchRequest bySearchRequest)
        {
            BySearchResponse bySearchResponse = null;
            string           parameters       = GenerateBySearchRequestParameters(bySearchRequest);

            HttpResponseMessage response = await client.GetAsync(parameters);

            if (response.IsSuccessStatusCode)
            {
                string jsonContent = await response.Content.ReadAsStringAsync();

                bySearchResponse = JsonConvert.DeserializeObject <BySearchResponse>(jsonContent);
            }
            else
            {
                throw new HttpRequestException($"BySearchRequest was unsuccessful with HTTP status code {response.StatusCode.ToString()}");
            }

            // If no page number was specified, process all pages (if more than one is found).
            if (bySearchRequest.Page.HasValue == false && bySearchResponse.TotalResults > RESULT_ITEMS_PER_BY_SEARCH_REQUEST)
            {
                uint searchResultsProcessed = RESULT_ITEMS_PER_BY_SEARCH_REQUEST;
                uint pagesProcessed         = 1;

                while (searchResultsProcessed < bySearchResponse.TotalResults)
                {
                    BySearchResponse additional_results            = null;
                    BySearchRequest  additional_request            = new BySearchRequest(bySearchRequest.Title, bySearchRequest.VideoType, bySearchRequest.Year, ++pagesProcessed);
                    string           parameters_additional_request = GenerateBySearchRequestParameters(additional_request);

                    HttpResponseMessage additional_response = await client.GetAsync(parameters_additional_request);

                    if (additional_response.IsSuccessStatusCode)
                    {
                        string additional_json_content = await additional_response.Content.ReadAsStringAsync();

                        additional_results = JsonConvert.DeserializeObject <BySearchResponse>(additional_json_content);

                        foreach (BySearchResponse.SearchResultItem resultItem in additional_results.SearchResults)
                        {
                            bySearchResponse.SearchResults.Add(resultItem);
                        }

                        searchResultsProcessed += (uint)additional_results.SearchResults.Count;
                    }
                    else
                    {
                        throw new HttpRequestException($"BySearchRequest was unsuccessful with HTTP status code {response.StatusCode.ToString()}");
                    }
                }
            }

            return(bySearchResponse);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Performs a BySearchRequest synchronously.
 /// </summary>
 /// <param name="bySearchRequest">Requires a BySearchRequest object that will be used to specify GET request parameters.</param>
 /// <returns>A BySearchResponse object.</returns>
 public BySearchResponse BySearchRequestSync(BySearchRequest bySearchRequest)
 {
     return(BySearchRequestAsync(bySearchRequest).GetAwaiter().GetResult());
 }
Exemplo n.º 4
0
        public static async Task MainAsync()
        {
            Console.WriteLine("The following is a demonstration the OMDB_API_Wrapper.\n");

            #region API Key Validation Demo

            // Ask user for the OMDB API Key.
            Console.WriteLine("Please enter a valid OMDB Key:\n");
            string omdb_api_key = Console.ReadLine();

            // Create the OMDB API Client.
            OmdbClient omdbClient = new OmdbClient(omdb_api_key);

            // Verify if the API Key is valid.
            bool isKeyValid = await omdbClient.IsAPIKeyValidAsync();

            while (isKeyValid == false)
            {
                Console.WriteLine("Please enter a valid OMDB Key to continue demonstration:\n");
                omdb_api_key = Console.ReadLine();

                omdbClient = new OmdbClient(omdb_api_key);
                isKeyValid = omdbClient.IsAPIKeyValidSync();

                if (isKeyValid == false)
                {
                    Console.WriteLine("->The API Key is NOT valid.\n");
                }
            }

            Console.WriteLine("->The API Key entered is valid.\n");

            #endregion

            #region ByTitleRequest DEMO

            Console.WriteLine("Here is a demo for requesting a ByTitleRequest:\n");

            // Create a ByTitleRequest.
            ByTitleRequest byTitleRequest = new ByTitleRequest("rick and morty", VideoType.Series, null, PlotSize.Full);

            // Obtain a ByTitleResponse for the ByTitleRequest asynchronously.
            ByTitleResponse byTitleResponse = await omdbClient.ByTitleRequestAsync(byTitleRequest);

            // Print ByTitleRequest object.
            Console.WriteLine("ByTitleRequest Object Attributes:");
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(byTitleRequest))
            {
                string name  = descriptor.Name;
                object value = descriptor.GetValue(byTitleRequest);
                Console.WriteLine("{0}={1}\n", name, value);
            }

            // Print ByTitleResponse object's JSON attributes.
            Console.WriteLine("ByTitleResponse JSON attributes:");
            PrintObjectJsonStyle(byTitleResponse);

            #endregion

            #region ByIDRequest DEMO

            Console.WriteLine("Here is a demo for requesting a ByIDRequest:\n");

            // Create a ByIDRequest.
            ByIDRequest byIDRequest = new ByIDRequest("tt1219827", PlotSize.Full);

            // Obtain a ByTitleResponse for the ByIDRequest asynchronously.
            ByTitleResponse byTitleResponseForIDRequest = await omdbClient.ByIDRequestAsync(byIDRequest);

            // Print ByIDRequest object.
            Console.WriteLine("ByIDRequest Object Attributes:");
            Console.WriteLine($"IMDB_ID = {byIDRequest.IMDB_ID}\n");

            // Print ByTitleResponse object's JSON attributes.
            Console.WriteLine("ByTitleResponse JSON attributes:");
            PrintObjectJsonStyle(byTitleResponseForIDRequest);

            #endregion

            #region Image Download DEMO

            // Attempt to download the image and write to root of C drive.
            ImageDownload imageDownload = await omdbClient.GetImageForByTitleResponseAsync(byTitleResponseForIDRequest);

            if (imageDownload.DownloadSuccessful)
            {
                File.WriteAllBytes(@"C:\" + imageDownload.FileName, imageDownload.Data);
            }

            #endregion

            #region BySearchRequest DEMO

            Console.WriteLine("Here is a demo for requesting a BySearchRequest:\n");

            // Create a BySearchRequest.
            BySearchRequest bySearchRequest = new BySearchRequest("ghost in the shell");

            // Obtain a BySearchResponse for the BySearchRequest asynchronously.
            BySearchResponse bySearchResponse = await omdbClient.BySearchRequestAsync(bySearchRequest);

            // Print BySearchRequest object.
            Console.WriteLine("BySearchRequest Object Attributes:");
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(bySearchRequest))
            {
                string name  = descriptor.Name;
                object value = descriptor.GetValue(bySearchRequest);
                Console.WriteLine("{0}={1}\n", name, value);
            }

            // Print BySearchResponse object's JSON attributes.
            Console.WriteLine("BySearchResponse JSON attributes:");
            PrintObjectJsonStyle(bySearchResponse);

            #endregion

            // Prevent console from terminating.
            string hold = Console.ReadLine();
        }