Example #1
0
 /// <summary>
 /// Requests 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="objectToBePosted">Object inherited from <see cref="SparkObject"/> to be sent to Spark API.</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> RequestJsonAsync <TSparkResult, TSparkObject>(HttpMethod method, Uri uri, NameValueCollection queryParameters = null, SparkObject objectToBePosted = null, CancellationToken?cancellationToken = null)
     where TSparkResult : SparkResult <TSparkObject>, new()
     where TSparkObject : SparkObject, new()
 {
     return(await RequestAsync <TSparkResult, TSparkObject>(CreateJsonRequest(method, uri, queryParameters, objectToBePosted), cancellationToken));
 }
Example #2
0
        /// <summary>
        /// Creates <see cref="HttpRequestMessage"/> to use for Json 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="objectToBePosted">Object inherited from <see cref="SparkObject"/> to be sent to Spark API.</param>
        /// <returns><see cref="HttpRequestMessage"/> that is created.</returns>
        public HttpRequestMessage CreateJsonRequest(HttpMethod method, Uri uri, NameValueCollection queryParameters = null, SparkObject objectToBePosted = 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));

            if (objectToBePosted != null)
            {
                request.Content = new StringContent(
                    objectToBePosted.ToJsonString(),
                    ENCODING,
                    MEDIA_TYPE_APPLICATION_JSON);
            }

            return(request);
        }
Example #3
0
        /// <summary>
        /// Requests 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="request"><see cref="HttpRequestMessage"/> to be requested.</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> RequestAsync <TSparkResult, TSparkObject>(HttpRequestMessage request, CancellationToken?cancellationToken = null)
            where TSparkResult : SparkResult <TSparkObject>, new()
            where TSparkObject : SparkObject, new()
        {
            var result = new TSparkResult();

            using (request)
                using (request.Content)
                {
                    var httpClient = selectHttpClient(request.RequestUri);

                    HttpResponseMessage response;

                    if (cancellationToken.HasValue)
                    {
                        response = await httpClient.SendAsync(request, cancellationToken.Value);
                    }
                    else
                    {
                        response = await httpClient.SendAsync(request);
                    }

                    using (response)
                    {
                        if (response.StatusCode != System.Net.HttpStatusCode.NoContent && response.Content != null)
                        {
                            using (var content = response.Content)
                            {
                                var contentHeaders = content.Headers;

                                if (contentHeaders.ContentType?.MediaType == MEDIA_TYPE_APPLICATION_JSON)
                                {
                                    var body = await content.ReadAsStringAsync();

                                    if (!String.IsNullOrEmpty(body))
                                    {
                                        result.Data = SparkObject.FromJsonString <TSparkObject>(body);
                                    }
                                }
                                else
                                {
                                    var info = new TSparkObject() as SparkFileInfo;

                                    if (info != null)
                                    {
                                        string fileName = contentHeaders.ContentDisposition?.FileName;

                                        if (fileName != null && fileName.StartsWith("\"") && fileName.EndsWith("\""))
                                        {
                                            fileName = fileName.Substring(1, (fileName.Length - 2));
                                        }

                                        info.FileName      = fileName;
                                        info.MediaTypeName = contentHeaders.ContentType?.MediaType;
                                        info.Size          = contentHeaders.ContentLength;
                                    }

                                    var data = info as SparkFileData;

                                    if (data != null)
                                    {
                                        data.Stream = new MemoryStream();

                                        await content.CopyToAsync(data.Stream);

                                        data.Stream.Position = 0;
                                    }

                                    result.Data = ((data != null) ? data : info) as TSparkObject;
                                }
                            }
                        }


                        if (result.Data == null)
                        {
                            result.Data = new TSparkObject();

                            result.Data.HasValues = false;
                        }


                        result.HttpStatusCode = response.StatusCode;

                        var headers = response.Headers;

                        if (headers.Contains(HEADER_NAME_TRACKING_ID))
                        {
                            foreach (var item in headers.GetValues(HEADER_NAME_TRACKING_ID))
                            {
                                result.TrackingId = item;
                                break;
                            }
                        }

                        result.RetryAfter = headers.RetryAfter;


                        if (result is SparkListResult <TSparkObject> )
                        {
                            if (headers.Contains(HEADER_NAME_LINK))
                            {
                                var listResult = result as SparkListResult <TSparkObject>;

                                if (listResult != null)
                                {
                                    foreach (var item in headers.GetValues(HEADER_NAME_LINK))
                                    {
                                        if (!String.IsNullOrEmpty(item))
                                        {
                                            var m = LINK_NEXT_PATTERN.Match(item);

                                            if (m.Success)
                                            {
                                                var g = m.Groups["NEXTURI"];

                                                if (g != null && !String.IsNullOrEmpty(g.Value))
                                                {
                                                    listResult.NextUri         = new Uri(g.Value);
                                                    listResult.SparkHttpClient = this;

                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // Result status is once set based on Http Status code.
                        // The exact criteria differs in each API.
                        // This value will be adjusted in each SparkAPIClient class.
                        result.IsSuccessStatus = response.IsSuccessStatusCode;
                    }
                }

            return(result);
        }