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