/** * Send the request and return the response or raise SearchAPIError. * <p/> * * @param strictValidation A bool argument that's passed to the * validateQueryParams method. * @return SearchAPIResponse object containing the response * @throws ArgumentException Raises ArgumentException (raised from validateQueryParams) * @throws WebException WebException * @throws SearchAPIError SearchAPIError with api specific error. */ public SearchAPIResponse Send(bool strictValidation = true) { ValidateQueryParams(strictValidation); TaskCompletionSource <SearchAPIResponse> taskCompletionSource = new TaskCompletionSource <SearchAPIResponse>(); using (WebClient client = new WebClient()) { client.Headers.Add("User-Agent", ClientUserAgent); Uri uri = new Uri(Url); try { byte[] response = client.UploadValues(Url, _getUrlParams()); string jsonResp = System.Text.Encoding.UTF8.GetString(response); var res = JsonConvert.DeserializeObject <SearchAPIResponse>(System.Text.Encoding.UTF8.GetString(response)); res.RawJSON = jsonResp; _update_response_headers(webClient: client, response: res); return(res); } catch (WebException e) { if (e.Response == null) { throw e; } string result; using (StreamReader sr = new StreamReader(e.Response.GetResponseStream())) { result = sr.ReadToEnd(); } Dictionary <string, object> err = null; err = JsonConvert.DeserializeObject <Dictionary <string, object> >(result); if (!err.ContainsKey("error")) { throw e; } string error = err["error"].ToString(); if (!err.ContainsKey("@http_status_code")) { throw e; } int httpStatusCode; if (!int.TryParse(err["@http_status_code"].ToString(), out httpStatusCode)) { throw e; } SearchAPIError exc = new SearchAPIError(error, httpStatusCode); _update_response_headers(webResponse: e.Response, errResponse: exc); throw exc; } } }
private void _update_response_headers(WebClient webClient = null, WebResponse webResponse = null, SearchAPIResponse response = null, SearchAPIError errResponse = null) { WebHeaderCollection headers = null; if (webClient != null) { headers = webClient.ResponseHeaders; } else if (webResponse != null) { headers = webResponse.Headers; } if (headers == null) { return; } string value = headers.Get("X-QPS-Allotted"); int intVal; if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsAllotted = intVal; } if (errResponse != null) { errResponse.QpsAllotted = intVal; } } } value = headers.Get("X-QPS-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsCurrent = intVal; } if (errResponse != null) { errResponse.QpsCurrent = intVal; } } } value = headers.Get("X-APIKey-Quota-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QuotaAllotted = intVal; } if (errResponse != null) { errResponse.QuotaAllotted = intVal; } } } value = headers.Get("X-APIKey-Quota-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QuotaCurrent = intVal; } if (errResponse != null) { errResponse.QuotaCurrent = intVal; } } } value = headers.Get("X-QPS-Live-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsLiveAllotted = intVal; } if (errResponse != null) { errResponse.QpsLiveAllotted = intVal; } } } value = headers.Get("X-QPS-Live-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsLiveCurrent = intVal; } if (errResponse != null) { errResponse.QpsLiveCurrent = intVal; } } } value = headers.Get("X-QPS-Demo-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsDemoAllotted = intVal; } if (errResponse != null) { errResponse.QpsDemoAllotted = intVal; } } } value = headers.Get("X-QPS-Demo-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsDemoCurrent = intVal; } if (errResponse != null) { errResponse.QpsDemoCurrent = intVal; } } } value = headers.Get("X-Quota-Reset"); if (value != null) { try { value = value.Replace(" UTC", " +0"); DateTime quotaReset = DateTime.ParseExact(value, "dddd, MMMM dd, yyyy hh:mm:ss tt z", CultureInfo.InvariantCulture); if (response != null) { response.QuotaReset = quotaReset; } if (errResponse != null) { errResponse.QuotaReset = quotaReset; } } catch {} } value = headers.Get("X-Demo-Usage-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.DemoUsageAlloted = intVal; } if (errResponse != null) { errResponse.DemoUsageAlloted = intVal; } } } value = headers.Get("X-Demo-Usage-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.DemoUsageCurrent = intVal; } if (errResponse != null) { errResponse.DemoUsageCurrent = intVal; } } } value = headers.Get("X-Demo-Usage-Expiry"); if (value != null) { try { value = value.Replace(" UTC", " +0"); DateTime quotaReset = DateTime.ParseExact(value, "dddd, MMMM dd, yyyy hh:mm:ss tt z", CultureInfo.InvariantCulture); if (response != null) { response.DemoUsageExpiry = quotaReset; } if (errResponse != null) { errResponse.DemoUsageExpiry = quotaReset; } } catch { } } }
private void _searchUploadValuesCompletedEventHandler(WebClient client, UploadValuesCompletedEventArgs e, TaskCompletionSource <SearchAPIResponse> taskCompletionSource) { if (e.Error == null) { string jsonResp = System.Text.Encoding.UTF8.GetString(e.Result); var res = JsonConvert.DeserializeObject <SearchAPIResponse>(jsonResp); res.RawJSON = jsonResp; _update_response_headers(webClient: client, response: res); taskCompletionSource.SetResult(res); return; } if (!(e.Error is WebException)) { taskCompletionSource.SetException(e.Error); return; } WebException we = (WebException)e.Error; HttpWebResponse response = (HttpWebResponse)we.Response; if (response == null) { taskCompletionSource.SetException(e.Error); return; } string result; try { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { result = sr.ReadToEnd(); } } catch (Exception ex) { taskCompletionSource.SetException(ex); return; } Dictionary <string, object> err = null; try { err = JsonConvert.DeserializeObject <Dictionary <string, object> >(result); } catch (Exception ex) { taskCompletionSource.SetException(ex); return; } if (!err.ContainsKey("error")) { taskCompletionSource.SetException(we); return; } string error = err["error"].ToString(); if (!err.ContainsKey("@http_status_code")) { taskCompletionSource.SetException(we); return; } int httpStatusCode; if (!int.TryParse(err["@http_status_code"].ToString(), out httpStatusCode)) { taskCompletionSource.SetException(we); return; } SearchAPIError exc = new SearchAPIError(error, httpStatusCode); _update_response_headers(webResponse: response, errResponse: exc); taskCompletionSource.SetException(exc); return; }
private void _update_response_headers(WebClient webClient= null, WebResponse webResponse = null, SearchAPIResponse response = null, SearchAPIError errResponse = null) { WebHeaderCollection headers = null; if (webClient != null) { headers = webClient.ResponseHeaders; } else if (webResponse != null) { headers = webResponse.Headers; } if (headers == null) { return; } string value = headers.Get("X-QPS-Allotted"); int intVal; if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsAllotted = intVal; } if (errResponse != null) { errResponse.QpsAllotted = intVal; } } } value = headers.Get("X-QPS-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsCurrent = intVal; } if (errResponse != null) { errResponse.QpsCurrent = intVal; } } } value = headers.Get("X-APIKey-Quota-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QuotaAllotted = intVal; } if (errResponse != null) { errResponse.QuotaAllotted = intVal; } } } value = headers.Get("X-APIKey-Quota-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QuotaCurrent = intVal; } if (errResponse != null) { errResponse.QuotaCurrent = intVal; } } } value = headers.Get("X-QPS-Live-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsLiveAllotted = intVal; } if (errResponse != null) { errResponse.QpsLiveAllotted = intVal; } } } value = headers.Get("X-QPS-Live-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsLiveCurrent = intVal; } if (errResponse != null) { errResponse.QpsLiveCurrent = intVal; } } } value = headers.Get("X-QPS-Demo-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsDemoAllotted = intVal; } if (errResponse != null) { errResponse.QpsDemoAllotted = intVal; } } } value = headers.Get("X-QPS-Demo-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.QpsDemoCurrent = intVal; } if (errResponse != null) { errResponse.QpsDemoCurrent = intVal; } } } value = headers.Get("X-Quota-Reset"); if (value != null) { try { value = value.Replace(" UTC", " +0"); DateTime quotaReset = DateTime.ParseExact(value, "dddd, MMMM dd, yyyy hh:mm:ss tt z", CultureInfo.InvariantCulture); if (response != null) { response.QuotaReset = quotaReset; } if (errResponse != null) { errResponse.QuotaReset = quotaReset; } } catch {} } value = headers.Get("X-Demo-Usage-Allotted"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.DemoUsageAlloted = intVal; } if (errResponse != null) { errResponse.DemoUsageAlloted = intVal; } } } value = headers.Get("X-Demo-Usage-Current"); if (value != null) { if (Int32.TryParse(value, out intVal)) { if (response != null) { response.DemoUsageCurrent = intVal; } if (errResponse != null) { errResponse.DemoUsageCurrent = intVal; } } } value = headers.Get("X-Demo-Usage-Expiry"); if (value != null) { try { value = value.Replace(" UTC", " +0"); DateTime quotaReset = DateTime.ParseExact(value, "dddd, MMMM dd, yyyy hh:mm:ss tt z", CultureInfo.InvariantCulture); if (response != null) { response.DemoUsageExpiry = quotaReset; } if (errResponse != null) { errResponse.DemoUsageExpiry = quotaReset; } } catch { } } }
private void _searchUploadValuesCompletedEventHandler(WebClient client, UploadValuesCompletedEventArgs e, TaskCompletionSource<SearchAPIResponse> taskCompletionSource) { if (e.Error == null) { string jsonResp = System.Text.Encoding.UTF8.GetString(e.Result); var res = JsonConvert.DeserializeObject<SearchAPIResponse>(jsonResp); res.RawJSON = jsonResp; _update_response_headers(webClient: client, response: res); taskCompletionSource.SetResult(res); return; } if (!(e.Error is WebException)) { taskCompletionSource.SetException(e.Error); return; } WebException we = (WebException)e.Error; HttpWebResponse response = (HttpWebResponse)we.Response; if (response == null) { taskCompletionSource.SetException(e.Error); return; } string result; try { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { result = sr.ReadToEnd(); } } catch (Exception ex) { taskCompletionSource.SetException(ex); return; } Dictionary<string, object> err = null; try { err = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); } catch (Exception ex) { taskCompletionSource.SetException(ex); return; } if (!err.ContainsKey("error")) { taskCompletionSource.SetException(we); return; } string error = err["error"].ToString(); if (!err.ContainsKey("@http_status_code")) { taskCompletionSource.SetException(we); return; } int httpStatusCode; if (!int.TryParse(err["@http_status_code"].ToString(), out httpStatusCode)) { taskCompletionSource.SetException(we); return; } SearchAPIError exc = new SearchAPIError(error, httpStatusCode); _update_response_headers(webResponse: response, errResponse: exc); taskCompletionSource.SetException(exc); return; }
/** * Send the request and return the response or raise SearchAPIError. * <p/> * * @param strictValidation A bool argument that's passed to the * validateQueryParams method. * @return SearchAPIResponse object containing the response * @throws ArgumentException Raises ArgumentException (raised from validateQueryParams) * @throws WebException WebException * @throws SearchAPIError SearchAPIError with api specific error. */ public SearchAPIResponse Send(bool strictValidation = true) { ValidateQueryParams(strictValidation); TaskCompletionSource<SearchAPIResponse> taskCompletionSource = new TaskCompletionSource<SearchAPIResponse>(); using (WebClient client = new WebClient()) { client.Headers.Add("User-Agent", ClientUserAgent); Uri uri = new Uri(Url); try { byte[] response = client.UploadValues(Url, _getUrlParams()); string jsonResp = System.Text.Encoding.UTF8.GetString(response); var res = JsonConvert.DeserializeObject<SearchAPIResponse>(System.Text.Encoding.UTF8.GetString(response)); res.RawJSON = jsonResp; _update_response_headers(webClient: client, response: res); return res; } catch (WebException e) { if (e.Response == null) { throw e; } string result; using (StreamReader sr = new StreamReader(e.Response.GetResponseStream())) { result = sr.ReadToEnd(); } Dictionary<string, object> err = null; err = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); if (!err.ContainsKey("error")) { throw e; } string error = err["error"].ToString(); if (!err.ContainsKey("@http_status_code")) { throw e; } int httpStatusCode; if (!int.TryParse(err["@http_status_code"].ToString(), out httpStatusCode)) { throw e; } SearchAPIError exc = new SearchAPIError(error, httpStatusCode); _update_response_headers(webResponse: e.Response, errResponse: exc); throw exc; } } }