| | 1 | | // <auto-generated> |
| | 2 | | // Code generated by Microsoft (R) AutoRest Code Generator. |
| | 3 | | // Changes may cause incorrect behavior and will be lost if the code is |
| | 4 | | // regenerated. |
| | 5 | | // </auto-generated> |
| | 6 | |
|
| | 7 | | namespace Microsoft.Azure.CognitiveServices.Search.EntitySearch |
| | 8 | | { |
| | 9 | | using Microsoft.Rest; |
| | 10 | | using Models; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | | using System.Collections; |
| | 13 | | using System.Collections.Generic; |
| | 14 | | using System.IO; |
| | 15 | | using System.Linq; |
| | 16 | | using System.Net; |
| | 17 | | using System.Net.Http; |
| | 18 | | using System.Threading; |
| | 19 | | using System.Threading.Tasks; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// EntitiesOperations operations. |
| | 23 | | /// </summary> |
| | 24 | | public partial class EntitiesOperations : IServiceOperations<EntitySearchClient>, IEntitiesOperations |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the EntitiesOperations class. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name='client'> |
| | 30 | | /// Reference to the service client. |
| | 31 | | /// </param> |
| | 32 | | /// <exception cref="System.ArgumentNullException"> |
| | 33 | | /// Thrown when a required parameter is null |
| | 34 | | /// </exception> |
| 2 | 35 | | public EntitiesOperations(EntitySearchClient client) |
| | 36 | | { |
| 2 | 37 | | if (client == null) |
| | 38 | | { |
| 0 | 39 | | throw new System.ArgumentNullException("client"); |
| | 40 | | } |
| 2 | 41 | | Client = client; |
| 2 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets a reference to the EntitySearchClient |
| | 46 | | /// </summary> |
| 16 | 47 | | public EntitySearchClient Client { get; private set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// The Entity Search API lets you send a search query to Bing and get back |
| | 51 | | /// search results that include entities and places. Place results include |
| | 52 | | /// restaurants, hotel, or other local businesses. For places, the query can |
| | 53 | | /// specify the name of the local business or it can ask for a list (for |
| | 54 | | /// example, restaurants near me). Entity results include persons, places, or |
| | 55 | | /// things. Place in this context is tourist attractions, states, countries, |
| | 56 | | /// etc. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name='query'> |
| | 59 | | /// The user's search term. |
| | 60 | | /// </param> |
| | 61 | | /// <param name='acceptLanguage'> |
| | 62 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 63 | | /// strings. The list is in decreasing order of preference. For additional |
| | 64 | | /// information, including expected format, see |
| | 65 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 66 | | /// header and the setLang query parameter are mutually exclusive; do not |
| | 67 | | /// specify both. If you set this header, you must also specify the cc query |
| | 68 | | /// parameter. Bing will use the first supported language it finds from the |
| | 69 | | /// list, and combine that language with the cc parameter value to determine |
| | 70 | | /// the market to return results for. If the list does not include a supported |
| | 71 | | /// language, Bing will find the closest language and market that supports the |
| | 72 | | /// request, and may use an aggregated or default market for the results |
| | 73 | | /// instead of a specified one. You should use this header and the cc query |
| | 74 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 75 | | /// the mkt and setLang query parameters. A user interface string is a string |
| | 76 | | /// that's used as a label in a user interface. There are very few user |
| | 77 | | /// interface strings in the JSON response objects. Any links in the response |
| | 78 | | /// objects to Bing.com properties will apply the specified language. |
| | 79 | | /// </param> |
| | 80 | | /// <param name='pragma'> |
| | 81 | | /// By default, Bing returns cached content, if available. To prevent Bing from |
| | 82 | | /// returning cached content, set the Pragma header to no-cache (for example, |
| | 83 | | /// Pragma: no-cache). |
| | 84 | | /// </param> |
| | 85 | | /// <param name='userAgent'> |
| | 86 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 87 | | /// mobile users with an optimized experience. Although optional, you are |
| | 88 | | /// strongly encouraged to always specify this header. The user-agent should be |
| | 89 | | /// the same string that any commonly used browser would send. For information |
| | 90 | | /// about user agents, see [RFC |
| | 91 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). |
| | 92 | | /// </param> |
| | 93 | | /// <param name='clientId'> |
| | 94 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 95 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 96 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 97 | | /// do not use the same client ID for a user across multiple requests, then |
| | 98 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 99 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 100 | | /// For example, if the second request has a different flight assignment than |
| | 101 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 102 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 103 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 104 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 105 | | /// relevance improvements help with better quality of results delivered by |
| | 106 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 107 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 108 | | /// required. Persisting the client ID across multiple requests for the same |
| | 109 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 110 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 111 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 112 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 113 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 114 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 115 | | /// include this header in a request is the first time the user uses your app |
| | 116 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 117 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 118 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 119 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 120 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 121 | | /// next time the user uses your app on that device, get the client ID that you |
| | 122 | | /// persisted. Bing responses may or may not include this header. If the |
| | 123 | | /// response includes this header, capture the client ID and use it for all |
| | 124 | | /// subsequent Bing requests for the user on that device. If you include the |
| | 125 | | /// X-MSEdge-ClientID, you must not include cookies in the request. |
| | 126 | | /// </param> |
| | 127 | | /// <param name='clientIp'> |
| | 128 | | /// The IPv4 or IPv6 address of the client device. The IP address is used to |
| | 129 | | /// discover the user's location. Bing uses the location information to |
| | 130 | | /// determine safe search behavior. Although optional, you are encouraged to |
| | 131 | | /// always specify this header and the X-Search-Location header. Do not |
| | 132 | | /// obfuscate the address (for example, by changing the last octet to 0). |
| | 133 | | /// Obfuscating the address results in the location not being anywhere near the |
| | 134 | | /// device's actual location, which may result in Bing serving erroneous |
| | 135 | | /// results. |
| | 136 | | /// </param> |
| | 137 | | /// <param name='location'> |
| | 138 | | /// A semicolon-delimited list of key/value pairs that describe the client's |
| | 139 | | /// geographical location. Bing uses the location information to determine safe |
| | 140 | | /// search behavior and to return relevant local content. Specify the key/value |
| | 141 | | /// pair as <key>:<value>. The following are the keys that you use |
| | 142 | | /// to specify the user's location. lat (required): The latitude of the |
| | 143 | | /// client's location, in degrees. The latitude must be greater than or equal |
| | 144 | | /// to -90.0 and less than or equal to +90.0. Negative values indicate southern |
| | 145 | | /// latitudes and positive values indicate northern latitudes. long (required): |
| | 146 | | /// The longitude of the client's location, in degrees. The longitude must be |
| | 147 | | /// greater than or equal to -180.0 and less than or equal to +180.0. Negative |
| | 148 | | /// values indicate western longitudes and positive values indicate eastern |
| | 149 | | /// longitudes. re (required): The radius, in meters, which specifies the |
| | 150 | | /// horizontal accuracy of the coordinates. Pass the value returned by the |
| | 151 | | /// device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m |
| | 152 | | /// for cell tower triangulation, and 18,000m for reverse IP lookup. ts |
| | 153 | | /// (optional): The UTC UNIX timestamp of when the client was at the location. |
| | 154 | | /// (The UNIX timestamp is the number of seconds since January 1, 1970.) head |
| | 155 | | /// (optional): The client's relative heading or direction of travel. Specify |
| | 156 | | /// the direction of travel as degrees from 0 through 360, counting clockwise |
| | 157 | | /// relative to true north. Specify this key only if the sp key is nonzero. sp |
| | 158 | | /// (optional): The horizontal velocity (speed), in meters per second, that the |
| | 159 | | /// client device is traveling. alt (optional): The altitude of the client |
| | 160 | | /// device, in meters. are (optional): The radius, in meters, that specifies |
| | 161 | | /// the vertical accuracy of the coordinates. Specify this key only if you |
| | 162 | | /// specify the alt key. Although many of the keys are optional, the more |
| | 163 | | /// information that you provide, the more accurate the location results are. |
| | 164 | | /// Although optional, you are encouraged to always specify the user's |
| | 165 | | /// geographical location. Providing the location is especially important if |
| | 166 | | /// the client's IP address does not accurately reflect the user's physical |
| | 167 | | /// location (for example, if the client uses VPN). For optimal results, you |
| | 168 | | /// should include this header and the X-MSEdge-ClientIP header, but at a |
| | 169 | | /// minimum, you should include this header. |
| | 170 | | /// </param> |
| | 171 | | /// <param name='countryCode'> |
| | 172 | | /// A 2-character country code of the country where the results come from. This |
| | 173 | | /// API supports only the United States market. If you specify this query |
| | 174 | | /// parameter, it must be set to us. If you set this parameter, you must also |
| | 175 | | /// specify the Accept-Language header. Bing uses the first supported language |
| | 176 | | /// it finds from the languages list, and combine that language with the |
| | 177 | | /// country code that you specify to determine the market to return results |
| | 178 | | /// for. If the languages list does not include a supported language, Bing |
| | 179 | | /// finds the closest language and market that supports the request, or it may |
| | 180 | | /// use an aggregated or default market for the results instead of a specified |
| | 181 | | /// one. You should use this query parameter and the Accept-Language query |
| | 182 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 183 | | /// the mkt and setLang query parameters. This parameter and the mkt query |
| | 184 | | /// parameter are mutually exclusive—do not specify both. |
| | 185 | | /// </param> |
| | 186 | | /// <param name='market'> |
| | 187 | | /// The market where the results come from. You are strongly encouraged to |
| | 188 | | /// always specify the market, if known. Specifying the market helps Bing route |
| | 189 | | /// the request and return an appropriate and optimal response. This parameter |
| | 190 | | /// and the cc query parameter are mutually exclusive—do not specify both. |
| | 191 | | /// </param> |
| | 192 | | /// <param name='responseFilter'> |
| | 193 | | /// A comma-delimited list of answers to include in the response. If you do not |
| | 194 | | /// specify this parameter, the response includes all search answers for which |
| | 195 | | /// there's relevant data. |
| | 196 | | /// </param> |
| | 197 | | /// <param name='responseFormat'> |
| | 198 | | /// The media type to use for the response. The following are the possible |
| | 199 | | /// case-insensitive values: JSON, JSONLD. The default is JSON. If you specify |
| | 200 | | /// JSONLD, the response body includes JSON-LD objects that contain the search |
| | 201 | | /// results. |
| | 202 | | /// </param> |
| | 203 | | /// <param name='safeSearch'> |
| | 204 | | /// A filter used to filter adult content. Off: Return webpages with adult |
| | 205 | | /// text, images, or videos. Moderate: Return webpages with adult text, but not |
| | 206 | | /// adult images or videos. Strict: Do not return webpages with adult text, |
| | 207 | | /// images, or videos. The default is Moderate. If the request comes from a |
| | 208 | | /// market that Bing's adult policy requires that safeSearch is set to Strict, |
| | 209 | | /// Bing ignores the safeSearch value and uses Strict. If you use the site: |
| | 210 | | /// query operator, there is the chance that the response may contain adult |
| | 211 | | /// content regardless of what the safeSearch query parameter is set to. Use |
| | 212 | | /// site: only if you are aware of the content on the site and your scenario |
| | 213 | | /// supports the possibility of adult content. Possible values include: 'Off', |
| | 214 | | /// 'Moderate', 'Strict' |
| | 215 | | /// </param> |
| | 216 | | /// <param name='setLang'> |
| | 217 | | /// The language to use for user interface strings. Specify the language using |
| | 218 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 219 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 220 | | /// always specify the language. Typically, you set setLang to the same |
| | 221 | | /// language specified by mkt unless the user wants the user interface strings |
| | 222 | | /// displayed in a different language. This parameter and the Accept-Language |
| | 223 | | /// header are mutually exclusive; do not specify both. A user interface string |
| | 224 | | /// is a string that's used as a label in a user interface. There are few user |
| | 225 | | /// interface strings in the JSON response objects. Also, any links to Bing.com |
| | 226 | | /// properties in the response objects apply the specified language. |
| | 227 | | /// </param> |
| | 228 | | /// <param name='customHeaders'> |
| | 229 | | /// Headers that will be added to request. |
| | 230 | | /// </param> |
| | 231 | | /// <param name='cancellationToken'> |
| | 232 | | /// The cancellation token. |
| | 233 | | /// </param> |
| | 234 | | /// <exception cref="ErrorResponseException"> |
| | 235 | | /// Thrown when the operation returned an invalid status code |
| | 236 | | /// </exception> |
| | 237 | | /// <exception cref="SerializationException"> |
| | 238 | | /// Thrown when unable to deserialize the response |
| | 239 | | /// </exception> |
| | 240 | | /// <exception cref="ValidationException"> |
| | 241 | | /// Thrown when a required parameter is null |
| | 242 | | /// </exception> |
| | 243 | | /// <exception cref="System.ArgumentNullException"> |
| | 244 | | /// Thrown when a required parameter is null |
| | 245 | | /// </exception> |
| | 246 | | /// <return> |
| | 247 | | /// A response object containing the response body and response headers. |
| | 248 | | /// </return> |
| | 249 | | public async Task<HttpOperationResponse<SearchResponse>> SearchWithHttpMessagesAsync(string query, string accept |
| | 250 | | { |
| 2 | 251 | | if (Client.Endpoint == null) |
| | 252 | | { |
| 0 | 253 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 254 | | } |
| 2 | 255 | | if (query == null) |
| | 256 | | { |
| 0 | 257 | | throw new ValidationException(ValidationRules.CannotBeNull, "query"); |
| | 258 | | } |
| 2 | 259 | | string xBingApisSDK = "true"; |
| | 260 | | // Tracing |
| 2 | 261 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 262 | | string _invocationId = null; |
| 2 | 263 | | if (_shouldTrace) |
| | 264 | | { |
| 0 | 265 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 266 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 267 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| 0 | 268 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| 0 | 269 | | tracingParameters.Add("pragma", pragma); |
| 0 | 270 | | tracingParameters.Add("userAgent", userAgent); |
| 0 | 271 | | tracingParameters.Add("clientId", clientId); |
| 0 | 272 | | tracingParameters.Add("clientIp", clientIp); |
| 0 | 273 | | tracingParameters.Add("location", location); |
| 0 | 274 | | tracingParameters.Add("countryCode", countryCode); |
| 0 | 275 | | tracingParameters.Add("market", market); |
| 0 | 276 | | tracingParameters.Add("query", query); |
| 0 | 277 | | tracingParameters.Add("responseFilter", responseFilter); |
| 0 | 278 | | tracingParameters.Add("responseFormat", responseFormat); |
| 0 | 279 | | tracingParameters.Add("safeSearch", safeSearch); |
| 0 | 280 | | tracingParameters.Add("setLang", setLang); |
| 0 | 281 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 282 | | ServiceClientTracing.Enter(_invocationId, this, "Search", tracingParameters); |
| | 283 | | } |
| | 284 | | // Construct URL |
| 2 | 285 | | var _baseUrl = Client.BaseUri; |
| 2 | 286 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "entities"; |
| 2 | 287 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 288 | | List<string> _queryParameters = new List<string>(); |
| 2 | 289 | | if (countryCode != null) |
| | 290 | | { |
| 0 | 291 | | _queryParameters.Add(string.Format("cc={0}", System.Uri.EscapeDataString(countryCode))); |
| | 292 | | } |
| 2 | 293 | | if (market != null) |
| | 294 | | { |
| 2 | 295 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 296 | | } |
| 2 | 297 | | if (query != null) |
| | 298 | | { |
| 2 | 299 | | _queryParameters.Add(string.Format("q={0}", System.Uri.EscapeDataString(query))); |
| | 300 | | } |
| 2 | 301 | | if (responseFilter != null) |
| | 302 | | { |
| 0 | 303 | | _queryParameters.Add(string.Format("ResponseFilter={0}", System.Uri.EscapeDataString(string.Join(",", re |
| | 304 | | } |
| 2 | 305 | | if (responseFormat != null) |
| | 306 | | { |
| 0 | 307 | | _queryParameters.Add(string.Format("ResponseFormat={0}", System.Uri.EscapeDataString(string.Join(",", re |
| | 308 | | } |
| 2 | 309 | | if (safeSearch != null) |
| | 310 | | { |
| 0 | 311 | | _queryParameters.Add(string.Format("SafeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 312 | | } |
| 2 | 313 | | if (setLang != null) |
| | 314 | | { |
| 0 | 315 | | _queryParameters.Add(string.Format("SetLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 316 | | } |
| 2 | 317 | | if (_queryParameters.Count > 0) |
| | 318 | | { |
| 2 | 319 | | _url += "?" + string.Join("&", _queryParameters); |
| | 320 | | } |
| | 321 | | // Create HTTP transport objects |
| 2 | 322 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 323 | | HttpResponseMessage _httpResponse = null; |
| 2 | 324 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 325 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 326 | | // Set Headers |
| 2 | 327 | | if (xBingApisSDK != null) |
| | 328 | | { |
| 2 | 329 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 330 | | { |
| 0 | 331 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 332 | | } |
| 2 | 333 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 334 | | } |
| 2 | 335 | | if (acceptLanguage != null) |
| | 336 | | { |
| 0 | 337 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 338 | | { |
| 0 | 339 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 340 | | } |
| 0 | 341 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 342 | | } |
| 2 | 343 | | if (pragma != null) |
| | 344 | | { |
| 0 | 345 | | if (_httpRequest.Headers.Contains("Pragma")) |
| | 346 | | { |
| 0 | 347 | | _httpRequest.Headers.Remove("Pragma"); |
| | 348 | | } |
| 0 | 349 | | _httpRequest.Headers.TryAddWithoutValidation("Pragma", pragma); |
| | 350 | | } |
| 2 | 351 | | if (userAgent != null) |
| | 352 | | { |
| 0 | 353 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 354 | | { |
| 0 | 355 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 356 | | } |
| 0 | 357 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 358 | | } |
| 2 | 359 | | if (clientId != null) |
| | 360 | | { |
| 0 | 361 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 362 | | { |
| 0 | 363 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 364 | | } |
| 0 | 365 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 366 | | } |
| 2 | 367 | | if (clientIp != null) |
| | 368 | | { |
| 0 | 369 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 370 | | { |
| 0 | 371 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 372 | | } |
| 0 | 373 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 374 | | } |
| 2 | 375 | | if (location != null) |
| | 376 | | { |
| 0 | 377 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 378 | | { |
| 0 | 379 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 380 | | } |
| 0 | 381 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 382 | | } |
| | 383 | |
|
| | 384 | |
|
| 2 | 385 | | if (customHeaders != null) |
| | 386 | | { |
| 0 | 387 | | foreach(var _header in customHeaders) |
| | 388 | | { |
| 0 | 389 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 390 | | { |
| 0 | 391 | | _httpRequest.Headers.Remove(_header.Key); |
| | 392 | | } |
| 0 | 393 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 394 | | } |
| | 395 | | } |
| | 396 | |
|
| | 397 | | // Serialize Request |
| 2 | 398 | | string _requestContent = null; |
| | 399 | | // Set Credentials |
| 2 | 400 | | if (Client.Credentials != null) |
| | 401 | | { |
| 2 | 402 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 403 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 404 | | } |
| | 405 | | // Send Request |
| 2 | 406 | | if (_shouldTrace) |
| | 407 | | { |
| 0 | 408 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 409 | | } |
| 2 | 410 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 411 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 412 | | if (_shouldTrace) |
| | 413 | | { |
| 0 | 414 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 415 | | } |
| 2 | 416 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 417 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 418 | | string _responseContent = null; |
| 2 | 419 | | if ((int)_statusCode != 200) |
| | 420 | | { |
| 0 | 421 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 422 | | try |
| | 423 | | { |
| 0 | 424 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 425 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 426 | | if (_errorBody != null) |
| | 427 | | { |
| 0 | 428 | | ex.Body = _errorBody; |
| | 429 | | } |
| 0 | 430 | | } |
| 0 | 431 | | catch (JsonException) |
| | 432 | | { |
| | 433 | | // Ignore the exception |
| 0 | 434 | | } |
| 0 | 435 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 436 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 437 | | if (_shouldTrace) |
| | 438 | | { |
| 0 | 439 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 440 | | } |
| 0 | 441 | | _httpRequest.Dispose(); |
| 0 | 442 | | if (_httpResponse != null) |
| | 443 | | { |
| 0 | 444 | | _httpResponse.Dispose(); |
| | 445 | | } |
| 0 | 446 | | throw ex; |
| | 447 | | } |
| | 448 | | // Create Result |
| 2 | 449 | | var _result = new HttpOperationResponse<SearchResponse>(); |
| 2 | 450 | | _result.Request = _httpRequest; |
| 2 | 451 | | _result.Response = _httpResponse; |
| | 452 | | // Deserialize Response |
| 2 | 453 | | if ((int)_statusCode == 200) |
| | 454 | | { |
| 2 | 455 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 456 | | try |
| | 457 | | { |
| 2 | 458 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<SearchResponse>(_responseContent |
| 2 | 459 | | } |
| 0 | 460 | | catch (JsonException ex) |
| | 461 | | { |
| 0 | 462 | | _httpRequest.Dispose(); |
| 0 | 463 | | if (_httpResponse != null) |
| | 464 | | { |
| 0 | 465 | | _httpResponse.Dispose(); |
| | 466 | | } |
| 0 | 467 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 468 | | } |
| | 469 | | } |
| 2 | 470 | | if (_shouldTrace) |
| | 471 | | { |
| 0 | 472 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 473 | | } |
| 2 | 474 | | return _result; |
| 2 | 475 | | } |
| | 476 | |
|
| | 477 | | } |
| | 478 | | } |