| | 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.NewsSearch |
| | 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 | | /// NewsOperations operations. |
| | 23 | | /// </summary> |
| | 24 | | public partial class NewsOperations : IServiceOperations<NewsSearchClient>, INewsOperations |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the NewsOperations 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> |
| 6 | 35 | | public NewsOperations(NewsSearchClient client) |
| | 36 | | { |
| 6 | 37 | | if (client == null) |
| | 38 | | { |
| 0 | 39 | | throw new System.ArgumentNullException("client"); |
| | 40 | | } |
| 6 | 41 | | Client = client; |
| 6 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets a reference to the NewsSearchClient |
| | 46 | | /// </summary> |
| 48 | 47 | | public NewsSearchClient Client { get; private set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// The News Search API lets you send a search query to Bing and get back a |
| | 51 | | /// list of news that are relevant to the search query. This section provides |
| | 52 | | /// technical details about the query parameters and headers that you use to |
| | 53 | | /// request news and the JSON response objects that contain them. For examples |
| | 54 | | /// that show how to make requests, see [Searching the web for |
| | 55 | | /// news](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-news-search/search-the-web). |
| | 56 | | /// </summary> |
| | 57 | | /// <param name='query'> |
| | 58 | | /// The user's search query string. The query string cannot be empty. The query |
| | 59 | | /// string may contain [Bing Advanced |
| | 60 | | /// Operators](http://msdn.microsoft.com/library/ff795620.aspx). For example, |
| | 61 | | /// to limit news to a specific domain, use the |
| | 62 | | /// [site:](http://msdn.microsoft.com/library/ff795613.aspx) operator. Use this |
| | 63 | | /// parameter only with the News Search API. Do not specify this parameter when |
| | 64 | | /// calling the Trending Topics API or News Category API. |
| | 65 | | /// </param> |
| | 66 | | /// <param name='acceptLanguage'> |
| | 67 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 68 | | /// strings. The list is in decreasing order of preference. For additional |
| | 69 | | /// information, including expected format, see |
| | 70 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 71 | | /// header and the |
| | 72 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 73 | | /// query parameter are mutually exclusive; do not specify both. If you set |
| | 74 | | /// this header, you must also specify the |
| | 75 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 76 | | /// query parameter. To determine the market to return results for, Bing uses |
| | 77 | | /// the first supported language it finds from the list and combines it with |
| | 78 | | /// the cc parameter value. If the list does not include a supported language, |
| | 79 | | /// Bing finds the closest language and market that supports the request or it |
| | 80 | | /// uses an aggregated or default market for the results. To determine the |
| | 81 | | /// market that Bing used, see the BingAPIs-Market header. Use this header and |
| | 82 | | /// the cc query parameter only if you specify multiple languages. Otherwise, |
| | 83 | | /// use the |
| | 84 | | /// [mkt](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#mkt) |
| | 85 | | /// and |
| | 86 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 87 | | /// query parameters. A user interface string is a string that's used as a |
| | 88 | | /// label in a user interface. There are few user interface strings in the JSON |
| | 89 | | /// response objects. Any links to Bing.com properties in the response objects |
| | 90 | | /// apply the specified language. |
| | 91 | | /// </param> |
| | 92 | | /// <param name='userAgent'> |
| | 93 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 94 | | /// mobile users with an optimized experience. Although optional, you are |
| | 95 | | /// encouraged to always specify this header. The user-agent should be the same |
| | 96 | | /// string that any commonly used browser sends. For information about user |
| | 97 | | /// agents, see [RFC |
| | 98 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). The |
| | 99 | | /// following are examples of user-agent strings. Windows Phone: Mozilla/5.0 |
| | 100 | | /// (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; |
| | 101 | | /// Touch; NOKIA; Lumia 822). Android: Mozilla / 5.0 (Linux; U; Android 2.3.5; |
| | 102 | | /// en - us; SCH - I500 Build / GINGERBREAD) AppleWebKit / 533.1 (KHTML; like |
| | 103 | | /// Gecko) Version / 4.0 Mobile Safari / 533.1. iPhone: Mozilla / 5.0 (iPhone; |
| | 104 | | /// CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 536.26 (KHTML; like Gecko) |
| | 105 | | /// Mobile / 10B142 iPhone4; 1 BingWeb / 3.03.1428.20120423. PC: Mozilla / 5.0 |
| | 106 | | /// (Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0) like Gecko. iPad: |
| | 107 | | /// Mozilla / 5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit / 537.51.1 |
| | 108 | | /// (KHTML, like Gecko) Version / 7.0 Mobile / 11A465 Safari / 9537.53 |
| | 109 | | /// </param> |
| | 110 | | /// <param name='clientId'> |
| | 111 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 112 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 113 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 114 | | /// do not use the same client ID for a user across multiple requests, then |
| | 115 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 116 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 117 | | /// For example, if the second request has a different flight assignment than |
| | 118 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 119 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 120 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 121 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 122 | | /// relevance improvements help with better quality of results delivered by |
| | 123 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 124 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 125 | | /// required. Persisting the client ID across multiple requests for the same |
| | 126 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 127 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 128 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 129 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 130 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 131 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 132 | | /// include this header in a request is the first time the user uses your app |
| | 133 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 134 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 135 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 136 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 137 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 138 | | /// next time the user uses your app on that device, get the client ID that you |
| | 139 | | /// persisted. Bing responses may or may not include this header. If the |
| | 140 | | /// response includes this header, capture the client ID and use it for all |
| | 141 | | /// subsequent Bing requests for the user on that device. If you include the |
| | 142 | | /// X-MSEdge-ClientID, you must not include cookies in the request. |
| | 143 | | /// </param> |
| | 144 | | /// <param name='clientIp'> |
| | 145 | | /// The IPv4 or IPv6 address of the client device. The IP address is used to |
| | 146 | | /// discover the user's location. Bing uses the location information to |
| | 147 | | /// determine safe search behavior. Although optional, you are encouraged to |
| | 148 | | /// always specify this header and the X-Search-Location header. Do not |
| | 149 | | /// obfuscate the address (for example, by changing the last octet to 0). |
| | 150 | | /// Obfuscating the address results in the location not being anywhere near the |
| | 151 | | /// device's actual location, which may result in Bing serving erroneous |
| | 152 | | /// results. |
| | 153 | | /// </param> |
| | 154 | | /// <param name='location'> |
| | 155 | | /// A semicolon-delimited list of key/value pairs that describe the client's |
| | 156 | | /// geographical location. Bing uses the location information to determine safe |
| | 157 | | /// search behavior and to return relevant local content. Specify the key/value |
| | 158 | | /// pair as <key>:<value>. The following are the keys that you use |
| | 159 | | /// to specify the user's location. lat (required): The latitude of the |
| | 160 | | /// client's location, in degrees. The latitude must be greater than or equal |
| | 161 | | /// to -90.0 and less than or equal to +90.0. Negative values indicate southern |
| | 162 | | /// latitudes and positive values indicate northern latitudes. long (required): |
| | 163 | | /// The longitude of the client's location, in degrees. The longitude must be |
| | 164 | | /// greater than or equal to -180.0 and less than or equal to +180.0. Negative |
| | 165 | | /// values indicate western longitudes and positive values indicate eastern |
| | 166 | | /// longitudes. re (required): The radius, in meters, which specifies the |
| | 167 | | /// horizontal accuracy of the coordinates. Pass the value returned by the |
| | 168 | | /// device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m |
| | 169 | | /// for cell tower triangulation, and 18,000m for reverse IP lookup. ts |
| | 170 | | /// (optional): The UTC UNIX timestamp of when the client was at the location. |
| | 171 | | /// (The UNIX timestamp is the number of seconds since January 1, 1970.) head |
| | 172 | | /// (optional): The client's relative heading or direction of travel. Specify |
| | 173 | | /// the direction of travel as degrees from 0 through 360, counting clockwise |
| | 174 | | /// relative to true north. Specify this key only if the sp key is nonzero. sp |
| | 175 | | /// (optional): The horizontal velocity (speed), in meters per second, that the |
| | 176 | | /// client device is traveling. alt (optional): The altitude of the client |
| | 177 | | /// device, in meters. are (optional): The radius, in meters, that specifies |
| | 178 | | /// the vertical accuracy of the coordinates. Specify this key only if you |
| | 179 | | /// specify the alt key. Although many of the keys are optional, the more |
| | 180 | | /// information that you provide, the more accurate the location results are. |
| | 181 | | /// Although optional, you are encouraged to always specify the user's |
| | 182 | | /// geographical location. Providing the location is especially important if |
| | 183 | | /// the client's IP address does not accurately reflect the user's physical |
| | 184 | | /// location (for example, if the client uses VPN). For optimal results, you |
| | 185 | | /// should include this header and the X-MSEdge-ClientIP header, but at a |
| | 186 | | /// minimum, you should include this header. |
| | 187 | | /// </param> |
| | 188 | | /// <param name='countryCode'> |
| | 189 | | /// A 2-character country code of the country where the results come from. This |
| | 190 | | /// API supports only the United States market. If you specify this query |
| | 191 | | /// parameter, it must be set to us. If you set this parameter, you must also |
| | 192 | | /// specify the Accept-Language header. Bing uses the first supported language |
| | 193 | | /// it finds from the languages list, and combine that language with the |
| | 194 | | /// country code that you specify to determine the market to return results |
| | 195 | | /// for. If the languages list does not include a supported language, Bing |
| | 196 | | /// finds the closest language and market that supports the request, or it may |
| | 197 | | /// use an aggregated or default market for the results instead of a specified |
| | 198 | | /// one. You should use this query parameter and the Accept-Language query |
| | 199 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 200 | | /// the mkt and setLang query parameters. This parameter and the mkt query |
| | 201 | | /// parameter are mutually exclusive—do not specify both. |
| | 202 | | /// </param> |
| | 203 | | /// <param name='count'> |
| | 204 | | /// The number of news articles to return in the response. The actual number |
| | 205 | | /// delivered may be less than requested. The default is 10 and the maximum |
| | 206 | | /// value is 100. The actual number delivered may be less than requested.You |
| | 207 | | /// may use this parameter along with the offset parameter to page results. For |
| | 208 | | /// example, if your user interface displays 20 articles per page, set count to |
| | 209 | | /// 20 and offset to 0 to get the first page of results. For each subsequent |
| | 210 | | /// page, increment offset by 20 (for example, 0, 20, 40). It is possible for |
| | 211 | | /// multiple pages to include some overlap in results. |
| | 212 | | /// </param> |
| | 213 | | /// <param name='freshness'> |
| | 214 | | /// Filter news by the date and time that Bing discovered the news. The |
| | 215 | | /// following are the possible filter values. Day: Return news discovered |
| | 216 | | /// within the last 24 hours. Week: Return news discovered within the last 7 |
| | 217 | | /// days. Month: Return news discovered within the last 30 days. Use this |
| | 218 | | /// parameter only with the News Search API. Do not specify this parameter when |
| | 219 | | /// calling the News Category API or the Trending Topics API. Possible values |
| | 220 | | /// include: 'Day', 'Week', 'Month' |
| | 221 | | /// </param> |
| | 222 | | /// <param name='market'> |
| | 223 | | /// The market where the results come from. Typically, mkt is the country where |
| | 224 | | /// the user is making the request from. However, it could be a different |
| | 225 | | /// country if the user is not located in a country where Bing delivers |
| | 226 | | /// results. The market must be in the form <language code>-<country |
| | 227 | | /// code>. For example, en-US. The string is case insensitive. For a list of |
| | 228 | | /// possible market values, see [Market |
| | 229 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes). |
| | 230 | | /// NOTE: If known, you are encouraged to always specify the market. Specifying |
| | 231 | | /// the market helps Bing route the request and return an appropriate and |
| | 232 | | /// optimal response. If you specify a market that is not listed in [Market |
| | 233 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes), |
| | 234 | | /// Bing uses a best fit market code based on an internal mapping that is |
| | 235 | | /// subject to change. This parameter and the |
| | 236 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 237 | | /// query parameter are mutually exclusive—do not specify both. |
| | 238 | | /// </param> |
| | 239 | | /// <param name='offset'> |
| | 240 | | /// The zero-based offset that indicates the number of news to skip before |
| | 241 | | /// returning news. The default is 0. The offset should be less than |
| | 242 | | /// ([totalEstimatedMatches](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-refere |
| | 243 | | /// - count). Use this parameter along with the count parameter to page |
| | 244 | | /// results. For example, if your user interface displays 20 news per page, set |
| | 245 | | /// count to 20 and offset to 0 to get the first page of results. For each |
| | 246 | | /// subsequent page, increment offset by 20 (for example, 0, 20, 40). It is |
| | 247 | | /// possible for multiple pages to include some overlap in results. |
| | 248 | | /// </param> |
| | 249 | | /// <param name='originalImage'> |
| | 250 | | /// A Boolean value that determines whether the image's contentUrl contains a |
| | 251 | | /// URL that points to a thumbnail of the original article's image or the image |
| | 252 | | /// itself. If the article includes an image, and this parameter is set to |
| | 253 | | /// true, the image's contentUrl property contains a URL that you may use to |
| | 254 | | /// download the original image from the publisher's website. Otherwise, if |
| | 255 | | /// this parameter is false, the image's contentUrl and thumbnailUrl URLs both |
| | 256 | | /// point to the same thumbnail image. Use this parameter only with the News |
| | 257 | | /// Search API. Do not specify this parameter when calling the Trending Topics |
| | 258 | | /// API or News Category API. |
| | 259 | | /// </param> |
| | 260 | | /// <param name='safeSearch'> |
| | 261 | | /// Filter news for adult content. The following are the possible filter |
| | 262 | | /// values. Off: Return news articles with adult text, images, or videos. |
| | 263 | | /// Moderate: Return news articles with adult text but not adult images or |
| | 264 | | /// videos. Strict: Do not return news articles with adult text, images, or |
| | 265 | | /// videos. If the request comes from a market that Bing's adult policy |
| | 266 | | /// requires that safeSearch is set to Strict, Bing ignores the safeSearch |
| | 267 | | /// value and uses Strict. If you use the site: query operator, there is the |
| | 268 | | /// chance that the response may contain adult content regardless of what the |
| | 269 | | /// safeSearch query parameter is set to. Use site: only if you are aware of |
| | 270 | | /// the content on the site and your scenario supports the possibility of adult |
| | 271 | | /// content. Possible values include: 'Off', 'Moderate', 'Strict' |
| | 272 | | /// </param> |
| | 273 | | /// <param name='setLang'> |
| | 274 | | /// The language to use for user interface strings. Specify the language using |
| | 275 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 276 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 277 | | /// always specify the language. Typically, you set setLang to the same |
| | 278 | | /// language specified by mkt unless the user wants the user interface strings |
| | 279 | | /// displayed in a different language. This parameter and the |
| | 280 | | /// [Accept-Language](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#acc |
| | 281 | | /// header are mutually exclusive; do not specify both. A user interface string |
| | 282 | | /// is a string that's used as a label in a user interface. There are few user |
| | 283 | | /// interface strings in the JSON response objects. Also, any links to Bing.com |
| | 284 | | /// properties in the response objects apply the specified language. |
| | 285 | | /// </param> |
| | 286 | | /// <param name='sortBy'> |
| | 287 | | /// The order to return the news in. The following are the possible |
| | 288 | | /// case-insensitive values. Date: If the request is through the News Search |
| | 289 | | /// API, the response returns news articles sorted by date from the most recent |
| | 290 | | /// to the oldest. If the request is through the News Trending Topics API, the |
| | 291 | | /// response returns trending topics sorted by date from the most recent to the |
| | 292 | | /// oldest. |
| | 293 | | /// </param> |
| | 294 | | /// <param name='textDecorations'> |
| | 295 | | /// A Boolean value that determines whether display strings contain decoration |
| | 296 | | /// markers such as hit highlighting characters. If true, the strings may |
| | 297 | | /// include markers. The default is false. To specify whether to use Unicode |
| | 298 | | /// characters or HTML tags as the markers, see the |
| | 299 | | /// [textFormat](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#textform |
| | 300 | | /// query parameter. For information about hit highlighting, see [Hit |
| | 301 | | /// Highlighting](https://docs.microsoft.com/azure/cognitive-services/bing-news-search/hit-highlighting). |
| | 302 | | /// </param> |
| | 303 | | /// <param name='textFormat'> |
| | 304 | | /// The type of markers to use for text decorations (see the textDecorations |
| | 305 | | /// query parameter). Possible values are Raw—Use Unicode characters to mark |
| | 306 | | /// content that needs special formatting. The Unicode characters are in the |
| | 307 | | /// range E000 through E019. For example, Bing uses E000 and E001 to mark the |
| | 308 | | /// beginning and end of query terms for hit highlighting. HTML—Use HTML tags |
| | 309 | | /// to mark content that needs special formatting. For example, use <b> |
| | 310 | | /// tags to highlight query terms in display strings. The default is Raw. For |
| | 311 | | /// display strings that contain escapable HTML characters such as <, >, |
| | 312 | | /// and &, if textFormat is set to HTML, Bing escapes the characters as |
| | 313 | | /// appropriate (for example, < is escaped to &lt;). Possible values |
| | 314 | | /// include: 'Raw', 'Html' |
| | 315 | | /// </param> |
| | 316 | | /// <param name='customHeaders'> |
| | 317 | | /// Headers that will be added to request. |
| | 318 | | /// </param> |
| | 319 | | /// <param name='cancellationToken'> |
| | 320 | | /// The cancellation token. |
| | 321 | | /// </param> |
| | 322 | | /// <exception cref="ErrorResponseException"> |
| | 323 | | /// Thrown when the operation returned an invalid status code |
| | 324 | | /// </exception> |
| | 325 | | /// <exception cref="SerializationException"> |
| | 326 | | /// Thrown when unable to deserialize the response |
| | 327 | | /// </exception> |
| | 328 | | /// <exception cref="ValidationException"> |
| | 329 | | /// Thrown when a required parameter is null |
| | 330 | | /// </exception> |
| | 331 | | /// <exception cref="System.ArgumentNullException"> |
| | 332 | | /// Thrown when a required parameter is null |
| | 333 | | /// </exception> |
| | 334 | | /// <return> |
| | 335 | | /// A response object containing the response body and response headers. |
| | 336 | | /// </return> |
| | 337 | | public async Task<HttpOperationResponse<News>> SearchWithHttpMessagesAsync(string query, string acceptLanguage = |
| | 338 | | { |
| 2 | 339 | | if (Client.Endpoint == null) |
| | 340 | | { |
| 0 | 341 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 342 | | } |
| 2 | 343 | | if (query == null) |
| | 344 | | { |
| 0 | 345 | | throw new ValidationException(ValidationRules.CannotBeNull, "query"); |
| | 346 | | } |
| 2 | 347 | | string xBingApisSDK = "true"; |
| | 348 | | // Tracing |
| 2 | 349 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 350 | | string _invocationId = null; |
| 2 | 351 | | if (_shouldTrace) |
| | 352 | | { |
| 0 | 353 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 354 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 355 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| 0 | 356 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| 0 | 357 | | tracingParameters.Add("userAgent", userAgent); |
| 0 | 358 | | tracingParameters.Add("clientId", clientId); |
| 0 | 359 | | tracingParameters.Add("clientIp", clientIp); |
| 0 | 360 | | tracingParameters.Add("location", location); |
| 0 | 361 | | tracingParameters.Add("countryCode", countryCode); |
| 0 | 362 | | tracingParameters.Add("count", count); |
| 0 | 363 | | tracingParameters.Add("freshness", freshness); |
| 0 | 364 | | tracingParameters.Add("market", market); |
| 0 | 365 | | tracingParameters.Add("offset", offset); |
| 0 | 366 | | tracingParameters.Add("originalImage", originalImage); |
| 0 | 367 | | tracingParameters.Add("query", query); |
| 0 | 368 | | tracingParameters.Add("safeSearch", safeSearch); |
| 0 | 369 | | tracingParameters.Add("setLang", setLang); |
| 0 | 370 | | tracingParameters.Add("sortBy", sortBy); |
| 0 | 371 | | tracingParameters.Add("textDecorations", textDecorations); |
| 0 | 372 | | tracingParameters.Add("textFormat", textFormat); |
| 0 | 373 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 374 | | ServiceClientTracing.Enter(_invocationId, this, "Search", tracingParameters); |
| | 375 | | } |
| | 376 | | // Construct URL |
| 2 | 377 | | var _baseUrl = Client.BaseUri; |
| 2 | 378 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "news/search"; |
| 2 | 379 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 380 | | List<string> _queryParameters = new List<string>(); |
| 2 | 381 | | if (countryCode != null) |
| | 382 | | { |
| 0 | 383 | | _queryParameters.Add(string.Format("cc={0}", System.Uri.EscapeDataString(countryCode))); |
| | 384 | | } |
| 2 | 385 | | if (count != null) |
| | 386 | | { |
| 0 | 387 | | _queryParameters.Add(string.Format("count={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonC |
| | 388 | | } |
| 2 | 389 | | if (freshness != null) |
| | 390 | | { |
| 0 | 391 | | _queryParameters.Add(string.Format("freshness={0}", System.Uri.EscapeDataString(freshness))); |
| | 392 | | } |
| 2 | 393 | | if (market != null) |
| | 394 | | { |
| 0 | 395 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 396 | | } |
| 2 | 397 | | if (offset != null) |
| | 398 | | { |
| 0 | 399 | | _queryParameters.Add(string.Format("offset={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJson |
| | 400 | | } |
| 2 | 401 | | if (originalImage != null) |
| | 402 | | { |
| 0 | 403 | | _queryParameters.Add(string.Format("originalImg={0}", System.Uri.EscapeDataString(Rest.Serialization.Saf |
| | 404 | | } |
| 2 | 405 | | if (query != null) |
| | 406 | | { |
| 2 | 407 | | _queryParameters.Add(string.Format("q={0}", System.Uri.EscapeDataString(query))); |
| | 408 | | } |
| 2 | 409 | | if (safeSearch != null) |
| | 410 | | { |
| 0 | 411 | | _queryParameters.Add(string.Format("safeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 412 | | } |
| 2 | 413 | | if (setLang != null) |
| | 414 | | { |
| 0 | 415 | | _queryParameters.Add(string.Format("setLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 416 | | } |
| 2 | 417 | | if (sortBy != null) |
| | 418 | | { |
| 0 | 419 | | _queryParameters.Add(string.Format("sortBy={0}", System.Uri.EscapeDataString(sortBy))); |
| | 420 | | } |
| 2 | 421 | | if (textDecorations != null) |
| | 422 | | { |
| 0 | 423 | | _queryParameters.Add(string.Format("textDecorations={0}", System.Uri.EscapeDataString(Rest.Serialization |
| | 424 | | } |
| 2 | 425 | | if (textFormat != null) |
| | 426 | | { |
| 0 | 427 | | _queryParameters.Add(string.Format("textFormat={0}", System.Uri.EscapeDataString(textFormat))); |
| | 428 | | } |
| 2 | 429 | | if (_queryParameters.Count > 0) |
| | 430 | | { |
| 2 | 431 | | _url += "?" + string.Join("&", _queryParameters); |
| | 432 | | } |
| | 433 | | // Create HTTP transport objects |
| 2 | 434 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 435 | | HttpResponseMessage _httpResponse = null; |
| 2 | 436 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 437 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 438 | | // Set Headers |
| 2 | 439 | | if (xBingApisSDK != null) |
| | 440 | | { |
| 2 | 441 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 442 | | { |
| 0 | 443 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 444 | | } |
| 2 | 445 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 446 | | } |
| 2 | 447 | | if (acceptLanguage != null) |
| | 448 | | { |
| 0 | 449 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 450 | | { |
| 0 | 451 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 452 | | } |
| 0 | 453 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 454 | | } |
| 2 | 455 | | if (userAgent != null) |
| | 456 | | { |
| 0 | 457 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 458 | | { |
| 0 | 459 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 460 | | } |
| 0 | 461 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 462 | | } |
| 2 | 463 | | if (clientId != null) |
| | 464 | | { |
| 0 | 465 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 466 | | { |
| 0 | 467 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 468 | | } |
| 0 | 469 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 470 | | } |
| 2 | 471 | | if (clientIp != null) |
| | 472 | | { |
| 0 | 473 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 474 | | { |
| 0 | 475 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 476 | | } |
| 0 | 477 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 478 | | } |
| 2 | 479 | | if (location != null) |
| | 480 | | { |
| 0 | 481 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 482 | | { |
| 0 | 483 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 484 | | } |
| 0 | 485 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 486 | | } |
| | 487 | |
|
| | 488 | |
|
| 2 | 489 | | if (customHeaders != null) |
| | 490 | | { |
| 0 | 491 | | foreach(var _header in customHeaders) |
| | 492 | | { |
| 0 | 493 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 494 | | { |
| 0 | 495 | | _httpRequest.Headers.Remove(_header.Key); |
| | 496 | | } |
| 0 | 497 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 498 | | } |
| | 499 | | } |
| | 500 | |
|
| | 501 | | // Serialize Request |
| 2 | 502 | | string _requestContent = null; |
| | 503 | | // Set Credentials |
| 2 | 504 | | if (Client.Credentials != null) |
| | 505 | | { |
| 2 | 506 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 507 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 508 | | } |
| | 509 | | // Send Request |
| 2 | 510 | | if (_shouldTrace) |
| | 511 | | { |
| 0 | 512 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 513 | | } |
| 2 | 514 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 515 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 516 | | if (_shouldTrace) |
| | 517 | | { |
| 0 | 518 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 519 | | } |
| 2 | 520 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 521 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 522 | | string _responseContent = null; |
| 2 | 523 | | if ((int)_statusCode != 200) |
| | 524 | | { |
| 0 | 525 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 526 | | try |
| | 527 | | { |
| 0 | 528 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 529 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 530 | | if (_errorBody != null) |
| | 531 | | { |
| 0 | 532 | | ex.Body = _errorBody; |
| | 533 | | } |
| 0 | 534 | | } |
| 0 | 535 | | catch (JsonException) |
| | 536 | | { |
| | 537 | | // Ignore the exception |
| 0 | 538 | | } |
| 0 | 539 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 540 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 541 | | if (_shouldTrace) |
| | 542 | | { |
| 0 | 543 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 544 | | } |
| 0 | 545 | | _httpRequest.Dispose(); |
| 0 | 546 | | if (_httpResponse != null) |
| | 547 | | { |
| 0 | 548 | | _httpResponse.Dispose(); |
| | 549 | | } |
| 0 | 550 | | throw ex; |
| | 551 | | } |
| | 552 | | // Create Result |
| 2 | 553 | | var _result = new HttpOperationResponse<News>(); |
| 2 | 554 | | _result.Request = _httpRequest; |
| 2 | 555 | | _result.Response = _httpResponse; |
| | 556 | | // Deserialize Response |
| 2 | 557 | | if ((int)_statusCode == 200) |
| | 558 | | { |
| 2 | 559 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 560 | | try |
| | 561 | | { |
| 2 | 562 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<News>(_responseContent, Client.D |
| 2 | 563 | | } |
| 0 | 564 | | catch (JsonException ex) |
| | 565 | | { |
| 0 | 566 | | _httpRequest.Dispose(); |
| 0 | 567 | | if (_httpResponse != null) |
| | 568 | | { |
| 0 | 569 | | _httpResponse.Dispose(); |
| | 570 | | } |
| 0 | 571 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 572 | | } |
| | 573 | | } |
| 2 | 574 | | if (_shouldTrace) |
| | 575 | | { |
| 0 | 576 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 577 | | } |
| 2 | 578 | | return _result; |
| 2 | 579 | | } |
| | 580 | |
|
| | 581 | | /// <summary> |
| | 582 | | /// The News Category API lets lets you search on Bing and get back a list of |
| | 583 | | /// top news articles by category. This section provides technical details |
| | 584 | | /// about the query parameters and headers that you use to request news and the |
| | 585 | | /// JSON response objects that contain them. For examples that show how to |
| | 586 | | /// make requests, see [Searching the web for |
| | 587 | | /// news](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-news-search/search-the-web). |
| | 588 | | /// </summary> |
| | 589 | | /// <param name='acceptLanguage'> |
| | 590 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 591 | | /// strings. The list is in decreasing order of preference. For additional |
| | 592 | | /// information, including expected format, see |
| | 593 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 594 | | /// header and the |
| | 595 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 596 | | /// query parameter are mutually exclusive; do not specify both. If you set |
| | 597 | | /// this header, you must also specify the |
| | 598 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 599 | | /// query parameter. To determine the market to return results for, Bing uses |
| | 600 | | /// the first supported language it finds from the list and combines it with |
| | 601 | | /// the cc parameter value. If the list does not include a supported language, |
| | 602 | | /// Bing finds the closest language and market that supports the request or it |
| | 603 | | /// uses an aggregated or default market for the results. To determine the |
| | 604 | | /// market that Bing used, see the BingAPIs-Market header. Use this header and |
| | 605 | | /// the cc query parameter only if you specify multiple languages. Otherwise, |
| | 606 | | /// use the |
| | 607 | | /// [mkt](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#mkt) |
| | 608 | | /// and |
| | 609 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 610 | | /// query parameters. A user interface string is a string that's used as a |
| | 611 | | /// label in a user interface. There are few user interface strings in the JSON |
| | 612 | | /// response objects. Any links to Bing.com properties in the response objects |
| | 613 | | /// apply the specified language. |
| | 614 | | /// </param> |
| | 615 | | /// <param name='userAgent'> |
| | 616 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 617 | | /// mobile users with an optimized experience. Although optional, you are |
| | 618 | | /// encouraged to always specify this header. The user-agent should be the same |
| | 619 | | /// string that any commonly used browser sends. For information about user |
| | 620 | | /// agents, see [RFC |
| | 621 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). The |
| | 622 | | /// following are examples of user-agent strings. Windows Phone: Mozilla/5.0 |
| | 623 | | /// (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; |
| | 624 | | /// Touch; NOKIA; Lumia 822). Android: Mozilla / 5.0 (Linux; U; Android 2.3.5; |
| | 625 | | /// en - us; SCH - I500 Build / GINGERBREAD) AppleWebKit / 533.1 (KHTML; like |
| | 626 | | /// Gecko) Version / 4.0 Mobile Safari / 533.1. iPhone: Mozilla / 5.0 (iPhone; |
| | 627 | | /// CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 536.26 (KHTML; like Gecko) |
| | 628 | | /// Mobile / 10B142 iPhone4; 1 BingWeb / 3.03.1428.20120423. PC: Mozilla / 5.0 |
| | 629 | | /// (Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0) like Gecko. iPad: |
| | 630 | | /// Mozilla / 5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit / 537.51.1 |
| | 631 | | /// (KHTML, like Gecko) Version / 7.0 Mobile / 11A465 Safari / 9537.53 |
| | 632 | | /// </param> |
| | 633 | | /// <param name='clientId'> |
| | 634 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 635 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 636 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 637 | | /// do not use the same client ID for a user across multiple requests, then |
| | 638 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 639 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 640 | | /// For example, if the second request has a different flight assignment than |
| | 641 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 642 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 643 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 644 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 645 | | /// relevance improvements help with better quality of results delivered by |
| | 646 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 647 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 648 | | /// required. Persisting the client ID across multiple requests for the same |
| | 649 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 650 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 651 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 652 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 653 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 654 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 655 | | /// include this header in a request is the first time the user uses your app |
| | 656 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 657 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 658 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 659 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 660 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 661 | | /// next time the user uses your app on that device, get the client ID that you |
| | 662 | | /// persisted. Bing responses may or may not include this header. If the |
| | 663 | | /// response includes this header, capture the client ID and use it for all |
| | 664 | | /// subsequent Bing requests for the user on that device. If you include the |
| | 665 | | /// X-MSEdge-ClientID, you must not include cookies in the request. |
| | 666 | | /// </param> |
| | 667 | | /// <param name='clientIp'> |
| | 668 | | /// The IPv4 or IPv6 address of the client device. The IP address is used to |
| | 669 | | /// discover the user's location. Bing uses the location information to |
| | 670 | | /// determine safe search behavior. Although optional, you are encouraged to |
| | 671 | | /// always specify this header and the X-Search-Location header. Do not |
| | 672 | | /// obfuscate the address (for example, by changing the last octet to 0). |
| | 673 | | /// Obfuscating the address results in the location not being anywhere near the |
| | 674 | | /// device's actual location, which may result in Bing serving erroneous |
| | 675 | | /// results. |
| | 676 | | /// </param> |
| | 677 | | /// <param name='location'> |
| | 678 | | /// A semicolon-delimited list of key/value pairs that describe the client's |
| | 679 | | /// geographical location. Bing uses the location information to determine safe |
| | 680 | | /// search behavior and to return relevant local content. Specify the key/value |
| | 681 | | /// pair as <key>:<value>. The following are the keys that you use |
| | 682 | | /// to specify the user's location. lat (required): The latitude of the |
| | 683 | | /// client's location, in degrees. The latitude must be greater than or equal |
| | 684 | | /// to -90.0 and less than or equal to +90.0. Negative values indicate southern |
| | 685 | | /// latitudes and positive values indicate northern latitudes. long (required): |
| | 686 | | /// The longitude of the client's location, in degrees. The longitude must be |
| | 687 | | /// greater than or equal to -180.0 and less than or equal to +180.0. Negative |
| | 688 | | /// values indicate western longitudes and positive values indicate eastern |
| | 689 | | /// longitudes. re (required): The radius, in meters, which specifies the |
| | 690 | | /// horizontal accuracy of the coordinates. Pass the value returned by the |
| | 691 | | /// device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m |
| | 692 | | /// for cell tower triangulation, and 18,000m for reverse IP lookup. ts |
| | 693 | | /// (optional): The UTC UNIX timestamp of when the client was at the location. |
| | 694 | | /// (The UNIX timestamp is the number of seconds since January 1, 1970.) head |
| | 695 | | /// (optional): The client's relative heading or direction of travel. Specify |
| | 696 | | /// the direction of travel as degrees from 0 through 360, counting clockwise |
| | 697 | | /// relative to true north. Specify this key only if the sp key is nonzero. sp |
| | 698 | | /// (optional): The horizontal velocity (speed), in meters per second, that the |
| | 699 | | /// client device is traveling. alt (optional): The altitude of the client |
| | 700 | | /// device, in meters. are (optional): The radius, in meters, that specifies |
| | 701 | | /// the vertical accuracy of the coordinates. Specify this key only if you |
| | 702 | | /// specify the alt key. Although many of the keys are optional, the more |
| | 703 | | /// information that you provide, the more accurate the location results are. |
| | 704 | | /// Although optional, you are encouraged to always specify the user's |
| | 705 | | /// geographical location. Providing the location is especially important if |
| | 706 | | /// the client's IP address does not accurately reflect the user's physical |
| | 707 | | /// location (for example, if the client uses VPN). For optimal results, you |
| | 708 | | /// should include this header and the X-MSEdge-ClientIP header, but at a |
| | 709 | | /// minimum, you should include this header. |
| | 710 | | /// </param> |
| | 711 | | /// <param name='countryCode'> |
| | 712 | | /// A 2-character country code of the country where the results come from. This |
| | 713 | | /// API supports only the United States market. If you specify this query |
| | 714 | | /// parameter, it must be set to us. If you set this parameter, you must also |
| | 715 | | /// specify the Accept-Language header. Bing uses the first supported language |
| | 716 | | /// it finds from the languages list, and combine that language with the |
| | 717 | | /// country code that you specify to determine the market to return results |
| | 718 | | /// for. If the languages list does not include a supported language, Bing |
| | 719 | | /// finds the closest language and market that supports the request, or it may |
| | 720 | | /// use an aggregated or default market for the results instead of a specified |
| | 721 | | /// one. You should use this query parameter and the Accept-Language query |
| | 722 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 723 | | /// the mkt and setLang query parameters. This parameter and the mkt query |
| | 724 | | /// parameter are mutually exclusive—do not specify both. |
| | 725 | | /// </param> |
| | 726 | | /// <param name='category'> |
| | 727 | | /// The category of articles to return. For example, Sports articles or |
| | 728 | | /// Entertainment articles. For a list of possible categories, see [News |
| | 729 | | /// Categories by |
| | 730 | | /// Market](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#categories-by |
| | 731 | | /// Use this parameter only with News Category API. If you do not specify this |
| | 732 | | /// parameter, the response includes both: Headline articles typically |
| | 733 | | /// published in the last 24 hours from any category and articles from each |
| | 734 | | /// parent category (up to four articles). If the article is a headline, the |
| | 735 | | /// article's headline field is set to true. By default, the response includes |
| | 736 | | /// up to 12 headline articles. To specify the number of headline articles to |
| | 737 | | /// return, set the |
| | 738 | | /// [headlineCount](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#headl |
| | 739 | | /// query parameter. |
| | 740 | | /// </param> |
| | 741 | | /// <param name='count'> |
| | 742 | | /// The number of news articles to return in the response. The actual number |
| | 743 | | /// delivered may be less than requested. The default is 10 and the maximum |
| | 744 | | /// value is 100. The actual number delivered may be less than requested.You |
| | 745 | | /// may use this parameter along with the offset parameter to page results. For |
| | 746 | | /// example, if your user interface displays 20 articles per page, set count to |
| | 747 | | /// 20 and offset to 0 to get the first page of results. For each subsequent |
| | 748 | | /// page, increment offset by 20 (for example, 0, 20, 40). It is possible for |
| | 749 | | /// multiple pages to include some overlap in results. If you do not specify |
| | 750 | | /// the |
| | 751 | | /// [category](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#category) |
| | 752 | | /// parameter, Bing ignores this parameter. |
| | 753 | | /// </param> |
| | 754 | | /// <param name='headlineCount'> |
| | 755 | | /// The number of headline articles to return in the response. The default is |
| | 756 | | /// 12. Specify this parameter only if you do not specify the |
| | 757 | | /// [category](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#category) |
| | 758 | | /// parameter. |
| | 759 | | /// </param> |
| | 760 | | /// <param name='market'> |
| | 761 | | /// The market where the results come from. Typically, mkt is the country where |
| | 762 | | /// the user is making the request from. However, it could be a different |
| | 763 | | /// country if the user is not located in a country where Bing delivers |
| | 764 | | /// results. The market must be in the form <language code>-<country |
| | 765 | | /// code>. For example, en-US. The string is case insensitive. For a list of |
| | 766 | | /// possible market values, see [Market |
| | 767 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes). |
| | 768 | | /// NOTE: If known, you are encouraged to always specify the market. Specifying |
| | 769 | | /// the market helps Bing route the request and return an appropriate and |
| | 770 | | /// optimal response. If you specify a market that is not listed in [Market |
| | 771 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes), |
| | 772 | | /// Bing uses a best fit market code based on an internal mapping that is |
| | 773 | | /// subject to change. This parameter and the |
| | 774 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 775 | | /// query parameter are mutually exclusive—do not specify both. |
| | 776 | | /// </param> |
| | 777 | | /// <param name='offset'> |
| | 778 | | /// The zero-based offset that indicates the number of news to skip before |
| | 779 | | /// returning news. The default is 0. The offset should be less than |
| | 780 | | /// ([totalEstimatedMatches](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-refere |
| | 781 | | /// - count). Use this parameter along with the count parameter to page |
| | 782 | | /// results. For example, if your user interface displays 20 news per page, set |
| | 783 | | /// count to 20 and offset to 0 to get the first page of results. For each |
| | 784 | | /// subsequent page, increment offset by 20 (for example, 0, 20, 40). It is |
| | 785 | | /// possible for multiple pages to include some overlap in results. If you do |
| | 786 | | /// not specify the |
| | 787 | | /// [category](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#category) |
| | 788 | | /// parameter, Bing ignores this parameter. |
| | 789 | | /// </param> |
| | 790 | | /// <param name='originalImage'> |
| | 791 | | /// A Boolean value that determines whether the image's contentUrl contains a |
| | 792 | | /// URL that points to a thumbnail of the original article's image or the image |
| | 793 | | /// itself. If the article includes an image, and this parameter is set to |
| | 794 | | /// true, the image's contentUrl property contains a URL that you may use to |
| | 795 | | /// download the original image from the publisher's website. Otherwise, if |
| | 796 | | /// this parameter is false, the image's contentUrl and thumbnailUrl URLs both |
| | 797 | | /// point to the same thumbnail image. Use this parameter only with the News |
| | 798 | | /// Search API or News Category API. Trending Topics API ignore this parameter. |
| | 799 | | /// </param> |
| | 800 | | /// <param name='safeSearch'> |
| | 801 | | /// Filter news for adult content. The following are the possible filter |
| | 802 | | /// values. Off: Return news articles with adult text, images, or videos. |
| | 803 | | /// Moderate: Return news articles with adult text but not adult images or |
| | 804 | | /// videos. Strict: Do not return news articles with adult text, images, or |
| | 805 | | /// videos. If the request comes from a market that Bing's adult policy |
| | 806 | | /// requires that safeSearch is set to Strict, Bing ignores the safeSearch |
| | 807 | | /// value and uses Strict. If you use the site: query operator, there is the |
| | 808 | | /// chance that the response may contain adult content regardless of what the |
| | 809 | | /// safeSearch query parameter is set to. Use site: only if you are aware of |
| | 810 | | /// the content on the site and your scenario supports the possibility of adult |
| | 811 | | /// content. Possible values include: 'Off', 'Moderate', 'Strict' |
| | 812 | | /// </param> |
| | 813 | | /// <param name='setLang'> |
| | 814 | | /// The language to use for user interface strings. Specify the language using |
| | 815 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 816 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 817 | | /// always specify the language. Typically, you set setLang to the same |
| | 818 | | /// language specified by mkt unless the user wants the user interface strings |
| | 819 | | /// displayed in a different language. This parameter and the |
| | 820 | | /// [Accept-Language](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#acc |
| | 821 | | /// header are mutually exclusive; do not specify both. A user interface string |
| | 822 | | /// is a string that's used as a label in a user interface. There are few user |
| | 823 | | /// interface strings in the JSON response objects. Also, any links to Bing.com |
| | 824 | | /// properties in the response objects apply the specified language. |
| | 825 | | /// </param> |
| | 826 | | /// <param name='textDecorations'> |
| | 827 | | /// A Boolean value that determines whether display strings contain decoration |
| | 828 | | /// markers such as hit highlighting characters. If true, the strings may |
| | 829 | | /// include markers. The default is false. To specify whether to use Unicode |
| | 830 | | /// characters or HTML tags as the markers, see the |
| | 831 | | /// [textFormat](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#textform |
| | 832 | | /// query parameter. For information about hit highlighting, see [Hit |
| | 833 | | /// Highlighting](https://docs.microsoft.com/azure/cognitive-services/bing-news-search/hit-highlighting). |
| | 834 | | /// </param> |
| | 835 | | /// <param name='textFormat'> |
| | 836 | | /// The type of markers to use for text decorations (see the textDecorations |
| | 837 | | /// query parameter). Possible values are Raw—Use Unicode characters to mark |
| | 838 | | /// content that needs special formatting. The Unicode characters are in the |
| | 839 | | /// range E000 through E019. For example, Bing uses E000 and E001 to mark the |
| | 840 | | /// beginning and end of query terms for hit highlighting. HTML—Use HTML tags |
| | 841 | | /// to mark content that needs special formatting. For example, use <b> |
| | 842 | | /// tags to highlight query terms in display strings. The default is Raw. For |
| | 843 | | /// display strings that contain escapable HTML characters such as <, >, |
| | 844 | | /// and &, if textFormat is set to HTML, Bing escapes the characters as |
| | 845 | | /// appropriate (for example, < is escaped to &lt;). Possible values |
| | 846 | | /// include: 'Raw', 'Html' |
| | 847 | | /// </param> |
| | 848 | | /// <param name='customHeaders'> |
| | 849 | | /// Headers that will be added to request. |
| | 850 | | /// </param> |
| | 851 | | /// <param name='cancellationToken'> |
| | 852 | | /// The cancellation token. |
| | 853 | | /// </param> |
| | 854 | | /// <exception cref="ErrorResponseException"> |
| | 855 | | /// Thrown when the operation returned an invalid status code |
| | 856 | | /// </exception> |
| | 857 | | /// <exception cref="SerializationException"> |
| | 858 | | /// Thrown when unable to deserialize the response |
| | 859 | | /// </exception> |
| | 860 | | /// <exception cref="ValidationException"> |
| | 861 | | /// Thrown when a required parameter is null |
| | 862 | | /// </exception> |
| | 863 | | /// <exception cref="System.ArgumentNullException"> |
| | 864 | | /// Thrown when a required parameter is null |
| | 865 | | /// </exception> |
| | 866 | | /// <return> |
| | 867 | | /// A response object containing the response body and response headers. |
| | 868 | | /// </return> |
| | 869 | | public async Task<HttpOperationResponse<News>> CategoryWithHttpMessagesAsync(string acceptLanguage = default(str |
| | 870 | | { |
| 2 | 871 | | if (Client.Endpoint == null) |
| | 872 | | { |
| 0 | 873 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 874 | | } |
| 2 | 875 | | string xBingApisSDK = "true"; |
| | 876 | | // Tracing |
| 2 | 877 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 878 | | string _invocationId = null; |
| 2 | 879 | | if (_shouldTrace) |
| | 880 | | { |
| 0 | 881 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 882 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 883 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| 0 | 884 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| 0 | 885 | | tracingParameters.Add("userAgent", userAgent); |
| 0 | 886 | | tracingParameters.Add("clientId", clientId); |
| 0 | 887 | | tracingParameters.Add("clientIp", clientIp); |
| 0 | 888 | | tracingParameters.Add("location", location); |
| 0 | 889 | | tracingParameters.Add("countryCode", countryCode); |
| 0 | 890 | | tracingParameters.Add("category", category); |
| 0 | 891 | | tracingParameters.Add("count", count); |
| 0 | 892 | | tracingParameters.Add("headlineCount", headlineCount); |
| 0 | 893 | | tracingParameters.Add("market", market); |
| 0 | 894 | | tracingParameters.Add("offset", offset); |
| 0 | 895 | | tracingParameters.Add("originalImage", originalImage); |
| 0 | 896 | | tracingParameters.Add("safeSearch", safeSearch); |
| 0 | 897 | | tracingParameters.Add("setLang", setLang); |
| 0 | 898 | | tracingParameters.Add("textDecorations", textDecorations); |
| 0 | 899 | | tracingParameters.Add("textFormat", textFormat); |
| 0 | 900 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 901 | | ServiceClientTracing.Enter(_invocationId, this, "Category", tracingParameters); |
| | 902 | | } |
| | 903 | | // Construct URL |
| 2 | 904 | | var _baseUrl = Client.BaseUri; |
| 2 | 905 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "news"; |
| 2 | 906 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 907 | | List<string> _queryParameters = new List<string>(); |
| 2 | 908 | | if (countryCode != null) |
| | 909 | | { |
| 0 | 910 | | _queryParameters.Add(string.Format("cc={0}", System.Uri.EscapeDataString(countryCode))); |
| | 911 | | } |
| 2 | 912 | | if (category != null) |
| | 913 | | { |
| 2 | 914 | | _queryParameters.Add(string.Format("category={0}", System.Uri.EscapeDataString(category))); |
| | 915 | | } |
| 2 | 916 | | if (count != null) |
| | 917 | | { |
| 0 | 918 | | _queryParameters.Add(string.Format("count={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonC |
| | 919 | | } |
| 2 | 920 | | if (headlineCount != null) |
| | 921 | | { |
| 0 | 922 | | _queryParameters.Add(string.Format("headlineCount={0}", System.Uri.EscapeDataString(Rest.Serialization.S |
| | 923 | | } |
| 2 | 924 | | if (market != null) |
| | 925 | | { |
| 0 | 926 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 927 | | } |
| 2 | 928 | | if (offset != null) |
| | 929 | | { |
| 0 | 930 | | _queryParameters.Add(string.Format("offset={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJson |
| | 931 | | } |
| 2 | 932 | | if (originalImage != null) |
| | 933 | | { |
| 0 | 934 | | _queryParameters.Add(string.Format("originalImg={0}", System.Uri.EscapeDataString(Rest.Serialization.Saf |
| | 935 | | } |
| 2 | 936 | | if (safeSearch != null) |
| | 937 | | { |
| 0 | 938 | | _queryParameters.Add(string.Format("safeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 939 | | } |
| 2 | 940 | | if (setLang != null) |
| | 941 | | { |
| 0 | 942 | | _queryParameters.Add(string.Format("setLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 943 | | } |
| 2 | 944 | | if (textDecorations != null) |
| | 945 | | { |
| 0 | 946 | | _queryParameters.Add(string.Format("textDecorations={0}", System.Uri.EscapeDataString(Rest.Serialization |
| | 947 | | } |
| 2 | 948 | | if (textFormat != null) |
| | 949 | | { |
| 0 | 950 | | _queryParameters.Add(string.Format("textFormat={0}", System.Uri.EscapeDataString(textFormat))); |
| | 951 | | } |
| 2 | 952 | | if (_queryParameters.Count > 0) |
| | 953 | | { |
| 2 | 954 | | _url += "?" + string.Join("&", _queryParameters); |
| | 955 | | } |
| | 956 | | // Create HTTP transport objects |
| 2 | 957 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 958 | | HttpResponseMessage _httpResponse = null; |
| 2 | 959 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 960 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 961 | | // Set Headers |
| 2 | 962 | | if (xBingApisSDK != null) |
| | 963 | | { |
| 2 | 964 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 965 | | { |
| 0 | 966 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 967 | | } |
| 2 | 968 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 969 | | } |
| 2 | 970 | | if (acceptLanguage != null) |
| | 971 | | { |
| 0 | 972 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 973 | | { |
| 0 | 974 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 975 | | } |
| 0 | 976 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 977 | | } |
| 2 | 978 | | if (userAgent != null) |
| | 979 | | { |
| 0 | 980 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 981 | | { |
| 0 | 982 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 983 | | } |
| 0 | 984 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 985 | | } |
| 2 | 986 | | if (clientId != null) |
| | 987 | | { |
| 0 | 988 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 989 | | { |
| 0 | 990 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 991 | | } |
| 0 | 992 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 993 | | } |
| 2 | 994 | | if (clientIp != null) |
| | 995 | | { |
| 0 | 996 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 997 | | { |
| 0 | 998 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 999 | | } |
| 0 | 1000 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 1001 | | } |
| 2 | 1002 | | if (location != null) |
| | 1003 | | { |
| 0 | 1004 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 1005 | | { |
| 0 | 1006 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 1007 | | } |
| 0 | 1008 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 1009 | | } |
| | 1010 | |
|
| | 1011 | |
|
| 2 | 1012 | | if (customHeaders != null) |
| | 1013 | | { |
| 0 | 1014 | | foreach(var _header in customHeaders) |
| | 1015 | | { |
| 0 | 1016 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1017 | | { |
| 0 | 1018 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1019 | | } |
| 0 | 1020 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1021 | | } |
| | 1022 | | } |
| | 1023 | |
|
| | 1024 | | // Serialize Request |
| 2 | 1025 | | string _requestContent = null; |
| | 1026 | | // Set Credentials |
| 2 | 1027 | | if (Client.Credentials != null) |
| | 1028 | | { |
| 2 | 1029 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1030 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1031 | | } |
| | 1032 | | // Send Request |
| 2 | 1033 | | if (_shouldTrace) |
| | 1034 | | { |
| 0 | 1035 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1036 | | } |
| 2 | 1037 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1038 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 1039 | | if (_shouldTrace) |
| | 1040 | | { |
| 0 | 1041 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1042 | | } |
| 2 | 1043 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1044 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1045 | | string _responseContent = null; |
| 2 | 1046 | | if ((int)_statusCode != 200) |
| | 1047 | | { |
| 0 | 1048 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 1049 | | try |
| | 1050 | | { |
| 0 | 1051 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1052 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 1053 | | if (_errorBody != null) |
| | 1054 | | { |
| 0 | 1055 | | ex.Body = _errorBody; |
| | 1056 | | } |
| 0 | 1057 | | } |
| 0 | 1058 | | catch (JsonException) |
| | 1059 | | { |
| | 1060 | | // Ignore the exception |
| 0 | 1061 | | } |
| 0 | 1062 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1063 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1064 | | if (_shouldTrace) |
| | 1065 | | { |
| 0 | 1066 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1067 | | } |
| 0 | 1068 | | _httpRequest.Dispose(); |
| 0 | 1069 | | if (_httpResponse != null) |
| | 1070 | | { |
| 0 | 1071 | | _httpResponse.Dispose(); |
| | 1072 | | } |
| 0 | 1073 | | throw ex; |
| | 1074 | | } |
| | 1075 | | // Create Result |
| 2 | 1076 | | var _result = new HttpOperationResponse<News>(); |
| 2 | 1077 | | _result.Request = _httpRequest; |
| 2 | 1078 | | _result.Response = _httpResponse; |
| | 1079 | | // Deserialize Response |
| 2 | 1080 | | if ((int)_statusCode == 200) |
| | 1081 | | { |
| 2 | 1082 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1083 | | try |
| | 1084 | | { |
| 2 | 1085 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<News>(_responseContent, Client.D |
| 2 | 1086 | | } |
| 0 | 1087 | | catch (JsonException ex) |
| | 1088 | | { |
| 0 | 1089 | | _httpRequest.Dispose(); |
| 0 | 1090 | | if (_httpResponse != null) |
| | 1091 | | { |
| 0 | 1092 | | _httpResponse.Dispose(); |
| | 1093 | | } |
| 0 | 1094 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1095 | | } |
| | 1096 | | } |
| 2 | 1097 | | if (_shouldTrace) |
| | 1098 | | { |
| 0 | 1099 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1100 | | } |
| 2 | 1101 | | return _result; |
| 2 | 1102 | | } |
| | 1103 | |
|
| | 1104 | | /// <summary> |
| | 1105 | | /// The News Trending Topics API lets lets you search on Bing and get back a |
| | 1106 | | /// list of trending news topics that are currently trending on Bing. This |
| | 1107 | | /// section provides technical details about the query parameters and headers |
| | 1108 | | /// that you use to request news and the JSON response objects that contain |
| | 1109 | | /// them. For examples that show how to make requests, see [Searching the web |
| | 1110 | | /// for |
| | 1111 | | /// news](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-news-search/search-the-web). |
| | 1112 | | /// </summary> |
| | 1113 | | /// <param name='acceptLanguage'> |
| | 1114 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 1115 | | /// strings. The list is in decreasing order of preference. For additional |
| | 1116 | | /// information, including expected format, see |
| | 1117 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 1118 | | /// header and the |
| | 1119 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 1120 | | /// query parameter are mutually exclusive; do not specify both. If you set |
| | 1121 | | /// this header, you must also specify the |
| | 1122 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 1123 | | /// query parameter. To determine the market to return results for, Bing uses |
| | 1124 | | /// the first supported language it finds from the list and combines it with |
| | 1125 | | /// the cc parameter value. If the list does not include a supported language, |
| | 1126 | | /// Bing finds the closest language and market that supports the request or it |
| | 1127 | | /// uses an aggregated or default market for the results. To determine the |
| | 1128 | | /// market that Bing used, see the BingAPIs-Market header. Use this header and |
| | 1129 | | /// the cc query parameter only if you specify multiple languages. Otherwise, |
| | 1130 | | /// use the |
| | 1131 | | /// [mkt](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#mkt) |
| | 1132 | | /// and |
| | 1133 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#setlang) |
| | 1134 | | /// query parameters. A user interface string is a string that's used as a |
| | 1135 | | /// label in a user interface. There are few user interface strings in the JSON |
| | 1136 | | /// response objects. Any links to Bing.com properties in the response objects |
| | 1137 | | /// apply the specified language. |
| | 1138 | | /// </param> |
| | 1139 | | /// <param name='userAgent'> |
| | 1140 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 1141 | | /// mobile users with an optimized experience. Although optional, you are |
| | 1142 | | /// encouraged to always specify this header. The user-agent should be the same |
| | 1143 | | /// string that any commonly used browser sends. For information about user |
| | 1144 | | /// agents, see [RFC |
| | 1145 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). The |
| | 1146 | | /// following are examples of user-agent strings. Windows Phone: Mozilla/5.0 |
| | 1147 | | /// (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; |
| | 1148 | | /// Touch; NOKIA; Lumia 822). Android: Mozilla / 5.0 (Linux; U; Android 2.3.5; |
| | 1149 | | /// en - us; SCH - I500 Build / GINGERBREAD) AppleWebKit / 533.1 (KHTML; like |
| | 1150 | | /// Gecko) Version / 4.0 Mobile Safari / 533.1. iPhone: Mozilla / 5.0 (iPhone; |
| | 1151 | | /// CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 536.26 (KHTML; like Gecko) |
| | 1152 | | /// Mobile / 10B142 iPhone4; 1 BingWeb / 3.03.1428.20120423. PC: Mozilla / 5.0 |
| | 1153 | | /// (Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0) like Gecko. iPad: |
| | 1154 | | /// Mozilla / 5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit / 537.51.1 |
| | 1155 | | /// (KHTML, like Gecko) Version / 7.0 Mobile / 11A465 Safari / 9537.53 |
| | 1156 | | /// </param> |
| | 1157 | | /// <param name='clientId'> |
| | 1158 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 1159 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 1160 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 1161 | | /// do not use the same client ID for a user across multiple requests, then |
| | 1162 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 1163 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 1164 | | /// For example, if the second request has a different flight assignment than |
| | 1165 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 1166 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 1167 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 1168 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 1169 | | /// relevance improvements help with better quality of results delivered by |
| | 1170 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 1171 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 1172 | | /// required. Persisting the client ID across multiple requests for the same |
| | 1173 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 1174 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 1175 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 1176 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 1177 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 1178 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 1179 | | /// include this header in a request is the first time the user uses your app |
| | 1180 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 1181 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 1182 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 1183 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 1184 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 1185 | | /// next time the user uses your app on that device, get the client ID that you |
| | 1186 | | /// persisted. Bing responses may or may not include this header. If the |
| | 1187 | | /// response includes this header, capture the client ID and use it for all |
| | 1188 | | /// subsequent Bing requests for the user on that device. If you include the |
| | 1189 | | /// X-MSEdge-ClientID, you must not include cookies in the request. |
| | 1190 | | /// </param> |
| | 1191 | | /// <param name='clientIp'> |
| | 1192 | | /// The IPv4 or IPv6 address of the client device. The IP address is used to |
| | 1193 | | /// discover the user's location. Bing uses the location information to |
| | 1194 | | /// determine safe search behavior. Although optional, you are encouraged to |
| | 1195 | | /// always specify this header and the X-Search-Location header. Do not |
| | 1196 | | /// obfuscate the address (for example, by changing the last octet to 0). |
| | 1197 | | /// Obfuscating the address results in the location not being anywhere near the |
| | 1198 | | /// device's actual location, which may result in Bing serving erroneous |
| | 1199 | | /// results. |
| | 1200 | | /// </param> |
| | 1201 | | /// <param name='location'> |
| | 1202 | | /// A semicolon-delimited list of key/value pairs that describe the client's |
| | 1203 | | /// geographical location. Bing uses the location information to determine safe |
| | 1204 | | /// search behavior and to return relevant local content. Specify the key/value |
| | 1205 | | /// pair as <key>:<value>. The following are the keys that you use |
| | 1206 | | /// to specify the user's location. lat (required): The latitude of the |
| | 1207 | | /// client's location, in degrees. The latitude must be greater than or equal |
| | 1208 | | /// to -90.0 and less than or equal to +90.0. Negative values indicate southern |
| | 1209 | | /// latitudes and positive values indicate northern latitudes. long (required): |
| | 1210 | | /// The longitude of the client's location, in degrees. The longitude must be |
| | 1211 | | /// greater than or equal to -180.0 and less than or equal to +180.0. Negative |
| | 1212 | | /// values indicate western longitudes and positive values indicate eastern |
| | 1213 | | /// longitudes. re (required): The radius, in meters, which specifies the |
| | 1214 | | /// horizontal accuracy of the coordinates. Pass the value returned by the |
| | 1215 | | /// device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m |
| | 1216 | | /// for cell tower triangulation, and 18,000m for reverse IP lookup. ts |
| | 1217 | | /// (optional): The UTC UNIX timestamp of when the client was at the location. |
| | 1218 | | /// (The UNIX timestamp is the number of seconds since January 1, 1970.) head |
| | 1219 | | /// (optional): The client's relative heading or direction of travel. Specify |
| | 1220 | | /// the direction of travel as degrees from 0 through 360, counting clockwise |
| | 1221 | | /// relative to true north. Specify this key only if the sp key is nonzero. sp |
| | 1222 | | /// (optional): The horizontal velocity (speed), in meters per second, that the |
| | 1223 | | /// client device is traveling. alt (optional): The altitude of the client |
| | 1224 | | /// device, in meters. are (optional): The radius, in meters, that specifies |
| | 1225 | | /// the vertical accuracy of the coordinates. Specify this key only if you |
| | 1226 | | /// specify the alt key. Although many of the keys are optional, the more |
| | 1227 | | /// information that you provide, the more accurate the location results are. |
| | 1228 | | /// Although optional, you are encouraged to always specify the user's |
| | 1229 | | /// geographical location. Providing the location is especially important if |
| | 1230 | | /// the client's IP address does not accurately reflect the user's physical |
| | 1231 | | /// location (for example, if the client uses VPN). For optimal results, you |
| | 1232 | | /// should include this header and the X-MSEdge-ClientIP header, but at a |
| | 1233 | | /// minimum, you should include this header. |
| | 1234 | | /// </param> |
| | 1235 | | /// <param name='countryCode'> |
| | 1236 | | /// A 2-character country code of the country where the results come from. This |
| | 1237 | | /// API supports only the United States market. If you specify this query |
| | 1238 | | /// parameter, it must be set to us. If you set this parameter, you must also |
| | 1239 | | /// specify the Accept-Language header. Bing uses the first supported language |
| | 1240 | | /// it finds from the languages list, and combine that language with the |
| | 1241 | | /// country code that you specify to determine the market to return results |
| | 1242 | | /// for. If the languages list does not include a supported language, Bing |
| | 1243 | | /// finds the closest language and market that supports the request, or it may |
| | 1244 | | /// use an aggregated or default market for the results instead of a specified |
| | 1245 | | /// one. You should use this query parameter and the Accept-Language query |
| | 1246 | | /// parameter only if you specify multiple languages; otherwise, you should use |
| | 1247 | | /// the mkt and setLang query parameters. This parameter and the mkt query |
| | 1248 | | /// parameter are mutually exclusive—do not specify both. |
| | 1249 | | /// </param> |
| | 1250 | | /// <param name='count'> |
| | 1251 | | /// The number of news articles to return in the response. The actual number |
| | 1252 | | /// delivered may be less than requested. The default is 10 and the maximum |
| | 1253 | | /// value is 100. The actual number delivered may be less than requested.You |
| | 1254 | | /// may use this parameter along with the offset parameter to page results. For |
| | 1255 | | /// example, if your user interface displays 20 articles per page, set count to |
| | 1256 | | /// 20 and offset to 0 to get the first page of results. For each subsequent |
| | 1257 | | /// page, increment offset by 20 (for example, 0, 20, 40). It is possible for |
| | 1258 | | /// multiple pages to include some overlap in results. |
| | 1259 | | /// </param> |
| | 1260 | | /// <param name='market'> |
| | 1261 | | /// The market where the results come from. Typically, mkt is the country where |
| | 1262 | | /// the user is making the request from. However, it could be a different |
| | 1263 | | /// country if the user is not located in a country where Bing delivers |
| | 1264 | | /// results. The market must be in the form <language code>-<country |
| | 1265 | | /// code>. For example, en-US. The string is case insensitive. For a list of |
| | 1266 | | /// possible market values, see [Market |
| | 1267 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes). |
| | 1268 | | /// NOTE: If known, you are encouraged to always specify the market. Specifying |
| | 1269 | | /// the market helps Bing route the request and return an appropriate and |
| | 1270 | | /// optimal response. If you specify a market that is not listed in [Market |
| | 1271 | | /// Codes](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#market-codes), |
| | 1272 | | /// Bing uses a best fit market code based on an internal mapping that is |
| | 1273 | | /// subject to change. This parameter and the |
| | 1274 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#cc) |
| | 1275 | | /// query parameter are mutually exclusive—do not specify both. |
| | 1276 | | /// </param> |
| | 1277 | | /// <param name='offset'> |
| | 1278 | | /// The zero-based offset that indicates the number of news to skip before |
| | 1279 | | /// returning news. The default is 0. The offset should be less than |
| | 1280 | | /// ([totalEstimatedMatches](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-refere |
| | 1281 | | /// - count). Use this parameter along with the count parameter to page |
| | 1282 | | /// results. For example, if your user interface displays 20 news per page, set |
| | 1283 | | /// count to 20 and offset to 0 to get the first page of results. For each |
| | 1284 | | /// subsequent page, increment offset by 20 (for example, 0, 20, 40). It is |
| | 1285 | | /// possible for multiple pages to include some overlap in results. |
| | 1286 | | /// </param> |
| | 1287 | | /// <param name='safeSearch'> |
| | 1288 | | /// Filter news for adult content. The following are the possible filter |
| | 1289 | | /// values. Off: Return news articles with adult text, images, or videos. |
| | 1290 | | /// Moderate: Return news articles with adult text but not adult images or |
| | 1291 | | /// videos. Strict: Do not return news articles with adult text, images, or |
| | 1292 | | /// videos. If the request comes from a market that Bing's adult policy |
| | 1293 | | /// requires that safeSearch is set to Strict, Bing ignores the safeSearch |
| | 1294 | | /// value and uses Strict. If you use the site: query operator, there is the |
| | 1295 | | /// chance that the response may contain adult content regardless of what the |
| | 1296 | | /// safeSearch query parameter is set to. Use site: only if you are aware of |
| | 1297 | | /// the content on the site and your scenario supports the possibility of adult |
| | 1298 | | /// content. Possible values include: 'Off', 'Moderate', 'Strict' |
| | 1299 | | /// </param> |
| | 1300 | | /// <param name='setLang'> |
| | 1301 | | /// The language to use for user interface strings. Specify the language using |
| | 1302 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 1303 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 1304 | | /// always specify the language. Typically, you set setLang to the same |
| | 1305 | | /// language specified by mkt unless the user wants the user interface strings |
| | 1306 | | /// displayed in a different language. This parameter and the |
| | 1307 | | /// [Accept-Language](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#acc |
| | 1308 | | /// header are mutually exclusive; do not specify both. A user interface string |
| | 1309 | | /// is a string that's used as a label in a user interface. There are few user |
| | 1310 | | /// interface strings in the JSON response objects. Also, any links to Bing.com |
| | 1311 | | /// properties in the response objects apply the specified language. |
| | 1312 | | /// </param> |
| | 1313 | | /// <param name='since'> |
| | 1314 | | /// The Unix epoch time (Unix timestamp) that Bing uses to select the trending |
| | 1315 | | /// topics. Bing returns trending topics that it discovered on or after the |
| | 1316 | | /// specified date and time, not the date the topic was published. To use this |
| | 1317 | | /// parameter, also specify the sortBy parameter. Use this parameter only with |
| | 1318 | | /// the News Trending Topics API. Do not specify this parameter when calling |
| | 1319 | | /// the News Search API or News Category API. |
| | 1320 | | /// </param> |
| | 1321 | | /// <param name='sortBy'> |
| | 1322 | | /// The order to return the news in. The following are the possible |
| | 1323 | | /// case-insensitive values. Date: If the request is through the News Search |
| | 1324 | | /// API, the response returns news articles sorted by date from the most recent |
| | 1325 | | /// to the oldest. If the request is through the News Trending Topics API, the |
| | 1326 | | /// response returns trending topics sorted by date from the most recent to the |
| | 1327 | | /// oldest. |
| | 1328 | | /// </param> |
| | 1329 | | /// <param name='textDecorations'> |
| | 1330 | | /// A Boolean value that determines whether display strings contain decoration |
| | 1331 | | /// markers such as hit highlighting characters. If true, the strings may |
| | 1332 | | /// include markers. The default is false. To specify whether to use Unicode |
| | 1333 | | /// characters or HTML tags as the markers, see the |
| | 1334 | | /// [textFormat](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-news-api-v7-reference#textform |
| | 1335 | | /// query parameter. For information about hit highlighting, see [Hit |
| | 1336 | | /// Highlighting](https://docs.microsoft.com/azure/cognitive-services/bing-news-search/hit-highlighting). |
| | 1337 | | /// </param> |
| | 1338 | | /// <param name='textFormat'> |
| | 1339 | | /// The type of markers to use for text decorations (see the textDecorations |
| | 1340 | | /// query parameter). Possible values are Raw—Use Unicode characters to mark |
| | 1341 | | /// content that needs special formatting. The Unicode characters are in the |
| | 1342 | | /// range E000 through E019. For example, Bing uses E000 and E001 to mark the |
| | 1343 | | /// beginning and end of query terms for hit highlighting. HTML—Use HTML tags |
| | 1344 | | /// to mark content that needs special formatting. For example, use <b> |
| | 1345 | | /// tags to highlight query terms in display strings. The default is Raw. For |
| | 1346 | | /// display strings that contain escapable HTML characters such as <, >, |
| | 1347 | | /// and &, if textFormat is set to HTML, Bing escapes the characters as |
| | 1348 | | /// appropriate (for example, < is escaped to &lt;). Possible values |
| | 1349 | | /// include: 'Raw', 'Html' |
| | 1350 | | /// </param> |
| | 1351 | | /// <param name='customHeaders'> |
| | 1352 | | /// Headers that will be added to request. |
| | 1353 | | /// </param> |
| | 1354 | | /// <param name='cancellationToken'> |
| | 1355 | | /// The cancellation token. |
| | 1356 | | /// </param> |
| | 1357 | | /// <exception cref="ErrorResponseException"> |
| | 1358 | | /// Thrown when the operation returned an invalid status code |
| | 1359 | | /// </exception> |
| | 1360 | | /// <exception cref="SerializationException"> |
| | 1361 | | /// Thrown when unable to deserialize the response |
| | 1362 | | /// </exception> |
| | 1363 | | /// <exception cref="ValidationException"> |
| | 1364 | | /// Thrown when a required parameter is null |
| | 1365 | | /// </exception> |
| | 1366 | | /// <exception cref="System.ArgumentNullException"> |
| | 1367 | | /// Thrown when a required parameter is null |
| | 1368 | | /// </exception> |
| | 1369 | | /// <return> |
| | 1370 | | /// A response object containing the response body and response headers. |
| | 1371 | | /// </return> |
| | 1372 | | public async Task<HttpOperationResponse<TrendingTopics>> TrendingWithHttpMessagesAsync(string acceptLanguage = d |
| | 1373 | | { |
| 2 | 1374 | | if (Client.Endpoint == null) |
| | 1375 | | { |
| 0 | 1376 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 1377 | | } |
| 2 | 1378 | | string xBingApisSDK = "true"; |
| | 1379 | | // Tracing |
| 2 | 1380 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 2 | 1381 | | string _invocationId = null; |
| 2 | 1382 | | if (_shouldTrace) |
| | 1383 | | { |
| 0 | 1384 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 1385 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 1386 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| 0 | 1387 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| 0 | 1388 | | tracingParameters.Add("userAgent", userAgent); |
| 0 | 1389 | | tracingParameters.Add("clientId", clientId); |
| 0 | 1390 | | tracingParameters.Add("clientIp", clientIp); |
| 0 | 1391 | | tracingParameters.Add("location", location); |
| 0 | 1392 | | tracingParameters.Add("countryCode", countryCode); |
| 0 | 1393 | | tracingParameters.Add("count", count); |
| 0 | 1394 | | tracingParameters.Add("market", market); |
| 0 | 1395 | | tracingParameters.Add("offset", offset); |
| 0 | 1396 | | tracingParameters.Add("safeSearch", safeSearch); |
| 0 | 1397 | | tracingParameters.Add("setLang", setLang); |
| 0 | 1398 | | tracingParameters.Add("since", since); |
| 0 | 1399 | | tracingParameters.Add("sortBy", sortBy); |
| 0 | 1400 | | tracingParameters.Add("textDecorations", textDecorations); |
| 0 | 1401 | | tracingParameters.Add("textFormat", textFormat); |
| 0 | 1402 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 1403 | | ServiceClientTracing.Enter(_invocationId, this, "Trending", tracingParameters); |
| | 1404 | | } |
| | 1405 | | // Construct URL |
| 2 | 1406 | | var _baseUrl = Client.BaseUri; |
| 2 | 1407 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "news/trendingtopics"; |
| 2 | 1408 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 2 | 1409 | | List<string> _queryParameters = new List<string>(); |
| 2 | 1410 | | if (countryCode != null) |
| | 1411 | | { |
| 0 | 1412 | | _queryParameters.Add(string.Format("cc={0}", System.Uri.EscapeDataString(countryCode))); |
| | 1413 | | } |
| 2 | 1414 | | if (count != null) |
| | 1415 | | { |
| 0 | 1416 | | _queryParameters.Add(string.Format("count={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonC |
| | 1417 | | } |
| 2 | 1418 | | if (market != null) |
| | 1419 | | { |
| 0 | 1420 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 1421 | | } |
| 2 | 1422 | | if (offset != null) |
| | 1423 | | { |
| 0 | 1424 | | _queryParameters.Add(string.Format("offset={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJson |
| | 1425 | | } |
| 2 | 1426 | | if (safeSearch != null) |
| | 1427 | | { |
| 0 | 1428 | | _queryParameters.Add(string.Format("safeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 1429 | | } |
| 2 | 1430 | | if (setLang != null) |
| | 1431 | | { |
| 0 | 1432 | | _queryParameters.Add(string.Format("setLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 1433 | | } |
| 2 | 1434 | | if (since != null) |
| | 1435 | | { |
| 0 | 1436 | | _queryParameters.Add(string.Format("since={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonC |
| | 1437 | | } |
| 2 | 1438 | | if (sortBy != null) |
| | 1439 | | { |
| 0 | 1440 | | _queryParameters.Add(string.Format("sortBy={0}", System.Uri.EscapeDataString(sortBy))); |
| | 1441 | | } |
| 2 | 1442 | | if (textDecorations != null) |
| | 1443 | | { |
| 0 | 1444 | | _queryParameters.Add(string.Format("textDecorations={0}", System.Uri.EscapeDataString(Rest.Serialization |
| | 1445 | | } |
| 2 | 1446 | | if (textFormat != null) |
| | 1447 | | { |
| 0 | 1448 | | _queryParameters.Add(string.Format("textFormat={0}", System.Uri.EscapeDataString(textFormat))); |
| | 1449 | | } |
| 2 | 1450 | | if (_queryParameters.Count > 0) |
| | 1451 | | { |
| 0 | 1452 | | _url += "?" + string.Join("&", _queryParameters); |
| | 1453 | | } |
| | 1454 | | // Create HTTP transport objects |
| 2 | 1455 | | var _httpRequest = new HttpRequestMessage(); |
| 2 | 1456 | | HttpResponseMessage _httpResponse = null; |
| 2 | 1457 | | _httpRequest.Method = new HttpMethod("GET"); |
| 2 | 1458 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 1459 | | // Set Headers |
| 2 | 1460 | | if (xBingApisSDK != null) |
| | 1461 | | { |
| 2 | 1462 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 1463 | | { |
| 0 | 1464 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 1465 | | } |
| 2 | 1466 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 1467 | | } |
| 2 | 1468 | | if (acceptLanguage != null) |
| | 1469 | | { |
| 0 | 1470 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 1471 | | { |
| 0 | 1472 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 1473 | | } |
| 0 | 1474 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 1475 | | } |
| 2 | 1476 | | if (userAgent != null) |
| | 1477 | | { |
| 0 | 1478 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 1479 | | { |
| 0 | 1480 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 1481 | | } |
| 0 | 1482 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 1483 | | } |
| 2 | 1484 | | if (clientId != null) |
| | 1485 | | { |
| 0 | 1486 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 1487 | | { |
| 0 | 1488 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 1489 | | } |
| 0 | 1490 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 1491 | | } |
| 2 | 1492 | | if (clientIp != null) |
| | 1493 | | { |
| 0 | 1494 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 1495 | | { |
| 0 | 1496 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 1497 | | } |
| 0 | 1498 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 1499 | | } |
| 2 | 1500 | | if (location != null) |
| | 1501 | | { |
| 0 | 1502 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 1503 | | { |
| 0 | 1504 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 1505 | | } |
| 0 | 1506 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 1507 | | } |
| | 1508 | |
|
| | 1509 | |
|
| 2 | 1510 | | if (customHeaders != null) |
| | 1511 | | { |
| 0 | 1512 | | foreach(var _header in customHeaders) |
| | 1513 | | { |
| 0 | 1514 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 1515 | | { |
| 0 | 1516 | | _httpRequest.Headers.Remove(_header.Key); |
| | 1517 | | } |
| 0 | 1518 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 1519 | | } |
| | 1520 | | } |
| | 1521 | |
|
| | 1522 | | // Serialize Request |
| 2 | 1523 | | string _requestContent = null; |
| | 1524 | | // Set Credentials |
| 2 | 1525 | | if (Client.Credentials != null) |
| | 1526 | | { |
| 2 | 1527 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1528 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 1529 | | } |
| | 1530 | | // Send Request |
| 2 | 1531 | | if (_shouldTrace) |
| | 1532 | | { |
| 0 | 1533 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 1534 | | } |
| 2 | 1535 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1536 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 2 | 1537 | | if (_shouldTrace) |
| | 1538 | | { |
| 0 | 1539 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 1540 | | } |
| 2 | 1541 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 2 | 1542 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 1543 | | string _responseContent = null; |
| 2 | 1544 | | if ((int)_statusCode != 200) |
| | 1545 | | { |
| 0 | 1546 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 1547 | | try |
| | 1548 | | { |
| 0 | 1549 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 1550 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| 0 | 1551 | | if (_errorBody != null) |
| | 1552 | | { |
| 0 | 1553 | | ex.Body = _errorBody; |
| | 1554 | | } |
| 0 | 1555 | | } |
| 0 | 1556 | | catch (JsonException) |
| | 1557 | | { |
| | 1558 | | // Ignore the exception |
| 0 | 1559 | | } |
| 0 | 1560 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 1561 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 1562 | | if (_shouldTrace) |
| | 1563 | | { |
| 0 | 1564 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 1565 | | } |
| 0 | 1566 | | _httpRequest.Dispose(); |
| 0 | 1567 | | if (_httpResponse != null) |
| | 1568 | | { |
| 0 | 1569 | | _httpResponse.Dispose(); |
| | 1570 | | } |
| 0 | 1571 | | throw ex; |
| | 1572 | | } |
| | 1573 | | // Create Result |
| 2 | 1574 | | var _result = new HttpOperationResponse<TrendingTopics>(); |
| 2 | 1575 | | _result.Request = _httpRequest; |
| 2 | 1576 | | _result.Response = _httpResponse; |
| | 1577 | | // Deserialize Response |
| 2 | 1578 | | if ((int)_statusCode == 200) |
| | 1579 | | { |
| 2 | 1580 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 1581 | | try |
| | 1582 | | { |
| 2 | 1583 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<TrendingTopics>(_responseContent |
| 2 | 1584 | | } |
| 0 | 1585 | | catch (JsonException ex) |
| | 1586 | | { |
| 0 | 1587 | | _httpRequest.Dispose(); |
| 0 | 1588 | | if (_httpResponse != null) |
| | 1589 | | { |
| 0 | 1590 | | _httpResponse.Dispose(); |
| | 1591 | | } |
| 0 | 1592 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 1593 | | } |
| | 1594 | | } |
| 2 | 1595 | | if (_shouldTrace) |
| | 1596 | | { |
| 0 | 1597 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 1598 | | } |
| 2 | 1599 | | return _result; |
| 2 | 1600 | | } |
| | 1601 | |
|
| | 1602 | | } |
| | 1603 | | } |