public async Task <JObject> GetTokensAsync(string username, string password, string tenantId)
        {
            var res = await SharedHttpClient.SendAsync(
                new HttpRequestMessage(HttpMethod.Post, this._endpoint + "/tokens")
            {
                Headers =
                {
                    Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
                },
                Content = CreateJsonContent(new
                {
                    auth = new
                    {
                        passwordCredentials = new { username, password },
                        tenantId,
                    }
                }),
            }
                ).ConfigureAwait(false);

            using (res)
            {
                if (!res.IsSuccessStatusCode && string.Equals(res.Content?.Headers.ContentType?.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
                {
                    // エラーレスポンス
                    throw new ConoHaApiException(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
                }

                var s = await res.EnsureSuccessStatusCode().Content
                        .ReadAsStringAsync().ConfigureAwait(false);

                return(JObject.Parse(s));
            }
        }
Ejemplo n.º 2
0
        private async Task <HttpResponseMessage> SendRequestAsync(HttpMethod method, string requestUri, HttpContent content)
        {
            var req = new HttpRequestMessage(method, requestUri)
            {
                Headers =
                {
                    Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
                },
                Content = content,
            };

            req.Headers.TryAddWithoutValidation("X-Auth-Token", this._token);

            var res = await SharedHttpClient.SendAsync(req).ConfigureAwait(false);

            if (!res.IsSuccessStatusCode && string.Equals(res.Content?.Headers.ContentType?.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
            {
                // エラーレスポンス
                using (res) throw new ConoHaApiException(await res.Content.ReadAsStringAsync().ConfigureAwait(false));
            }

            return(res.EnsureSuccessStatusCode());
        }