Ejemplo n.º 1
0
        /// <summary>
        /// Tests the validity of the API Key asynchronously.
        /// </summary>
        /// <returns>TRUE if OMDB API Key is valid and HTTP Response code is Success. False if specified key generates an HTTP Unauthorized code.</returns>
        public async Task <bool> IsAPIKeyValidAsync()
        {
            ByTitleRequest      byTitleRequest    = new ByTitleRequest("ghost in the shell", VideoType.Movie, 1995);
            string              requestParameters = GenerateByTitleRequestParameters(byTitleRequest);
            HttpResponseMessage response          = await client.GetAsync(requestParameters);

            // Verify that HTTP status code is 200 (Success).
            if (response.IsSuccessStatusCode)
            {
                string jsonContent = await response.Content.ReadAsStringAsync();

                ByTitleResponse title = JsonConvert.DeserializeObject <ByTitleResponse>(jsonContent);

                // Also verify the ByTitleResponse's Response attribute (called Success in this API wrapper).
                return(title.ResponseSuccess);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Attempt to download the poster cover using the URI from the OMDB by-title response (synchronously).
 /// </summary>
 /// <param name="byTitleResponse"></param>
 /// <returns>An ImageDownload object which has a flag indicating is download was successful.</returns>
 public ImageDownload GetImageForByTitleResponseSync(ByTitleResponse byTitleResponse)
 {
     return(GetImageForByTitleResponseAsync(byTitleResponse).GetAwaiter().GetResult());
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Attempt to download the poster cover using the URI from the OMDB by-title response.
 /// </summary>
 /// <param name="byTitleResponse"></param>
 /// <returns>An ImageDownload object which has a flag indicating is download was successful.</returns>
 public async Task <ImageDownload> GetImageForByTitleResponseAsync(ByTitleResponse byTitleResponse)
 {
     return(await ImageDownloader.DownloadImage(client, byTitleResponse.Poster_URI));
 }
Ejemplo n.º 5
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();
        }