Exemplo n.º 1
0
        public async Task <Comment> RemoveAsync(int id)
        {
            var tokenResult = await tokenProvider.RequestAccessToken(new AccessTokenRequestOptions()
            {
                Scopes = new string[] { "commentsgrpc" }
            });

            if (tokenResult.TryGetToken(out var token))
            {
                GrpCore.Metadata headers = new GrpCore.Metadata();
                headers.Add("Authorization", $"Bearer {token.Value}");
                RemoveReply c = await serviceClient.RemoveAsync(new RemoveRequest()
                {
                    Id = id
                }, headers);

                return(new Comment {
                    Id = c.Id, PhotoId = c.PhotoId, UserName = c.UserName, Subject = c.Subject, Body = c.Body, SubmittedOn = c.SubmittedOn.ToDateTime()
                });
            }
            else
            {
                throw new UnauthorizedDeleteAttemptException <Comment>();
            }
        }
Exemplo n.º 2
0
        private Grpc.Core.Metadata GetDefaultHeaders()
        {
            var metadata = new Grpc.Core.Metadata();

            metadata.Add("x-helm-api-client", Version);
            return(metadata);
        }
Exemplo n.º 3
0
        protected override void HandleCallException(Exception exception, IRpcSerializer?serializer)
        {
            var rpcError = RpcError.TryCreate(exception, serializer);

            if (rpcError != null)
            {
                if (serializer != null)
                {
                    var serializedError = serializer.Serialize(rpcError);
                    throw new GrpcCore.RpcException(new GrpcCore.Status(GrpcCore.StatusCode.Unknown, rpcError.Message),
                                                    new GrpcCore.Metadata
                    {
                        { WellKnownHeaderKeys.ErrorInfo, serializedError }
                    });
                }
                else
                {
                    var metadata = new GrpcCore.Metadata
                    {
                        { WellKnownHeaderKeys.ErrorType, rpcError.ErrorType },
                        { WellKnownHeaderKeys.ErrorMessage, rpcError.Message },
                        { WellKnownHeaderKeys.ErrorCode, rpcError.ErrorCode }
                    };

                    if (rpcError.ErrorDetails != null)
                    {
                        metadata.Add(new GrpcCore.Metadata.Entry(WellKnownHeaderKeys.ErrorDetails, rpcError.ErrorDetails));
                    }

                    throw new GrpcCore.RpcException(new GrpcCore.Status(GrpcCore.StatusCode.Unknown, rpcError.Message), metadata);
                }
            }
        }
Exemplo n.º 4
0
 public CallGrpcClientJob(
     ILoggerFactory loggerFactory)
 {
     Logger  = loggerFactory.CreateLogger("CallGrpcClientJob");
     channel = new Channel("localhost:828",
                           ChannelCredentials.Insecure);
     AuthMetadata = new Grpc.Core.Metadata();
     AuthMetadata.Add("token", "smallchi518");
 }
Exemplo n.º 5
0
 public CallGrpcClientJob(
     ILoggerFactory loggerFactory,
     JT808Configuration configuration)
 {
     Logger  = loggerFactory.CreateLogger("CallGrpcClientJob");
     channel = new Channel($"{configuration.WebApiHost}:{configuration.WebApiPort}",
                           ChannelCredentials.Insecure);
     AuthMetadata = new Grpc.Core.Metadata();
     AuthMetadata.Add("token", configuration.WebApiToken);
 }
Exemplo n.º 6
0
        protected async Task WriteResponseHeadersAsync(IResponse httpRes, CallContext context)
        {
            var res = (GrpcResponse)httpRes;
            var nonSuccessStatus = res.StatusCode >= 300;

            if (!Feature.DisableResponseHeaders || nonSuccessStatus)
            {
                foreach (var header in Feature.IgnoreResponseHeaders)
                {
                    res.Headers.Remove(header);
                }

                if (res.Headers.Count > 0 || nonSuccessStatus)
                {
                    var headers = new Grpc.Core.Metadata();
                    if (nonSuccessStatus)
                    {
                        headers.Add(Keywords.HttpStatus, res.StatusCode.ToString());
                    }

                    foreach (var entry in res.Headers)
                    {
                        headers.Add(entry.Key, entry.Value);
                    }

                    if (nonSuccessStatus)
                    {
                        var status = res.Dto.GetResponseStatus();
                        if (status != null)
                        {
                            headers.Add(Keywords.GrpcResponseStatus,
                                        GrpcMarshaller <ResponseStatus> .Instance.Serializer(status));
                        }

                        var desc = status?.ErrorCode ?? res.StatusDescription ??
                                   status?.Message ?? HttpStatus.GetStatusDescription(res.StatusCode);
                        context.ServerCallContext.Status = ToGrpcStatus(res.StatusCode, desc);
                    }

                    await context.ServerCallContext.WriteResponseHeadersAsync(headers);
                }
            }
        }
Exemplo n.º 7
0
        private CallOptions PrepareRequest(bool noAuth)
        {
            var auth = noAuth
                ? null
                : !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password)
                    ? "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(UserName + ":" + Password))
                    : !string.IsNullOrEmpty(BearerToken)
                        ? "Bearer " + BearerToken
                        : !string.IsNullOrEmpty(SessionId)
                            ? nameof(SessionId)
                            : null;

            if (Headers.Count > 0 || auth != null || UserAgent != null)
            {
                var headers = new Grpc.Core.Metadata();
                foreach (var entry in Headers)
                {
                    headers.Add(entry);
                }

                if (auth != null)
                {
                    if (auth == nameof(SessionId))
                    {
                        headers.Add(Keywords.HeaderSessionId, SessionId);
                    }
                    else
                    {
                        headers.Add(HttpHeaders.Authorization, auth);
                    }
                }

                if (UserAgent != null)
                {
                    headers.Add(HttpHeaders.UserAgent, UserAgent);
                }

                return(new CallOptions(headers: headers));
            }
            return(default);
Exemplo n.º 8
0
        public async Task <Comment> CreateAsync(Comment comment)
        {
            var tokenResult = await tokenProvider.RequestAccessToken(new AccessTokenRequestOptions()
            {
                Scopes = new string[] { "photosrest" }
            });

            if (tokenResult.TryGetToken(out var token))
            {
                GrpCore.Metadata headers = new GrpCore.Metadata();
                headers.Add("Authorization", $"Bearer {token.Value}");

                CreateRequest createRequest = new CreateRequest()
                {
                    PhotoId = comment.PhotoId, Subject = comment.Subject, Body = comment.Body
                };
                CreateReply c = await serviceClient.CreateAsync(createRequest, headers);

                return(new Comment {
                    Id = c.Id, PhotoId = c.PhotoId, UserName = c.UserName, Subject = c.Subject, Body = c.Body, SubmittedOn = c.SubmittedOn.ToDateTime()
                });
            }
            return(null);
        }
Exemplo n.º 9
0
 public void InterceptHeaders(Metadata metadata)
 {
     var accessToken = GetAccessToken(CancellationToken.None);
     metadata.Add(new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken));
 }
Exemplo n.º 10
0
 public void InterceptHeaders(Metadata.Builder headerBuilder)
 {
     var accessToken = GetAccessToken(CancellationToken.None);
     headerBuilder.Add(new Metadata.MetadataEntry(AuthorizationHeader, Schema + " " + accessToken));
 }
Exemplo n.º 11
0
 public void InterceptHeaders(string authUri, Metadata metadata)
 {
     var accessToken = GetAccessToken(authUri, CancellationToken.None);
     metadata.Add(CreateBearerTokenHeader(accessToken));
 }