Esempio n. 1
0
        private IEnumerable <BlobResultSegment> EnumerateDirectoryBlobs(CloudBlobDirectory cloudBlobDirectory)
        {
            Log.Info($"enter {cloudBlobDirectory.Uri}");
            BlobResultSegment     resultSegment = default(BlobResultSegment);
            BlobContinuationToken blobToken     = null;

            while (true)
            {
                resultSegment = _blobChildTasks.TaskFunction((blobresultsegment) =>
                                                             cloudBlobDirectory.ListBlobsSegmentedAsync(
                                                                 false,
                                                                 BlobListingDetails.None,
                                                                 MaxResults,
                                                                 blobToken,
                                                                 null,
                                                                 null).Result as BlobResultSegment).Result as BlobResultSegment;

                blobToken = resultSegment.ContinuationToken;
                yield return(resultSegment);

                if (blobToken == null)
                {
                    break;
                }
            }

            Log.Info($"exit {cloudBlobDirectory.Uri}");
        }
Esempio n. 2
0
        public bool SendRequest(
            string uri,
            string authToken      = null,
            string jsonBody       = null,
            HttpMethod httpMethod = null,
            Dictionary <string, string> headers = null,
            HttpStatusCode okStatus             = HttpStatusCode.OK)
        {
            HttpContent httpContent = default(HttpContent);

            httpMethod = httpMethod ?? Method;
            Log.Info($"enter:method: {httpMethod} uri: {uri}", ConsoleColor.Magenta, ConsoleColor.Black);

            try
            {
                if (!string.IsNullOrEmpty(jsonBody))
                {
                    byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonBody);
                    httpContent = new ByteArrayContent(jsonBytes);
                    httpContent.Headers.ContentType   = new MediaTypeHeaderValue("application/json");
                    httpContent.Headers.ContentLength = jsonBytes.Length;

                    Log.Info($"json bytes:{jsonBytes.Length} uri:{uri}", ConsoleColor.Magenta, ConsoleColor.Black);
                }

                HttpRequestMessage request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(uri),
                    Method     = httpMethod,
                    Content    = httpContent,
                };

                if (authToken != null)
                {
                    request.Headers.Add("Authorization", $"Bearer {authToken}");
                }

                if (headers != null)
                {
                    foreach (KeyValuePair <string, string> header in headers)
                    {
                        request.Headers.Add(header.Key, header.Value);
                    }
                }

                Response = _httpTasks.TaskFunction((httpresponse) => _httpClient.SendAsync(request).Result).Result as HttpResponseMessage;
                Log.Info($"response status: {Response.StatusCode}", ConsoleColor.DarkMagenta, ConsoleColor.Black);
                StatusCode = Response.StatusCode;

                if (Response.Content.Headers.ContentLength > 0)
                {
                    ResponseStreamString = Response.Content.ReadAsStringAsync().Result;

                    if (!string.IsNullOrEmpty(ResponseStreamString))
                    {
                        ResponseStreamJson = JObject.Parse(ResponseStreamString);
                        if (DisplayResponse)
                        {
                            Log.Info($"WebResponse stream: bytes: {Response.Content.Headers.ContentLength}\r\n{ResponseStreamJson}", ConsoleColor.DarkMagenta, ConsoleColor.Black);
                        }
                    }
                }
                else
                {
                    ResponseStreamJson   = new JObject();
                    ResponseStreamString = string.Empty;
                    Log.Info("no responseStream");
                }

                Log.Debug("response headers:", Response.Headers);

                if (Response.IsSuccessStatusCode || Response.StatusCode == okStatus)
                {
                    return(Success = true);
                }

                Log.Error("unsuccessful response:", Response);
                return(Success = false);
            }
            catch (WebException we)
            {
                StatusCode = ((HttpWebResponse)we.Response).StatusCode;
                Log.Error($"webexception: {we.Status}: {(int)StatusCode}", we.Response);
                return(Success = false);
            }
            catch (Exception e)
            {
                Log.Exception($"post exception:{e}");
                return(Success = false);
            }
        }