[#525] v2/container: Make context with token reusable

Rename `PutContext` to `ContextWithToken` and implement its constructor as a
separate function in order to reuse it in other RPCs.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-25 19:23:11 +03:00 committed by Leonard Lyubich
parent 959610080a
commit 5c2b8de87d
2 changed files with 12 additions and 6 deletions

View file

@ -9,14 +9,14 @@ import (
) )
// FIXME: (temp solution) we need to pass session token from header // FIXME: (temp solution) we need to pass session token from header
type PutContext struct { type ContextWithToken struct {
context.Context context.Context
SessionToken *session.SessionToken SessionToken *session.SessionToken
} }
type ServiceExecutor interface { type ServiceExecutor interface {
Put(PutContext, *container.PutRequestBody) (*container.PutResponseBody, error) Put(ContextWithToken, *container.PutRequestBody) (*container.PutResponseBody, error)
Delete(context.Context, *container.DeleteRequestBody) (*container.DeleteResponseBody, error) Delete(context.Context, *container.DeleteRequestBody) (*container.DeleteResponseBody, error)
Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error) Get(context.Context, *container.GetRequestBody) (*container.GetResponseBody, error)
List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error) List(context.Context, *container.ListRequestBody) (*container.ListResponseBody, error)
@ -37,17 +37,23 @@ func NewExecutionService(exec ServiceExecutor) Server {
} }
} }
func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) { func contextWithTokenFromRequest(ctx context.Context, req interface {
GetMetaHeader() *session.RequestMetaHeader
}) ContextWithToken {
var tok *session.SessionToken var tok *session.SessionToken
for meta := req.GetMetaHeader(); meta != nil; meta = meta.GetOrigin() { for meta := req.GetMetaHeader(); meta != nil; meta = meta.GetOrigin() {
tok = meta.GetSessionToken() tok = meta.GetSessionToken()
} }
respBody, err := s.exec.Put(PutContext{ return ContextWithToken{
Context: ctx, Context: ctx,
SessionToken: tok, SessionToken: tok,
}, req.GetBody()) }
}
func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
respBody, err := s.exec.Put(contextWithTokenFromRequest(ctx, req), req.GetBody())
if err != nil { if err != nil {
return nil, fmt.Errorf("could not execute Put request: %w", err) return nil, fmt.Errorf("could not execute Put request: %w", err)
} }

View file

@ -25,7 +25,7 @@ func NewExecutor(w *wrapper.Wrapper) containerSvc.ServiceExecutor {
} }
} }
func (s *morphExecutor) Put(ctx containerSvc.PutContext, body *container.PutRequestBody) (*container.PutResponseBody, error) { func (s *morphExecutor) Put(ctx containerSvc.ContextWithToken, body *container.PutRequestBody) (*container.PutResponseBody, error) {
cnr, err := containerSDK.NewVerifiedFromV2(body.GetContainer()) cnr, err := containerSDK.NewVerifiedFromV2(body.GetContainer())
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid format of the container structure: %w", err) return nil, fmt.Errorf("invalid format of the container structure: %w", err)