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