Example #1
0
        /// <summary>
        /// Creates <see cref="HttpRequestMessage"/> to use for MultipartFormData request.
        /// </summary>
        /// <param name="method"><see cref="HttpMethod"/> to be used on requesting.</param>
        /// <param name="uri">Uri to be requested.</param>
        /// <param name="queryParameters">Query parameter collection.</param>
        /// <param name="stringData">String data collection.</param>
        /// <param name="fileData">File data list.</param>
        /// <returns><see cref="HttpRequestMessage"/> that is created.</returns>
        public HttpRequestMessage CreateMultipartFormDataRequest(HttpMethod method, Uri uri, NameValueCollection queryParameters = null, NameValueCollection stringData = null, SparkFileData fileData = null)
        {
            var request = new HttpRequestMessage(method, HttpUtils.BuildUri(uri, queryParameters));

            var headers = request.Headers;

            headers.AcceptCharset.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue(ENCODING.WebName));
            headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(MEDIA_TYPE_APPLICATION_JSON));

            var content = new MultipartFormDataContent();

            if (stringData != null)
            {
                foreach (var key in stringData.AllKeys)
                {
                    var values = stringData.GetValues(key);

                    if (values != null)
                    {
                        foreach (var item in values)
                        {
                            if (item != null)
                            {
                                content.Add(new StringContent(item, ENCODING), key);
                            }
                        }
                    }
                }
            }

            if (fileData != null)
            {
                var sc = new StreamContent(fileData.Stream);

                sc.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(fileData.MediaTypeName);

                content.Add(sc, "files", fileData.FileName);
            }

            request.Content = content;

            return(request);
        }
Example #2
0
 /// <summary>
 /// Requests MultipartFormData to Cisco Spark API.
 /// </summary>
 /// <typeparam name="TSparkResult">Type of SparkResult to be returned.</typeparam>
 /// <typeparam name="TSparkObject">Type of SparkObject to be returned.</typeparam>
 /// <param name="method"><see cref="HttpMethod"/> to be used on requesting.</param>
 /// <param name="uri">Uri to be requested.</param>
 /// <param name="queryParameters">Query parameter collection.</param>
 /// <param name="stringData">String data collection.</param>
 /// <param name="fileData">File data list.</param>
 /// <param name="cancellationToken"><see cref="CancellationToken"/> to be used cancellation.</param>
 /// <returns><see cref="SparkResult{TSparkObject}"/> that represents result of API request.</returns>
 public async Task <TSparkResult> RequestMultipartFormDataAsync <TSparkResult, TSparkObject>(HttpMethod method, Uri uri, NameValueCollection queryParameters = null, NameValueCollection stringData = null, SparkFileData fileData = null, CancellationToken?cancellationToken = null)
     where TSparkResult : SparkResult <TSparkObject>, new()
     where TSparkObject : SparkObject, new()
 {
     return(await RequestAsync <TSparkResult, TSparkObject>(CreateMultipartFormDataRequest(method, uri, queryParameters, stringData, fileData), cancellationToken));
 }