예제 #1
0
    public async ValueTask <T> GetAsync <T>(string serviceName, string endpoint, EndpointAuthentication endpointAuthentication, CancellationToken cancellationToken)
    {
        if (!_serviceAddresses.ContainsKey(serviceName))
        {
            throw new InvalidOperationException("Service is not registered in service discovery.");
        }

        var uri = $"{_serviceAddresses[serviceName]}/{endpoint}";

        var endpointAuthenticationType = endpointAuthentication.AuthenticationType;
        var accessToken = endpointAuthenticationType switch
        {
            EndpointAuthenticationType.Profile => await _profileTokenService.GetProfileAccessTokenAsync(cancellationToken).ConfigureAwait(false),
            EndpointAuthenticationType.Service => endpointAuthentication.Credentials == null
                ? await _serviceTokenService.GetServiceAccessTokenAsync(cancellationToken).ConfigureAwait(false)
                : await _serviceTokenService.GetServiceAccessTokenAsync(endpointAuthentication.Credentials, cancellationToken).ConfigureAwait(false),
            _ => null
        };

        try
        {
            using var message    = new HttpRequestMessage(HttpMethod.Get, uri);
            using var httpClient = _httpClientFactory.CreateClient();
            return(await httpClient.SendMessageAsync <T>(message, accessToken, cancellationToken)
                   .ConfigureAwait(false));
        }
        catch (HttpRequestException exception)
        {
            // TODO: Consider placing it in some other place of code, like IHttpClient.
            // TODO: Fix the issue that if endpoint return STRUCT - we return default value when it's not found!
            // TODO: What if URL is incorrect - it shouldn't return default value.
            // TODO: If object is valueobject - we cannot return default value. We always need to return NULL.
            // Consider wrapping response in some kind of Result object.
            if (exception.StatusCode == HttpStatusCode.NotFound)
            {
                return(default !);
 public ValueTask <string> SignInAsync(CancellationToken cancellationToken)
 {
     return(_profileTokenService.GetProfileAccessTokenAsync(cancellationToken));
 }