/// <summary>
        ///     Query Azure Management API for the IotHub metrics.
        ///     More details in following docs:
        ///     https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-rest-api-walkthrough
        ///     https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-metrics
        ///     https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list
        /// </summary>
        /// <param name="requestList"></param>
        public async Task <MetricsResponseListModel> PostAsync(MetricsRequestListModel requestList)
        {
            if (this.AccessTokenIsNullOrEmpty())
            {
                await this.GetAadTokenAsync();
            }

            // Renew access token 10 minutes before it's expire time
            if (this.AccessTokenExpireSoon())
            {
                this.GetAadTokenAsync();
            }

            if (requestList == null)
            {
                requestList = this.GetDefaultMetricsRequests();
            }

            var accessToken = $"Bearer {this.ReadSecureString(this.secureAccessToken)}";

            var request = this.PrepareRequest($"batch?api-version={this.config.AzureManagementAdapterApiVersion}", accessToken, requestList);

            this.log.Debug("Azure Management request content", () => new { request.Content });

            var response = await this.httpClient.PostAsync(request);

            this.log.Debug("Azure Management response", () => new { response });

            this.ThrowIfError(response);

            return(JsonConvert.DeserializeObject <MetricsResponseListModel>(response.Content));
        }
Example #2
0
        /// <summary>
        ///     Query Azure Management API for the IotHub metrics.
        ///     More details in following docs:
        ///     https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-rest-api-walkthrough
        ///     https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-metrics
        ///     https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list
        /// </summary>
        /// <param name="requestList"></param>
        public async Task <MetricsResponseListModel> PostAsync(MetricsRequestListModel requestList)
        {
            await this.CreateOrUpdateAccessTokenAsync();

            if (requestList == null)
            {
                requestList = this.GetDefaultMetricsRequests();
            }

            var accessToken = $"Bearer {this.ReadSecureString(this.secureAccessToken)}";

            var request = this.PrepareRequest($"batch?api-version={this.config.AzureManagementAdapterApiVersion}", accessToken, requestList);

            this.log.Debug("Azure Management request content", () => new { request.Content });

            var response = await this.httpClient.PostAsync(request);

            this.log.Debug("Azure Management response", () => new { response });

            this.ThrowIfError(response);

            var metricsResponseList = JsonConvert.DeserializeObject <MetricsResponseListModel>(response.Content);

            foreach (var responseModel in metricsResponseList.Responses)
            {
                if (responseModel.Content.Error != null)
                {
                    throw new ExternalDependencyException(responseModel.Content.Error.Message);
                }
            }

            return(metricsResponseList);
        }
        private MetricsRequestListModel GetDefaultMetricsRequests()
        {
            var request = new MetricsRequestModel
            {
                HttpMethod  = "GET",
                RelativeUrl = this.GetDefaultIoTHubMetricsUrl()
            };

            var result = new MetricsRequestListModel();

            result.Requests.Add(request);

            return(result);
        }
Example #4
0
        private HttpRequest PrepareRequest(string path, string token, MetricsRequestListModel content = null)
        {
            var request = new HttpRequest();

            request.AddHeader(HttpRequestHeader.Accept.ToString(), "application/json");
            request.AddHeader(HttpRequestHeader.CacheControl.ToString(), "no-cache");
            request.AddHeader(HttpRequestHeader.Authorization.ToString(), token);
            request.SetUriFromString($"{this.config.AzureManagementAdapterApiUrl}/{path}");
            request.Options.EnsureSuccess = false;
            request.Options.Timeout       = this.config.AzureManagementAdapterApiTimeout;

            if (content != null)
            {
                request.SetContent(content);
            }

            this.log.Debug("Azure Management request", () => new { request });

            return(request);
        }
        private HttpRequest PrepareRequest(string path, string token, MetricsRequestListModel content = null)
        {
            var request = new HttpRequest();

            request.AddHeader(HttpRequestHeader.Accept.ToString(), "application/json");
            request.AddHeader(HttpRequestHeader.CacheControl.ToString(), "no-cache");
            request.AddHeader(HttpRequestHeader.Authorization.ToString(), token);
            request.SetUriFromString($"{this.config.AzureManagementAdapterApiUrl}/{path}");
            request.Options.EnsureSuccess = false;
            request.Options.Timeout       = this.config.AzureManagementAdapterApiTimeout;
            if (!this.config.AzureManagementAdapterApiUrl.ToLowerInvariant().StartsWith("https:"))
            {
                throw new InvalidConfigurationException("Azure Management API url must start with https");
            }

            if (content != null)
            {
                request.SetContent(content);
            }

            this.log.Debug("Azure Management request", () => new { request });

            return(request);
        }