| | 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.AutoSuggest |
| | 8 | | { |
| | 9 | | using Microsoft.Rest; |
| | 10 | | using Microsoft.Rest.Serialization; |
| | 11 | | using Models; |
| | 12 | | using Newtonsoft.Json; |
| | 13 | | using System.Collections; |
| | 14 | | using System.Collections.Generic; |
| | 15 | | using System.Net; |
| | 16 | | using System.Net.Http; |
| | 17 | | using System.Threading; |
| | 18 | | using System.Threading.Tasks; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// The AutoSuggest Search API lets you send a search query to Bing and get |
| | 22 | | /// back a list of news that are relevant to the search query. This section |
| | 23 | | /// provides technical details about the query parameters and headers that |
| | 24 | | /// you use to request news and the JSON response objects that contain |
| | 25 | | /// them. For examples that show how to make requests, see [Searching the |
| | 26 | | /// web for |
| | 27 | | /// AutoSuggest](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference). |
| | 28 | | /// </summary> |
| | 29 | | public partial class AutoSuggestClient : ServiceClient<AutoSuggestClient>, IAutoSuggestClient |
| | 30 | | { |
| | 31 | | /// <summary> |
| | 32 | | /// The base URI of the service. |
| | 33 | | /// </summary> |
| 4 | 34 | | internal string BaseUri {get; set;} |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets or sets json serialization settings. |
| | 38 | | /// </summary> |
| 10 | 39 | | public JsonSerializerSettings SerializationSettings { get; private set; } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets or sets json deserialization settings. |
| | 43 | | /// </summary> |
| 12 | 44 | | public JsonSerializerSettings DeserializationSettings { get; private set; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Supported Cognitive Services endpoints (protocol and hostname, for example: |
| | 48 | | /// "https://westus.api.cognitive.microsoft.com", |
| | 49 | | /// "https://api.cognitive.microsoft.com"). |
| | 50 | | /// </summary> |
| 6 | 51 | | public string Endpoint { get; set; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Subscription credentials which uniquely identify client subscription. |
| | 55 | | /// </summary> |
| 10 | 56 | | public ServiceClientCredentials Credentials { get; private set; } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name='httpClient'> |
| | 62 | | /// HttpClient to be used |
| | 63 | | /// </param> |
| | 64 | | /// <param name='disposeHttpClient'> |
| | 65 | | /// True: will dispose the provided httpClient on calling AutoSuggestClient.Dispose(). False: will not dispose p |
| 0 | 66 | | protected AutoSuggestClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient) |
| | 67 | | { |
| 0 | 68 | | Initialize(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name='handlers'> |
| | 75 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 76 | | /// </param> |
| 2 | 77 | | protected AutoSuggestClient(params DelegatingHandler[] handlers) : base(handlers) |
| | 78 | | { |
| 2 | 79 | | Initialize(); |
| 2 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name='rootHandler'> |
| | 86 | | /// Optional. The http client handler used to handle http transport. |
| | 87 | | /// </param> |
| | 88 | | /// <param name='handlers'> |
| | 89 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 90 | | /// </param> |
| 0 | 91 | | protected AutoSuggestClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandl |
| | 92 | | { |
| 0 | 93 | | Initialize(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 98 | | /// </summary> |
| | 99 | | /// <param name='credentials'> |
| | 100 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 101 | | /// </param> |
| | 102 | | /// <param name='handlers'> |
| | 103 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 104 | | /// </param> |
| | 105 | | /// <exception cref="System.ArgumentNullException"> |
| | 106 | | /// Thrown when a required parameter is null |
| | 107 | | /// </exception> |
| 2 | 108 | | public AutoSuggestClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(handl |
| | 109 | | { |
| 2 | 110 | | if (credentials == null) |
| | 111 | | { |
| 0 | 112 | | throw new System.ArgumentNullException("credentials"); |
| | 113 | | } |
| 2 | 114 | | Credentials = credentials; |
| 2 | 115 | | if (Credentials != null) |
| | 116 | | { |
| 2 | 117 | | Credentials.InitializeServiceClient(this); |
| | 118 | | } |
| 2 | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name='credentials'> |
| | 125 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 126 | | /// </param> |
| | 127 | | /// <param name='httpClient'> |
| | 128 | | /// HttpClient to be used |
| | 129 | | /// </param> |
| | 130 | | /// <param name='disposeHttpClient'> |
| | 131 | | /// True: will dispose the provided httpClient on calling AutoSuggestClient.Dispose(). False: will not dispose p |
| | 132 | | /// <exception cref="System.ArgumentNullException"> |
| | 133 | | /// Thrown when a required parameter is null |
| | 134 | | /// </exception> |
| 0 | 135 | | public AutoSuggestClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) : |
| | 136 | | { |
| 0 | 137 | | if (credentials == null) |
| | 138 | | { |
| 0 | 139 | | throw new System.ArgumentNullException("credentials"); |
| | 140 | | } |
| 0 | 141 | | Credentials = credentials; |
| 0 | 142 | | if (Credentials != null) |
| | 143 | | { |
| 0 | 144 | | Credentials.InitializeServiceClient(this); |
| | 145 | | } |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Initializes a new instance of the AutoSuggestClient class. |
| | 150 | | /// </summary> |
| | 151 | | /// <param name='credentials'> |
| | 152 | | /// Required. Subscription credentials which uniquely identify client subscription. |
| | 153 | | /// </param> |
| | 154 | | /// <param name='rootHandler'> |
| | 155 | | /// Optional. The http client handler used to handle http transport. |
| | 156 | | /// </param> |
| | 157 | | /// <param name='handlers'> |
| | 158 | | /// Optional. The delegating handlers to add to the http client pipeline. |
| | 159 | | /// </param> |
| | 160 | | /// <exception cref="System.ArgumentNullException"> |
| | 161 | | /// Thrown when a required parameter is null |
| | 162 | | /// </exception> |
| 0 | 163 | | public AutoSuggestClient(ServiceClientCredentials credentials, HttpClientHandler rootHandler, params DelegatingH |
| | 164 | | { |
| 0 | 165 | | if (credentials == null) |
| | 166 | | { |
| 0 | 167 | | throw new System.ArgumentNullException("credentials"); |
| | 168 | | } |
| 0 | 169 | | Credentials = credentials; |
| 0 | 170 | | if (Credentials != null) |
| | 171 | | { |
| 0 | 172 | | Credentials.InitializeServiceClient(this); |
| | 173 | | } |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// An optional partial-method to perform custom initialization. |
| | 178 | | ///</summary> |
| | 179 | | partial void CustomInitialize(); |
| | 180 | | /// <summary> |
| | 181 | | /// Initializes client properties. |
| | 182 | | /// </summary> |
| | 183 | | private void Initialize() |
| | 184 | | { |
| 2 | 185 | | BaseUri = "{Endpoint}/bing/v7.0"; |
| 2 | 186 | | Endpoint = "https://api.cognitive.microsoft.com"; |
| 2 | 187 | | SerializationSettings = new JsonSerializerSettings |
| 2 | 188 | | { |
| 2 | 189 | | Formatting = Newtonsoft.Json.Formatting.Indented, |
| 2 | 190 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 191 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 192 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 193 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 194 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 195 | | Converters = new List<JsonConverter> |
| 2 | 196 | | { |
| 2 | 197 | | new Iso8601TimeSpanConverter() |
| 2 | 198 | | } |
| 2 | 199 | | }; |
| 2 | 200 | | DeserializationSettings = new JsonSerializerSettings |
| 2 | 201 | | { |
| 2 | 202 | | DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, |
| 2 | 203 | | DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc, |
| 2 | 204 | | NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, |
| 2 | 205 | | ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize, |
| 2 | 206 | | ContractResolver = new ReadOnlyJsonContractResolver(), |
| 2 | 207 | | Converters = new List<JsonConverter> |
| 2 | 208 | | { |
| 2 | 209 | | new Iso8601TimeSpanConverter() |
| 2 | 210 | | } |
| 2 | 211 | | }; |
| 2 | 212 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<SuggestionsSuggestionGroup>("_typ |
| 2 | 213 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<SuggestionsSuggestionGroup>( |
| 2 | 214 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<QueryContext>("_type")); |
| 2 | 215 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<QueryContext>("_type")); |
| 2 | 216 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<Error>("_type")); |
| 2 | 217 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<Error>("_type")); |
| 2 | 218 | | SerializationSettings.Converters.Add(new PolymorphicSerializeJsonConverter<ResponseBase>("_type")); |
| 2 | 219 | | DeserializationSettings.Converters.Add(new PolymorphicDeserializeJsonConverter<ResponseBase>("_type")); |
| | 220 | | CustomInitialize(); |
| 2 | 221 | | } |
| | 222 | | /// <summary> |
| | 223 | | /// The AutoSuggest API lets you send a search query to Bing and get back a |
| | 224 | | /// list of suggestions. This section provides technical details about the |
| | 225 | | /// query parameters and headers that you use to request suggestions and the |
| | 226 | | /// JSON response objects that contain them. |
| | 227 | | /// </summary> |
| | 228 | | /// <param name='query'> |
| | 229 | | /// The user's search term. |
| | 230 | | /// </param> |
| | 231 | | /// <param name='acceptLanguage'> |
| | 232 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 233 | | /// strings. The list is in decreasing order of preference. For additional |
| | 234 | | /// information, including expected format, see |
| | 235 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 236 | | /// header and the setLang query parameter are mutually exclusive; do not |
| | 237 | | /// specify both. If you set this header, you must also specify the |
| | 238 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference#cc) |
| | 239 | | /// query parameter. To determine the market to return results for, Bing uses |
| | 240 | | /// the first supported language it finds from the list and combines it with |
| | 241 | | /// the cc parameter value. If the list does not include a supported language, |
| | 242 | | /// Bing finds the closest language and market that supports the request or it |
| | 243 | | /// uses an aggregated or default market for the results. To determine the |
| | 244 | | /// market that Bing used, see the BingAPIs-Market header. Use this header and |
| | 245 | | /// the cc query parameter only if you specify multiple languages. Otherwise, |
| | 246 | | /// use the |
| | 247 | | /// [mkt](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference#mkt) |
| | 248 | | /// and |
| | 249 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference#setl |
| | 250 | | /// query parameters. A user interface string is a string that's used as a |
| | 251 | | /// label in a user interface. There are few user interface strings in the JSON |
| | 252 | | /// response objects. Any links to Bing.com properties in the response objects |
| | 253 | | /// apply the specified language. |
| | 254 | | /// </param> |
| | 255 | | /// <param name='pragma'> |
| | 256 | | /// By default, Bing returns cached content, if available. To prevent Bing from |
| | 257 | | /// returning cached content, set the Pragma header to no-cache (for example, |
| | 258 | | /// Pragma: no-cache). |
| | 259 | | /// </param> |
| | 260 | | /// <param name='userAgent'> |
| | 261 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 262 | | /// mobile users with an optimized experience. Although optional, you are |
| | 263 | | /// encouraged to always specify this header. The user-agent should be the same |
| | 264 | | /// string that any commonly used browser sends. For information about user |
| | 265 | | /// agents, see [RFC |
| | 266 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). The |
| | 267 | | /// following are examples of user-agent strings. Windows Phone: Mozilla/5.0 |
| | 268 | | /// (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; |
| | 269 | | /// Touch; NOKIA; Lumia 822). Android: Mozilla / 5.0 (Linux; U; Android 2.3.5; |
| | 270 | | /// en - us; SCH - I500 Build / GINGERBREAD) AppleWebKit / 533.1 (KHTML; like |
| | 271 | | /// Gecko) Version / 4.0 Mobile Safari / 533.1. iPhone: Mozilla / 5.0 (iPhone; |
| | 272 | | /// CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 536.26 (KHTML; like Gecko) |
| | 273 | | /// Mobile / 10B142 iPhone4; 1 BingWeb / 3.03.1428.20120423. PC: Mozilla / 5.0 |
| | 274 | | /// (Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0) like Gecko. iPad: |
| | 275 | | /// Mozilla / 5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit / 537.51.1 |
| | 276 | | /// (KHTML, like Gecko) Version / 7.0 Mobile / 11A465 Safari / 9537.53 |
| | 277 | | /// </param> |
| | 278 | | /// <param name='clientId'> |
| | 279 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 280 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 281 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 282 | | /// do not use the same client ID for a user across multiple requests, then |
| | 283 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 284 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 285 | | /// For example, if the second request has a different flight assignment than |
| | 286 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 287 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 288 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 289 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 290 | | /// relevance improvements help with better quality of results delivered by |
| | 291 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 292 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 293 | | /// required. Persisting the client ID across multiple requests for the same |
| | 294 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 295 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 296 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 297 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 298 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 299 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 300 | | /// include this header in a request is the first time the user uses your app |
| | 301 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 302 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 303 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 304 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 305 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 306 | | /// next time the user uses your app on that device, get the client ID that you |
| | 307 | | /// persisted. Bing responses may or may not include this header. If the |
| | 308 | | /// response includes this header, capture the client ID and use it for all |
| | 309 | | /// subsequent Bing requests for the user on that device. If you include the |
| | 310 | | /// X-MSEdge-ClientID, you must not include cookies in the request. |
| | 311 | | /// </param> |
| | 312 | | /// <param name='clientIp'> |
| | 313 | | /// The IPv4 or IPv6 address of the client device. The IP address is used to |
| | 314 | | /// discover the user's location. Bing uses the location information to |
| | 315 | | /// determine safe search behavior. Although optional, you are encouraged to |
| | 316 | | /// always specify this header and the X-Search-Location header. Do not |
| | 317 | | /// obfuscate the address (for example, by changing the last octet to 0). |
| | 318 | | /// Obfuscating the address results in the location not being anywhere near the |
| | 319 | | /// device's actual location, which may result in Bing serving erroneous |
| | 320 | | /// results. |
| | 321 | | /// </param> |
| | 322 | | /// <param name='location'> |
| | 323 | | /// A semicolon-delimited list of key/value pairs that describe the client's |
| | 324 | | /// geographical location. Bing uses the location information to determine safe |
| | 325 | | /// search behavior and to return relevant local content. Specify the key/value |
| | 326 | | /// pair as <key>:<value>. The following are the keys that you use |
| | 327 | | /// to specify the user's location. lat (required): The latitude of the |
| | 328 | | /// client's location, in degrees. The latitude must be greater than or equal |
| | 329 | | /// to -90.0 and less than or equal to +90.0. Negative values indicate southern |
| | 330 | | /// latitudes and positive values indicate northern latitudes. long (required): |
| | 331 | | /// The longitude of the client's location, in degrees. The longitude must be |
| | 332 | | /// greater than or equal to -180.0 and less than or equal to +180.0. Negative |
| | 333 | | /// values indicate western longitudes and positive values indicate eastern |
| | 334 | | /// longitudes. re (required): The radius, in meters, which specifies the |
| | 335 | | /// horizontal accuracy of the coordinates. Pass the value returned by the |
| | 336 | | /// device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m |
| | 337 | | /// for cell tower triangulation, and 18,000m for reverse IP lookup. ts |
| | 338 | | /// (optional): The UTC UNIX timestamp of when the client was at the location. |
| | 339 | | /// (The UNIX timestamp is the number of seconds since January 1, 1970.) head |
| | 340 | | /// (optional): The client's relative heading or direction of travel. Specify |
| | 341 | | /// the direction of travel as degrees from 0 through 360, counting clockwise |
| | 342 | | /// relative to true north. Specify this key only if the sp key is nonzero. sp |
| | 343 | | /// (optional): The horizontal velocity (speed), in meters per second, that the |
| | 344 | | /// client device is traveling. alt (optional): The altitude of the client |
| | 345 | | /// device, in meters. are (optional): The radius, in meters, that specifies |
| | 346 | | /// the vertical accuracy of the coordinates. Specify this key only if you |
| | 347 | | /// specify the alt key. Although many of the keys are optional, the more |
| | 348 | | /// information that you provide, the more accurate the location results are. |
| | 349 | | /// Although optional, you are encouraged to always specify the user's |
| | 350 | | /// geographical location. Providing the location is especially important if |
| | 351 | | /// the client's IP address does not accurately reflect the user's physical |
| | 352 | | /// location (for example, if the client uses VPN). For optimal results, you |
| | 353 | | /// should include this header and the X-MSEdge-ClientIP header, but at a |
| | 354 | | /// minimum, you should include this header. |
| | 355 | | /// </param> |
| | 356 | | /// <param name='countryCode'> |
| | 357 | | /// A 2-character country code of the country where the results come from. This |
| | 358 | | /// API supports only the United States market. If you specify this query |
| | 359 | | /// parameter, it must be set to us. If you set this parameter, you must also |
| | 360 | | /// specify the Accept-Language header. Bing uses the first supported language |
| | 361 | | /// it finds from the languages list, and combine that language with the |
| | 362 | | /// country code that you specify to determine the market to return results |
| | 363 | | /// for. If the languages list does not include a supported language, Bing |
| | 364 | | /// finds the closest language and market that supports the request, or it may |
| | 365 | | /// use an aggregated or default market for the results instead of a specified |
| | 366 | | /// one. You should use this query parameter and the Accept-Language query |
| | 367 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 368 | | /// the mkt and setLang query parameters. This parameter and the mkt query |
| | 369 | | /// parameter are mutually exclusive—do not specify both. |
| | 370 | | /// </param> |
| | 371 | | /// <param name='market'> |
| | 372 | | /// The market where the results come from. You are strongly encouraged to |
| | 373 | | /// always specify the market, if known. Specifying the market helps Bing route |
| | 374 | | /// the request and return an appropriate and optimal response. This parameter |
| | 375 | | /// and the cc query parameter are mutually exclusive—do not specify both. |
| | 376 | | /// </param> |
| | 377 | | /// <param name='safeSearch'> |
| | 378 | | /// Filter suggestions for adult content. The following are the possible filter |
| | 379 | | /// values. Off: Return suggestions with adult text, images, or videos. |
| | 380 | | /// Moderate: Return suggestion with adult text but not adult images or videos. |
| | 381 | | /// Strict: Do not return news articles with adult text, images, or videos. If |
| | 382 | | /// the request comes from a market that Bing's adult policy requires that |
| | 383 | | /// safeSearch is set to Strict, Bing ignores the safeSearch value and uses |
| | 384 | | /// Strict. If you use the site: query operator, there is the chance that the |
| | 385 | | /// response may contain adult content regardless of what the safeSearch query |
| | 386 | | /// parameter is set to. Use site: only if you are aware of the content on the |
| | 387 | | /// site and your scenario supports the possibility of adult content. Possible |
| | 388 | | /// values include: 'Off', 'Moderate', 'Strict' |
| | 389 | | /// </param> |
| | 390 | | /// <param name='setLang'> |
| | 391 | | /// The language to use for user interface strings. Specify the language using |
| | 392 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 393 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 394 | | /// always specify the language. Typically, you set setLang to the same |
| | 395 | | /// language specified by mkt unless the user wants the user interface strings |
| | 396 | | /// displayed in a different language. This parameter and the Accept-Language |
| | 397 | | /// header are mutually exclusive; do not specify both. A user interface string |
| | 398 | | /// is a string that's used as a label in a user interface. There are few user |
| | 399 | | /// interface strings in the JSON response objects. Also, any links to Bing.com |
| | 400 | | /// properties in the response objects apply the specified language. |
| | 401 | | /// </param> |
| | 402 | | /// <param name='responseFormat'> |
| | 403 | | /// The media type to use for the response. The following are the possible |
| | 404 | | /// case-insensitive values: JSON, JSONLD. The default is JSON. If you specify |
| | 405 | | /// JSONLD, the response body includes JSON-LD objects that contain the search |
| | 406 | | /// results. |
| | 407 | | /// </param> |
| | 408 | | /// <param name='customHeaders'> |
| | 409 | | /// Headers that will be added to request. |
| | 410 | | /// </param> |
| | 411 | | /// <param name='cancellationToken'> |
| | 412 | | /// The cancellation token. |
| | 413 | | /// </param> |
| | 414 | | /// <exception cref="ErrorResponseException"> |
| | 415 | | /// Thrown when the operation returned an invalid status code |
| | 416 | | /// </exception> |
| | 417 | | /// <exception cref="SerializationException"> |
| | 418 | | /// Thrown when unable to deserialize the response |
| | 419 | | /// </exception> |
| | 420 | | /// <exception cref="ValidationException"> |
| | 421 | | /// Thrown when a required parameter is null |
| | 422 | | /// </exception> |
| | 423 | | /// <exception cref="System.ArgumentNullException"> |
| | 424 | | /// Thrown when a required parameter is null |
| | 425 | | /// </exception> |
| | 426 | | /// <return> |
| | 427 | | /// A response object containing the response body and response headers. |
| | 428 | | /// </return> |
| | 429 | | public async Task<HttpOperationResponse<Suggestions>> AutoSuggestMethodWithHttpMessagesAsync(string query, strin |
| | 430 | | { |
| 2 | 431 | | if (Endpoint == null) |
| | 432 | | { |
| 0 | 433 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint"); |
| | 434 | | } |
| 2 | 435 | | if (query == null) |
| | 436 | | { |
| 0 | 437 | | throw new ValidationException(ValidationRules.CannotBeNull, "query"); |
| | 438 | | } |
| 2 | 439 | | string xBingApisSDK = "true"; |
| | 440 | | // Tracing |
| 2 | 441 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 442 | | string _invocationId = null; |
| 2 | 443 | | if (_shouldTrace) |
| | 444 | | { |
| 0 | 445 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 446 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 447 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| 0 | 448 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| 0 | 449 | | tracingParameters.Add("pragma", pragma); |
| 0 | 450 | | tracingParameters.Add("userAgent", userAgent); |
| 0 | 451 | | tracingParameters.Add("clientId", clientId); |
| 0 | 452 | | tracingParameters.Add("clientIp", clientIp); |
| 0 | 453 | | tracingParameters.Add("location", location); |
| 0 | 454 | | tracingParameters.Add("countryCode", countryCode); |
| 0 | 455 | | tracingParameters.Add("market", market); |
| 0 | 456 | | tracingParameters.Add("query", query); |
| 0 | 457 | | tracingParameters.Add("safeSearch", safeSearch); |
| 0 | 458 | | tracingParameters.Add("setLang", setLang); |
| 0 | 459 | | tracingParameters.Add("responseFormat", responseFormat); |
| 0 | 460 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 461 | | ServiceClientTracing.Enter(_invocationId, this, "AutoSuggestMethod", tracingParameters); |
| | 462 | | } |
| | 463 | | // Construct URL |
| 2 | 464 | | var _baseUrl = BaseUri; |
| 2 | 465 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "Suggestions"; |
| 2 | 466 | | _url = _url.Replace("{Endpoint}", Endpoint); |
| 2 | 467 | | List<string> _queryParameters = new List<string>(); |
| 2 | 468 | | if (countryCode != null) |
| | 469 | | { |
| 0 | 470 | | _queryParameters.Add(string.Format("cc={0}", System.Uri.EscapeDataString(countryCode))); |
| | 471 | | } |
| 2 | 472 | | if (market != null) |
| | 473 | | { |
| 2 | 474 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 475 | | } |
| 2 | 476 | | if (query != null) |
| | 477 | | { |
| 2 | 478 | | _queryParameters.Add(string.Format("q={0}", System.Uri.EscapeDataString(query))); |
| | 479 | | } |
| 2 | 480 | | if (safeSearch != null) |
| | 481 | | { |
| 0 | 482 | | _queryParameters.Add(string.Format("safeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 483 | | } |
| 2 | 484 | | if (setLang != null) |
| | 485 | | { |
| 0 | 486 | | _queryParameters.Add(string.Format("setLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 487 | | } |
| 2 | 488 | | if (responseFormat != null) |
| | 489 | | { |
| 0 | 490 | | _queryParameters.Add(string.Format("ResponseFormat={0}", System.Uri.EscapeDataString(string.Join(",", re |
| | 491 | | } |
| 2 | 492 | | if (_queryParameters.Count > 0) |
| | 493 | | { |
| 2 | 494 | | _url += "?" + string.Join("&", _queryParameters); |
| | 495 | | } |
| | 496 | | // Create HTTP transport objects |
| 2 | 497 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 498 | | HttpResponseMessage _httpResponse = null; |
| 2 | 499 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 500 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 501 | | // Set Headers |
| 2 | 502 | | if (xBingApisSDK != null) |
| | 503 | | { |
| 2 | 504 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 505 | | { |
| 0 | 506 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 507 | | } |
| 2 | 508 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 509 | | } |
| 2 | 510 | | if (acceptLanguage != null) |
| | 511 | | { |
| 0 | 512 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 513 | | { |
| 0 | 514 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 515 | | } |
| 0 | 516 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 517 | | } |
| 2 | 518 | | if (pragma != null) |
| | 519 | | { |
| 0 | 520 | | if (_httpRequest.Headers.Contains("Pragma")) |
| | 521 | | { |
| 0 | 522 | | _httpRequest.Headers.Remove("Pragma"); |
| | 523 | | } |
| 0 | 524 | | _httpRequest.Headers.TryAddWithoutValidation("Pragma", pragma); |
| | 525 | | } |
| 2 | 526 | | if (userAgent != null) |
| | 527 | | { |
| 0 | 528 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 529 | | { |
| 0 | 530 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 531 | | } |
| 0 | 532 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 533 | | } |
| 2 | 534 | | if (clientId != null) |
| | 535 | | { |
| 0 | 536 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 537 | | { |
| 0 | 538 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 539 | | } |
| 0 | 540 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 541 | | } |
| 2 | 542 | | if (clientIp != null) |
| | 543 | | { |
| 0 | 544 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 545 | | { |
| 0 | 546 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 547 | | } |
| 0 | 548 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 549 | | } |
| 2 | 550 | | if (location != null) |
| | 551 | | { |
| 0 | 552 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 553 | | { |
| 0 | 554 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 555 | | } |
| 0 | 556 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 557 | | } |
| | 558 | |
|
| | 559 | |
|
| 2 | 560 | | if (customHeaders != null) |
| | 561 | | { |
| 0 | 562 | | foreach(var _header in customHeaders) |
| | 563 | | { |
| 0 | 564 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 565 | | { |
| 0 | 566 | | _httpRequest.Headers.Remove(_header.Key); |
| | 567 | | } |
| 0 | 568 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 569 | | } |
| | 570 | | } |
| | 571 | |
|
| | 572 | | // Serialize Request |
| 2 | 573 | | string _requestContent = null; |
| | 574 | | // Set Credentials |
| 2 | 575 | | if (Credentials != null) |
| | 576 | | { |
| 2 | 577 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 578 | | await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 579 | | } |
| | 580 | | // Send Request |
| 2 | 581 | | if (_shouldTrace) |
| | 582 | | { |
| 0 | 583 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 584 | | } |
| 2 | 585 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 586 | | _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 587 | | if (_shouldTrace) |
| | 588 | | { |
| 0 | 589 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 590 | | } |
| 2 | 591 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 592 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 593 | | string _responseContent = null; |
| 2 | 594 | | if ((int)_statusCode != 200) |
| | 595 | | { |
| 0 | 596 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 597 | | try |
| | 598 | | { |
| 0 | 599 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 600 | | ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject<ErrorResponse>(_responseContent, Deser |
| 0 | 601 | | if (_errorBody != null) |
| | 602 | | { |
| 0 | 603 | | ex.Body = _errorBody; |
| | 604 | | } |
| 0 | 605 | | } |
| 0 | 606 | | catch (JsonException) |
| | 607 | | { |
| | 608 | | // Ignore the exception |
| 0 | 609 | | } |
| 0 | 610 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 611 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 612 | | if (_shouldTrace) |
| | 613 | | { |
| 0 | 614 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 615 | | } |
| 0 | 616 | | _httpRequest.Dispose(); |
| 0 | 617 | | if (_httpResponse != null) |
| | 618 | | { |
| 0 | 619 | | _httpResponse.Dispose(); |
| | 620 | | } |
| 0 | 621 | | throw ex; |
| | 622 | | } |
| | 623 | | // Create Result |
| 2 | 624 | | var _result = new HttpOperationResponse<Suggestions>(); |
| 2 | 625 | | _result.Request = _httpRequest; |
| 2 | 626 | | _result.Response = _httpResponse; |
| | 627 | | // Deserialize Response |
| 2 | 628 | | if ((int)_statusCode == 200) |
| | 629 | | { |
| 2 | 630 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 631 | | try |
| | 632 | | { |
| 2 | 633 | | _result.Body = SafeJsonConvert.DeserializeObject<Suggestions>(_responseContent, DeserializationSetti |
| 2 | 634 | | } |
| 0 | 635 | | catch (JsonException ex) |
| | 636 | | { |
| 0 | 637 | | _httpRequest.Dispose(); |
| 0 | 638 | | if (_httpResponse != null) |
| | 639 | | { |
| 0 | 640 | | _httpResponse.Dispose(); |
| | 641 | | } |
| 0 | 642 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 643 | | } |
| | 644 | | } |
| 2 | 645 | | if (_shouldTrace) |
| | 646 | | { |
| 0 | 647 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 648 | | } |
| 2 | 649 | | return _result; |
| 2 | 650 | | } |
| | 651 | |
|
| | 652 | | } |
| | 653 | | } |