Exemple #1
0
        /// <summary>
        ///     Executes a generic HTTP GET command.
        /// </summary>
        /// <typeparam name="T">
        ///     The type which the JSON response from the API will be deserialized to.
        /// </typeparam>
        /// <param name="uri">
        ///     The URI for the target endpoint.
        /// </param>
        /// <returns>
        ///     The response metadata from the API and the deserialized query results.
        /// </returns>
        public async Task <IApiResponse <T> > Get <T>(Uri uri)
        {
            HttpRequestMessage requestMessage = new HttpRequestMessage
            {
                RequestUri = uri,
                Method     = HttpMethod.Get
            };

            //pass credentials to API
            requestMessage.Headers.Add("X-Mashape-Key", this._apiKey);
            HttpResponseMessage responseMessage = await this._httpClient.SendAsync(requestMessage);

            //validation
            Ensure.ApiKeyIsValid(responseMessage.StatusCode);
            Ensure.ResponseIsNotInternalServerError(responseMessage.StatusCode);
            //deserialization and formatting response
            T deserializedObject = this.DeserializeJson <T>(await responseMessage.Content.ReadAsStringAsync());

            return(new ApiResponse <T>
            {
                Content = deserializedObject,
                HttpResponse = new Response
                {
                    Body = responseMessage.Content,
                    StatusCode = responseMessage.StatusCode,
                    ReasonPhrase = responseMessage.ReasonPhrase
                }
            });
        }