コード例 #1
0
        public async Task <IamTokenCreateResult> GetIamToken(TokenRecipientOptions options, CancellationToken cancellationToken = default)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested();

            var jsonContent = !string.IsNullOrWhiteSpace(options.JwtToken) ?
                              JsonBodyHelper.JwtBody(options.JwtToken) : JsonBodyHelper.OauthBody(options.OauthToken);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, options.EndPoint);
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            request.Content = content;


            var httpOptions = new HttpClientOptions()
            {
                HttpClientTimeout = options.HttpClientTimeout,
#if !NETCOREAPP
                EndPoint = options.EndPoint
#endif
            };


            return(await ServiceCaller.CallService <IamTokenCreateResult>(httpOptions, async (client) =>
            {
                var httpResponse = await client.SendAsync(request, cancellationToken);

                var stream = await httpResponse.Content.ReadAsStreamAsync();
                var resultContent = await new StreamReader(stream).ReadToEndAsync();
                if (httpResponse.IsSuccessStatusCode)
                {
                    try
                    {
                        return JsonBodyHelper.DeserializeResult(resultContent);
                    }
                    catch (Exception ex)
                    {
                        throw new YandexIamServiceException(resultContent, ex, httpResponse.StatusCode);
                    }
                }
                else
                {
                    throw new YandexIamServiceException(resultContent, httpResponse.StatusCode);
                }
            }));
        }
コード例 #2
0
 public static async Task <IamTokenCreateResult> GetIamTokenWithOAuth(this ITokenRecipient client, string token, CancellationToken cancellationToken = default)
 {
     return(await client.GetIamToken(TokenRecipientOptions.WithOAuthToken(token), cancellationToken));
 }