private async Task <HttpClient> GetRemoteZipClient(Uri url)
        {
            var client = new HttpClient
            {
                BaseAddress = url,
                MaxResponseContentBufferSize = 30 * 1024 * 1024,
                Timeout = Timeout.InfiniteTimeSpan
            };
            var token = await _tokenManager.GetToken(_settings.CurrentTenant);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            return(client);
        }
        private async Task <HttpResponseMessage> HttpInvoke(Uri uri, string verb, object objectPayload)
        {
            var payload = JsonConvert.SerializeObject(objectPayload);

            using (var client = new HttpClient(new HttpClientHandler()))
            {
                const string jsonContentType = "application/json";
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {await _tokenManager.GetToken(_currentTenant)}");
                client.DefaultRequestHeaders.Add("User-Agent", "functions-cli/2.0");
                client.DefaultRequestHeaders.Add("Accept", jsonContentType);
                client.DefaultRequestHeaders.Add("x-ms-request-id", Guid.NewGuid().ToString());

                HttpResponseMessage response = null;
                if (String.Equals(verb, "get", StringComparison.OrdinalIgnoreCase))
                {
                    response = await client.GetAsync(uri).ConfigureAwait(false);
                }
                else if (String.Equals(verb, "delete", StringComparison.OrdinalIgnoreCase))
                {
                    response = await client.DeleteAsync(uri).ConfigureAwait(false);
                }
                else if (String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase))
                {
                    response = await client.PostAsync(uri, new StringContent(payload ?? String.Empty, Encoding.UTF8, jsonContentType)).ConfigureAwait(false);
                }
                else if (String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase))
                {
                    response = await client.PutAsync(uri, new StringContent(payload ?? String.Empty, Encoding.UTF8, jsonContentType)).ConfigureAwait(false);
                }
                else if (String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase))
                {
                    using (var message = new HttpRequestMessage(new HttpMethod("PATCH"), uri))
                    {
                        message.Content = new StringContent(payload ?? String.Empty, Encoding.UTF8, jsonContentType);
                        response        = await client.SendAsync(message).ConfigureAwait(false);
                    }
                }
                else
                {
                    throw new InvalidOperationException(String.Format("Invalid http verb '{0}'!", verb));
                }

                return(response);
            }
        }
Exemplo n.º 3
0
        public override async Task RunAsync()
        {
            var functionApp = await _armManager.GetFunctionAppAsync(FunctionAppName);

            if (functionApp != null)
            {
                await RetryHelper.Retry(async() =>
                {
                    using (var client = new HttpClient()
                    {
                        BaseAddress = new Uri($"https://{functionApp.ScmUri}")
                    })
                    {
                        var token = await _tokenManager.GetToken(_settings.CurrentTenant);
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                        var response = await client.GetAsync(new Uri("api/functions", UriKind.Relative));

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new CliException($"Error trying to retrieve list of functions ({response.StatusCode}).");
                        }

                        var functions = await response.Content.ReadAsAsync <IEnumerable <FunctionInfo> >();

                        ColoredConsole.WriteLine(TitleColor($"Functions in {FunctionAppName}:"));
                        foreach (var function in functions)
                        {
                            var trigger = function
                                          .Config?["bindings"]
                                          ?.FirstOrDefault(o => o["type"]?.ToString().IndexOf("Trigger", StringComparison.OrdinalIgnoreCase) != -1)
                                ?["type"];

                            trigger = trigger ?? "No Trigger Found";

                            ColoredConsole.WriteLine($"    {function.Name} - [{VerboseColor(trigger.ToString())}]");
                        }
                    }
                }, 2);
            }
            else
            {
                ColoredConsole.Error.WriteLine(ErrorColor($"Can't find function app by name {FunctionAppName}"));
            }
        }
Exemplo n.º 4
0
        public async Task <IEnumerable <TenantSubscriptionMap> > GetTenants()
        {
            var tenants = await _tokenManager.GetTenants();

            var tokenList = await tenants.Select(async t => new { Tenant = t, Token = await _tokenManager.GetToken(t) }).WhenAll();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(Constants.ArmConstants.ArmDomain);
                return(await tokenList
                       .Select(async t =>
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, "/subscriptions?api-version=2014-04-01");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", t.Token);
                    var response = await client.SendAsync(request);
                    if (response.IsSuccessStatusCode)
                    {
                        var resString = await response.Content.ReadAsStringAsync();
                        var subscriptions = JsonConvert.DeserializeObject <ArmSubscriptionsArray>(resString);
                        return new TenantSubscriptionMap
                        {
                            TenantId = t.Tenant,
                            Subscriptions = subscriptions.Value
                        };
                    }
                    else
                    {
                        return null;
                    }
                })
                       .WhenAll());
            }
        }