private string GenerateByIDRequestParameters(ByIDRequest byIDRequest) { string parameters = "?apikey=" + this.OMDB_API_Key; parameters += "&" + BY_ID_PARAM_IMDB_ID + "=" + byIDRequest.IMDB_ID; parameters += "&" + BY_ID_PARAM_PLOT_LENGTH + "=" + byIDRequest.PlotSize.ToString().ToLower(); parameters += "&" + PARAM_RESPONSE_DATA_TYPE; return(parameters); }
/// <summary> /// Performs a ByIDRequest asynchronously. /// </summary> /// <param name="byIDRequest">Requires a ByIDRequest object that will be used to specify GET request parameters.</param> /// <returns>A ByTitleResponse object.</returns> public async Task <ByTitleResponse> ByIDRequestAsync(ByIDRequest byIDRequest) { ByTitleResponse title = null; string parameters = GenerateByIDRequestParameters(byIDRequest); HttpResponseMessage response = await client.GetAsync(parameters); if (response.IsSuccessStatusCode) { string jsonContent = await response.Content.ReadAsStringAsync(); title = JsonConvert.DeserializeObject <ByTitleResponse>(jsonContent); } else { throw new HttpRequestException($"ByIDRequest was unsuccessful with HTTP status code {response.StatusCode.ToString()}"); } return(title); }
/// <summary> /// Performs a ByIDRequest synchronously. /// </summary> /// <param name="byIDRequest">Requires a ByIDRequest object that will be used to specify GET request parameters.</param> /// <returns>A ByTitleResponse object.</returns> public ByTitleResponse ByIDRequestSync(ByIDRequest byIDRequest) { return(ByIDRequestAsync(byIDRequest).GetAwaiter().GetResult()); }
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(); }