예제 #1
0
        public static async Task <T> PostEmptyBodyAsync <T>(this ServiceManagementHttpClient client, string relativeUri, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = await client.PostAsync(relativeUri, null, cancellationToken).ConfigureAwait(false);

            await client.EnsureSuccessOrThrow(response).ConfigureAwait(false);

            return(await client.ReadContentAs <T>(response).ConfigureAwait(false));
        }
예제 #2
0
        public static Task <IoTHubListResponse> GetIoTHubsAsync(this ServiceManagementHttpClient client, CancellationToken cancellationToken)
        {
            string relativeUrl = string.Format(CultureInfo.InvariantCulture,
                                               "subscriptions/{0}/providers/Microsoft.Devices/IoTHubs?api-version={1}",
                                               client.SubscriptionId,
                                               IoTHubPreviewApiVersion);

            return(client.GetAsync <IoTHubListResponse>(relativeUrl, cancellationToken));
        }
예제 #3
0
        public static async Task <IoTHub> GetIoTHubDetailsAsync(this ServiceManagementHttpClient client, IoTHub iotHubAccount, CancellationToken cancellationToken)
        {
            /// POST:
            ///  subscriptions/{subscriptionId}/resourceGroups/
            ///  {resourceGroupName}/providers/Microsoft.Devices/IotHubs/
            ///  {IotHubName}/IoTHubKeys/listkeys?api-version={api-version}

            string relativeUrl = string.Format(CultureInfo.InvariantCulture,
                                               "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Devices/IotHubs/{2}/IoTHubKeys/listKeys?api-version={3}",
                                               client.SubscriptionId,
                                               Uri.EscapeDataString(iotHubAccount.ResourceGroup),
                                               Uri.EscapeDataString(iotHubAccount.Name),
                                               IoTHubPreviewApiVersion);

            AuthorizationPolicies authorizationPolicies = await client.PostEmptyBodyAsync <AuthorizationPolicies>(relativeUrl, cancellationToken).ConfigureAwait(false);

            iotHubAccount.AuthorizationPolicies = authorizationPolicies;
            return(iotHubAccount);
        }
예제 #4
0
        public async Task <ServiceManagementHttpClient> CreateAsync()
        {
            if (this.UserAgent == null)
            {
                throw new InvalidOperationException("UserAgent is null.");
            }

            if (string.IsNullOrEmpty(this.MsVersion))
            {
                throw new InvalidOperationException("MsVersion is null or empty.");
            }

            if (this.Formatter == null)
            {
                throw new InvalidOperationException("Formatter is null.");
            }

            var webRequestHandler = new WebRequestHandler();

            //
            // Build a ServiceManagementHttpClientHandler.
            //
            var clientHandler = await BuildServiceManagementHttpClientHandlerAsync().ConfigureAwait(false);

            clientHandler.InnerHandler = webRequestHandler;

            Debug.Assert(_azureRMSubscription != null);
            string subscriptionId = _azureRMSubscription.SubscriptionId;

            //
            // Build a http client.
            //
            var client = new ServiceManagementHttpClient(clientHandler, this.Formatter, subscriptionId);

            client.BaseAddress = _azureRMSubscription.ResourceManagementEndpointUri;

            return(client);
        }
예제 #5
0
        public static async Task EnsureSuccessOrThrow(this ServiceManagementHttpClient client, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                // if it is not successful, try to get the exception message from the response
                AzureErrorResponse errorResponse = await client.ReadContentAs <AzureErrorResponse>(response);

                string code    = errorResponse?.Error?.Code;
                string message = errorResponse?.Error?.Message;
                if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(message))
                {
                    if (code == "DisallowedProvider")
                    {
                        throw new InvalidOperationException("AzureResourceManagementResources.DisallowedProviderErrorMessge");
                    }

                    throw new InvalidOperationException(message);
                }
            }

            // if we didn't get an exception message from the response, just ensure a successful status code
            response.EnsureSuccessStatusCode();
        }