private static void SetStatusCode(NCDCResponse <T> ncdcResponse, HttpStatusCode statusCode) { switch (statusCode) { case HttpStatusCode.OK: ncdcResponse.Result = RequestResult.Success; break; case HttpStatusCode.BadRequest: ncdcResponse.Result = RequestResult.BadRequest; break; case (HttpStatusCode)420: //Rate Limited from Search/Trends API ncdcResponse.Result = RequestResult.RateLimited; break; case HttpStatusCode.Unauthorized: ncdcResponse.Result = RequestResult.Unauthorized; break; case HttpStatusCode.NotFound: ncdcResponse.Result = RequestResult.FileNotFound; break; case HttpStatusCode.Forbidden: ncdcResponse.Result = RequestResult.Unauthorized; break; default: ncdcResponse.Result = RequestResult.Unknown; break; } }
/// <summary> /// Executes the command. /// </summary> /// <returns>The results of the command.</returns> /// <remarks></remarks> public NCDCResponse <T> ExecuteCommand() { this.Token = NCDCUtilities.GetUnlockedToken(); lock (Token) { NCDCResponse <T> ncdcResponse = new NCDCResponse <T>(); ncdcResponse.Command = this; if (true) // use SSL { this.Uri = new Uri(this.Uri.AbsoluteUri.Replace("http://", "https://")); } // check if token exists if (string.IsNullOrEmpty(this.Token)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Token is required for the \"{0}\" command.", this.GetType())); } // Prepare the query parameters Dictionary <string, object> queryParameters = new Dictionary <string, object>(); foreach (KeyValuePair <string, object> item in this.RequestParameters) { queryParameters.Add(item.Key, item.Value); } // Declare the variable to be returned ncdcResponse.ResponseObject = default(T); ncdcResponse.RequestUrl = this.Uri.AbsoluteUri; byte[] responseData; try { NCDCWebRequest requestBuilder = new NCDCWebRequest(this.Uri, this.Verb, this.Token); foreach (var item in queryParameters) { requestBuilder.Parameters.Add(item.Key, item.Value); } HttpWebResponse response = requestBuilder.ExecuteRequest(); if (response == null) { ncdcResponse.Result = RequestResult.Unknown; return(ncdcResponse); } responseData = NCDCUtilities.ReadStream(response.GetResponseStream()); ncdcResponse.Content = Encoding.UTF8.GetString(responseData, 0, responseData.Length); ncdcResponse.RequestUrl = requestBuilder.RequestUri.AbsoluteUri; SetStatusCode(ncdcResponse, response.StatusCode); } catch (WebException wex) { if (new[] { WebExceptionStatus.Timeout, WebExceptionStatus.ConnectionClosed, WebExceptionStatus.ConnectFailure }.Contains(wex.Status)) { ncdcResponse.Result = RequestResult.ConnectionFailure; ncdcResponse.ErrorMessage = wex.Message; return(ncdcResponse); } // The exception response should always be an HttpWebResponse, but we check for good measure. HttpWebResponse exceptionResponse = wex.Response as HttpWebResponse; if (exceptionResponse == null) { throw; } responseData = NCDCUtilities.ReadStream(exceptionResponse.GetResponseStream()); ncdcResponse.Content = Encoding.UTF8.GetString(responseData, 0, responseData.Length); SetStatusCode(ncdcResponse, exceptionResponse.StatusCode); if (wex.Status == WebExceptionStatus.UnknownError) { throw; } return(ncdcResponse); } try { ncdcResponse.ResponseObject = NCDCSerialization <T> .Deserialize(responseData, this.DeserializationHandler); } catch (Newtonsoft.Json.JsonReaderException) { ncdcResponse.ErrorMessage = "Unable to parse JSON"; ncdcResponse.Result = RequestResult.Unknown; return(ncdcResponse); } catch (Newtonsoft.Json.JsonSerializationException) { ncdcResponse.ErrorMessage = "Unable to parse JSON"; ncdcResponse.Result = RequestResult.Unknown; return(ncdcResponse); } // Pass the current oauth tokens into the new object, so method calls from there will keep the authentication. ncdcResponse.Token = this.Token; //ncdcResponse.UpdateContent(); return(ncdcResponse); } }