| | 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.VisualSearch |
| | 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.Net.Http.Headers; |
| | 19 | | using System.Threading; |
| | 20 | | using System.Threading.Tasks; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Images operations. |
| | 24 | | /// </summary> |
| | 25 | | public partial class Images : IServiceOperations<VisualSearchClient>, IImages |
| | 26 | | { |
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the Images class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name='client'> |
| | 31 | | /// Reference to the service client. |
| | 32 | | /// </param> |
| | 33 | | /// <exception cref="System.ArgumentNullException"> |
| | 34 | | /// Thrown when a required parameter is null |
| | 35 | | /// </exception> |
| 8 | 36 | | public Images(VisualSearchClient client) |
| | 37 | | { |
| 8 | 38 | | if (client == null) |
| | 39 | | { |
| 0 | 40 | | throw new System.ArgumentNullException("client"); |
| | 41 | | } |
| 8 | 42 | | Client = client; |
| 8 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets a reference to the VisualSearchClient |
| | 47 | | /// </summary> |
| 64 | 48 | | public VisualSearchClient Client { get; private set; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Visual Search API lets you discover insights about an image such as |
| | 52 | | /// visually similar images, shopping sources, and related searches. The API |
| | 53 | | /// can also perform text recognition, identify entities (people, places, |
| | 54 | | /// things), return other topical content for the user to explore, and more. |
| | 55 | | /// For more information, see [Visual Search |
| | 56 | | /// Overview](https://docs.microsoft.com/azure/cognitive-services/bing-visual-search/overview). |
| | 57 | | /// </summary> |
| | 58 | | /// <param name='acceptLanguage'> |
| | 59 | | /// A comma-delimited list of one or more languages to use for user interface |
| | 60 | | /// strings. The list is in decreasing order of preference. For additional |
| | 61 | | /// information, including expected format, see |
| | 62 | | /// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This |
| | 63 | | /// header and the |
| | 64 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-visual-search-api-v7-reference#se |
| | 65 | | /// query parameter are mutually exclusive; do not specify both. If you set |
| | 66 | | /// this header, you must also specify the |
| | 67 | | /// [cc](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-visual-search-api-v7-reference#cc) |
| | 68 | | /// query parameter. To determine the market to return results for, Bing uses |
| | 69 | | /// the first supported language it finds from the list and combines it with |
| | 70 | | /// the cc parameter value. If the list does not include a supported language, |
| | 71 | | /// Bing finds the closest language and market that supports the request or it |
| | 72 | | /// uses an aggregated or default market for the results. To determine the |
| | 73 | | /// market that Bing used, see the BingAPIs-Market header. Use this header and |
| | 74 | | /// the cc query parameter only if you specify multiple languages. Otherwise, |
| | 75 | | /// use the |
| | 76 | | /// [mkt](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-visual-search-api-v7-reference#mkt) |
| | 77 | | /// and |
| | 78 | | /// [setLang](https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-visual-search-api-v7-reference#se |
| | 79 | | /// query parameters. A user interface string is a string that's used as a |
| | 80 | | /// label in a user interface. There are few user interface strings in the JSON |
| | 81 | | /// response objects. Any links to Bing.com properties in the response objects |
| | 82 | | /// apply the specified language. |
| | 83 | | /// </param> |
| | 84 | | /// <param name='contentType'> |
| | 85 | | /// Must be set to multipart/form-data and include a boundary parameter (for |
| | 86 | | /// example, multipart/form-data; boundary=<boundary string>). For more |
| | 87 | | /// details, see [Content form types]( |
| | 88 | | /// https://docs.microsoft.com/en-us/azure/cognitive-services/bing-visual-search/overview#content-form-types). |
| | 89 | | /// </param> |
| | 90 | | /// <param name='userAgent'> |
| | 91 | | /// The user agent originating the request. Bing uses the user agent to provide |
| | 92 | | /// mobile users with an optimized experience. Although optional, you are |
| | 93 | | /// encouraged to always specify this header. The user-agent should be the same |
| | 94 | | /// string that any commonly used browser sends. For information about user |
| | 95 | | /// agents, see [RFC |
| | 96 | | /// 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). The |
| | 97 | | /// following are examples of user-agent strings. Windows Phone: Mozilla/5.0 |
| | 98 | | /// (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; |
| | 99 | | /// Touch; NOKIA; Lumia 822). Android: Mozilla / 5.0 (Linux; U; Android 2.3.5; |
| | 100 | | /// en - us; SCH - I500 Build / GINGERBREAD) AppleWebKit / 533.1 (KHTML; like |
| | 101 | | /// Gecko) Version / 4.0 Mobile Safari / 533.1. iPhone: Mozilla / 5.0 (iPhone; |
| | 102 | | /// CPU iPhone OS 6_1 like Mac OS X) AppleWebKit / 536.26 (KHTML; like Gecko) |
| | 103 | | /// Mobile / 10B142 iPhone4; 1 BingWeb / 3.03.1428.20120423. PC: Mozilla / 5.0 |
| | 104 | | /// (Windows NT 6.3; WOW64; Trident / 7.0; Touch; rv:11.0) like Gecko. iPad: |
| | 105 | | /// Mozilla / 5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit / 537.51.1 |
| | 106 | | /// (KHTML, like Gecko) Version / 7.0 Mobile / 11A465 Safari / 9537.53. |
| | 107 | | /// </param> |
| | 108 | | /// <param name='clientId'> |
| | 109 | | /// Bing uses this header to provide users with consistent behavior across Bing |
| | 110 | | /// API calls. Bing often flights new features and improvements, and it uses |
| | 111 | | /// the client ID as a key for assigning traffic on different flights. If you |
| | 112 | | /// do not use the same client ID for a user across multiple requests, then |
| | 113 | | /// Bing may assign the user to multiple conflicting flights. Being assigned to |
| | 114 | | /// multiple conflicting flights can lead to an inconsistent user experience. |
| | 115 | | /// For example, if the second request has a different flight assignment than |
| | 116 | | /// the first, the experience may be unexpected. Also, Bing can use the client |
| | 117 | | /// ID to tailor web results to that client ID’s search history, providing a |
| | 118 | | /// richer experience for the user. Bing also uses this header to help improve |
| | 119 | | /// result rankings by analyzing the activity generated by a client ID. The |
| | 120 | | /// relevance improvements help with better quality of results delivered by |
| | 121 | | /// Bing APIs and in turn enables higher click-through rates for the API |
| | 122 | | /// consumer. IMPORTANT: Although optional, you should consider this header |
| | 123 | | /// required. Persisting the client ID across multiple requests for the same |
| | 124 | | /// end user and device combination enables 1) the API consumer to receive a |
| | 125 | | /// consistent user experience, and 2) higher click-through rates via better |
| | 126 | | /// quality of results from the Bing APIs. Each user that uses your application |
| | 127 | | /// on the device must have a unique, Bing generated client ID. If you do not |
| | 128 | | /// include this header in the request, Bing generates an ID and returns it in |
| | 129 | | /// the X-MSEdge-ClientID response header. The only time that you should NOT |
| | 130 | | /// include this header in a request is the first time the user uses your app |
| | 131 | | /// on that device. Use the client ID for each Bing API request that your app |
| | 132 | | /// makes for this user on the device. Persist the client ID. To persist the ID |
| | 133 | | /// in a browser app, use a persistent HTTP cookie to ensure the ID is used |
| | 134 | | /// across all sessions. Do not use a session cookie. For other apps such as |
| | 135 | | /// mobile apps, use the device's persistent storage to persist the ID. The |
| | 136 | | /// next time the user uses your app on that device, get the client ID that you |
| | 137 | | /// persisted. Bing responses may or may not include this header. If the |
| | 138 | | /// response includes this header, capture the client ID and use it for all |
| | 139 | | /// subsequent Bing requests for the user on that device. ATTENTION: You must |
| | 140 | | /// ensure that this Client ID is not linkable to any authenticatable user |
| | 141 | | /// account information. If you include the X-MSEdge-ClientID, you must not |
| | 142 | | /// 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='market'> |
| | 189 | | /// The market where the results come from. Typically, mkt is the country where |
| | 190 | | /// the user is making the request from. However, it could be a different |
| | 191 | | /// country if the user is not located in a country where Bing delivers |
| | 192 | | /// results. The market must be in the form <language code>-<country |
| | 193 | | /// code>. For example, en-US. The string is case insensitive. For a list of |
| | 194 | | /// possible market values, see [Market |
| | 195 | | /// Codes](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-visual-search/supported-countries-mark |
| | 196 | | /// NOTE: If known, you are encouraged to always specify the market. Specifying |
| | 197 | | /// the market helps Bing route the request and return an appropriate and |
| | 198 | | /// optimal response. If you specify a market that is not listed in [Market |
| | 199 | | /// Codes](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-visual-search/supported-countries-mark |
| | 200 | | /// Bing uses a best fit market code based on an internal mapping that is |
| | 201 | | /// subject to change. |
| | 202 | | /// </param> |
| | 203 | | /// <param name='safeSearch'> |
| | 204 | | /// Filter the image results in actions with type 'VisualSearch' for adult |
| | 205 | | /// content. The following are the possible filter values. Off: May return |
| | 206 | | /// images with adult content. Moderate: Do not return images with adult |
| | 207 | | /// content. Strict: Do not return images with adult content. The default is |
| | 208 | | /// Moderate. If the request comes from a market that Bing's adult policy |
| | 209 | | /// requires that safeSearch is set to Strict, Bing ignores the safeSearch |
| | 210 | | /// value and uses Strict. If you use the site: filter in the knowledge |
| | 211 | | /// request, there is the chance that the response may contain adult content |
| | 212 | | /// regardless of what the safeSearch query parameter is set to. Use site: only |
| | 213 | | /// if you are aware of the content on the site and your scenario supports the |
| | 214 | | /// possibility of adult content. Possible values include: 'Off', 'Moderate', |
| | 215 | | /// 'Strict' |
| | 216 | | /// </param> |
| | 217 | | /// <param name='setLang'> |
| | 218 | | /// The language to use for user interface strings. Specify the language using |
| | 219 | | /// the ISO 639-1 2-letter language code. For example, the language code for |
| | 220 | | /// English is EN. The default is EN (English). Although optional, you should |
| | 221 | | /// always specify the language. Typically, you set setLang to the same |
| | 222 | | /// language specified by mkt unless the user wants the user interface strings |
| | 223 | | /// displayed in a different language. A user interface string is a string |
| | 224 | | /// that's used as a label in a user interface. There are few user interface |
| | 225 | | /// strings in the JSON response objects. Also, any links to Bing.com |
| | 226 | | /// properties in the response objects apply the specified language. |
| | 227 | | /// </param> |
| 1 | 228 | | /// <param name='knowledgeRequest'> |
| 1 | 229 | | /// The form data is a JSON object that identifies the image using an insights |
| | 230 | | /// token or URL to the image. The object may also include an optional crop |
| | 231 | | /// area that identifies an area of interest in the image. The insights token |
| | 232 | | /// and URL are mutually exclusive – do not specify both. You may specify |
| | 233 | | /// knowledgeRequest form data and image form data in the same request only if |
| | 234 | | /// knowledgeRequest form data specifies the cropArea field only (it must not |
| | 235 | | /// include an insights token or URL). |
| | 236 | | /// </param> |
| | 237 | | /// <param name='image'> |
| | 238 | | /// The form data is an image binary. The Content-Disposition header's name |
| | 239 | | /// parameter must be set to "image". You must specify an image binary if you |
| | 240 | | /// do not use knowledgeRequest form data to specify the image; you may not use |
| | 241 | | /// both forms to specify an image. You may specify knowledgeRequest form data |
| | 242 | | /// and image form data in the same request only if knowledgeRequest form data |
| | 243 | | /// specifies the cropArea field only (it must not include an insights token |
| | 244 | | /// or URL). |
| | 245 | | /// </param> |
| | 246 | | /// <param name='customHeaders'> |
| | 247 | | /// Headers that will be added to request. |
| | 248 | | /// </param> |
| | 249 | | /// <param name='cancellationToken'> |
| | 250 | | /// The cancellation token. |
| | 251 | | /// </param> |
| | 252 | | /// <exception cref="ErrorResponseException"> |
| | 253 | | /// Thrown when the operation returned an invalid status code |
| | 254 | | /// </exception> |
| | 255 | | /// <exception cref="SerializationException"> |
| | 256 | | /// Thrown when unable to deserialize the response |
| | 257 | | /// </exception> |
| | 258 | | /// <exception cref="ValidationException"> |
| | 259 | | /// Thrown when a required parameter is null |
| | 260 | | /// </exception> |
| | 261 | | /// <exception cref="System.ArgumentNullException"> |
| | 262 | | /// Thrown when a required parameter is null |
| | 263 | | /// </exception> |
| | 264 | | /// <return> |
| | 265 | | /// A response object containing the response body and response headers. |
| | 266 | | /// </return> |
| | 267 | | public async Task<HttpOperationResponse<ImageKnowledge>> VisualSearchMethodWithHttpMessagesAsync(string acceptLa |
| | 268 | | { |
| | 269 | | if (Client.Endpoint == null) |
| | 270 | | { |
| | 271 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 272 | | } |
| | 273 | | string xBingApisSDK = "true"; |
| | 274 | | // Tracing |
| | 275 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| | 276 | | string _invocationId = null; |
| | 277 | | if (_shouldTrace) |
| | 278 | | { |
| | 279 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| | 280 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| | 281 | | tracingParameters.Add("xBingApisSDK", xBingApisSDK); |
| | 282 | | tracingParameters.Add("acceptLanguage", acceptLanguage); |
| | 283 | | tracingParameters.Add("contentType", contentType); |
| | 284 | | tracingParameters.Add("userAgent", userAgent); |
| | 285 | | tracingParameters.Add("clientId", clientId); |
| | 286 | | tracingParameters.Add("clientIp", clientIp); |
| | 287 | | tracingParameters.Add("location", location); |
| | 288 | | tracingParameters.Add("market", market); |
| | 289 | | tracingParameters.Add("safeSearch", safeSearch); |
| | 290 | | tracingParameters.Add("setLang", setLang); |
| | 291 | | tracingParameters.Add("knowledgeRequest", knowledgeRequest); |
| | 292 | | tracingParameters.Add("image", image); |
| | 293 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| | 294 | | ServiceClientTracing.Enter(_invocationId, this, "VisualSearchMethod", tracingParameters); |
| | 295 | | } |
| | 296 | | // Construct URL |
| | 297 | | var _baseUrl = Client.BaseUri; |
| | 298 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "images/visualsearch"; |
| | 299 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 300 | | List<string> _queryParameters = new List<string>(); |
| | 301 | | if (market != null) |
| | 302 | | { |
| | 303 | | _queryParameters.Add(string.Format("mkt={0}", System.Uri.EscapeDataString(market))); |
| | 304 | | } |
| | 305 | | if (safeSearch != null) |
| | 306 | | { |
| | 307 | | _queryParameters.Add(string.Format("safeSearch={0}", System.Uri.EscapeDataString(safeSearch))); |
| | 308 | | } |
| | 309 | | if (setLang != null) |
| | 310 | | { |
| | 311 | | _queryParameters.Add(string.Format("setLang={0}", System.Uri.EscapeDataString(setLang))); |
| | 312 | | } |
| | 313 | | if (_queryParameters.Count > 0) |
| | 314 | | { |
| | 315 | | _url += "?" + string.Join("&", _queryParameters); |
| | 316 | | } |
| | 317 | | // Create HTTP transport objects |
| | 318 | | var _httpRequest = new HttpRequestMessage(); |
| | 319 | | HttpResponseMessage _httpResponse = null; |
| | 320 | | _httpRequest.Method = new HttpMethod("POST"); |
| | 321 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 322 | | // Set Headers |
| | 323 | | if (xBingApisSDK != null) |
| | 324 | | { |
| | 325 | | if (_httpRequest.Headers.Contains("X-BingApis-SDK")) |
| | 326 | | { |
| | 327 | | _httpRequest.Headers.Remove("X-BingApis-SDK"); |
| | 328 | | } |
| | 329 | | _httpRequest.Headers.TryAddWithoutValidation("X-BingApis-SDK", xBingApisSDK); |
| | 330 | | } |
| | 331 | | if (acceptLanguage != null) |
| | 332 | | { |
| | 333 | | if (_httpRequest.Headers.Contains("Accept-Language")) |
| | 334 | | { |
| | 335 | | _httpRequest.Headers.Remove("Accept-Language"); |
| | 336 | | } |
| | 337 | | _httpRequest.Headers.TryAddWithoutValidation("Accept-Language", acceptLanguage); |
| | 338 | | } |
| | 339 | | if (userAgent != null) |
| | 340 | | { |
| | 341 | | if (_httpRequest.Headers.Contains("User-Agent")) |
| | 342 | | { |
| | 343 | | _httpRequest.Headers.Remove("User-Agent"); |
| | 344 | | } |
| | 345 | | _httpRequest.Headers.TryAddWithoutValidation("User-Agent", userAgent); |
| | 346 | | } |
| | 347 | | if (clientId != null) |
| | 348 | | { |
| | 349 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientID")) |
| | 350 | | { |
| | 351 | | _httpRequest.Headers.Remove("X-MSEdge-ClientID"); |
| | 352 | | } |
| | 353 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientID", clientId); |
| | 354 | | } |
| | 355 | | if (clientIp != null) |
| | 356 | | { |
| | 357 | | if (_httpRequest.Headers.Contains("X-MSEdge-ClientIP")) |
| | 358 | | { |
| | 359 | | _httpRequest.Headers.Remove("X-MSEdge-ClientIP"); |
| | 360 | | } |
| | 361 | | _httpRequest.Headers.TryAddWithoutValidation("X-MSEdge-ClientIP", clientIp); |
| | 362 | | } |
| | 363 | | if (location != null) |
| | 364 | | { |
| | 365 | | if (_httpRequest.Headers.Contains("X-Search-Location")) |
| | 366 | | { |
| | 367 | | _httpRequest.Headers.Remove("X-Search-Location"); |
| | 368 | | } |
| | 369 | | _httpRequest.Headers.TryAddWithoutValidation("X-Search-Location", location); |
| | 370 | | } |
| | 371 | |
|
| | 372 | |
|
| | 373 | | if (customHeaders != null) |
| | 374 | | { |
| | 375 | | foreach(var _header in customHeaders) |
| | 376 | | { |
| | 377 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 378 | | { |
| | 379 | | _httpRequest.Headers.Remove(_header.Key); |
| | 380 | | } |
| | 381 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 382 | | } |
| | 383 | | } |
| | 384 | |
|
| | 385 | | // Serialize Request |
| | 386 | | string _requestContent = null; |
| | 387 | | MultipartFormDataContent _multiPartContent = new MultipartFormDataContent(); |
| | 388 | | if (knowledgeRequest != null) |
| | 389 | | { |
| | 390 | | StringContent _knowledgeRequest = new StringContent(knowledgeRequest, System.Text.Encoding.UTF8); |
| | 391 | | _multiPartContent.Add(_knowledgeRequest, "knowledgeRequest"); |
| | 392 | | } |
| | 393 | | if (image != null) |
| | 394 | | { |
| | 395 | | StreamContent _image = new StreamContent(image); |
| | 396 | | _image.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); |
| | 397 | | ContentDispositionHeaderValue _contentDispositionHeaderValue = new ContentDispositionHeaderValue("form-d |
| | 398 | | _contentDispositionHeaderValue.Name = "image"; |
| | 399 | | // get filename from stream if it's a file otherwise, just use 'unknown' |
| | 400 | | var _fileStream = image as FileStream; |
| | 401 | | var _fileName = (_fileStream != null ? _fileStream.Name : null) ?? "unknown"; |
| 292 | 402 | | if(System.Linq.Enumerable.Any(_fileName, c => c > 127) ) |
| | 403 | | { |
| | 404 | | // non ASCII chars detected, need UTF encoding: |
| | 405 | | _contentDispositionHeaderValue.FileNameStar = _fileName; |
| | 406 | | } |
| | 407 | | else |
| | 408 | | { |
| | 409 | | // ASCII only |
| | 410 | | _contentDispositionHeaderValue.FileName = _fileName; |
| | 411 | | } |
| | 412 | | _image.Headers.ContentDisposition = _contentDispositionHeaderValue; |
| | 413 | | _multiPartContent.Add(_image, "image"); |
| | 414 | | } |
| | 415 | | _httpRequest.Content = _multiPartContent; |
| | 416 | | // Set Credentials |
| | 417 | | if (Client.Credentials != null) |
| | 418 | | { |
| | 419 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 420 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 421 | | } |
| | 422 | | // Send Request |
| | 423 | | if (_shouldTrace) |
| | 424 | | { |
| | 425 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 426 | | } |
| | 427 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 428 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 429 | | if (_shouldTrace) |
| | 430 | | { |
| | 431 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 432 | | } |
| | 433 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| | 434 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 435 | | string _responseContent = null; |
| | 436 | | if ((int)_statusCode != 200) |
| | 437 | | { |
| | 438 | | var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _st |
| | 439 | | try |
| | 440 | | { |
| | 441 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 442 | | ErrorResponse _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<ErrorResponse>(_res |
| | 443 | | if (_errorBody != null) |
| | 444 | | { |
| | 445 | | ex.Body = _errorBody; |
| | 446 | | } |
| | 447 | | } |
| | 448 | | catch (JsonException) |
| | 449 | | { |
| | 450 | | // Ignore the exception |
| | 451 | | } |
| | 452 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| | 453 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| | 454 | | if (_shouldTrace) |
| | 455 | | { |
| | 456 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 457 | | } |
| | 458 | | _httpRequest.Dispose(); |
| | 459 | | if (_httpResponse != null) |
| | 460 | | { |
| | 461 | | _httpResponse.Dispose(); |
| | 462 | | } |
| | 463 | | throw ex; |
| | 464 | | } |
| | 465 | | // Create Result |
| | 466 | | var _result = new HttpOperationResponse<ImageKnowledge>(); |
| | 467 | | _result.Request = _httpRequest; |
| | 468 | | _result.Response = _httpResponse; |
| | 469 | | // Deserialize Response |
| | 470 | | if ((int)_statusCode == 200) |
| | 471 | | { |
| | 472 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 473 | | try |
| | 474 | | { |
| | 475 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<ImageKnowledge>(_responseContent |
| | 476 | | } |
| | 477 | | catch (JsonException ex) |
| | 478 | | { |
| | 479 | | _httpRequest.Dispose(); |
| | 480 | | if (_httpResponse != null) |
| | 481 | | { |
| | 482 | | _httpResponse.Dispose(); |
| | 483 | | } |
| | 484 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 485 | | } |
| | 486 | | } |
| | 487 | | if (_shouldTrace) |
| | 488 | | { |
| | 489 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 490 | | } |
| | 491 | | return _result; |
| | 492 | | } |
| | 493 | |
|
| | 494 | | } |
| | 495 | | } |