Skip to content

Commit

Permalink
clientv1: expose get and update metadata (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi authored Dec 11, 2024
1 parent 2487054 commit 67ba7d2
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions clientv1_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

"connectrpc.com/connect"
"golang.org/x/oauth2"
"google.golang.org/protobuf/types/known/structpb"

clientsv1 "github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1"
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1/clientsv1connect"
"github.com/sourcegraph/sourcegraph/lib/errors"
)

// UsersServiceV1 provides client methods to interact with the UsersService API
Expand Down Expand Up @@ -84,3 +86,57 @@ func (s *UsersServiceV1) GetUserRolesByID(ctx context.Context, userID, service s
}
return resp.Msg.GetUserRoles(), nil
}

// GetUserMetadata returns the metadata associated with the given user ID and
// metadata namespaces.
//
// Required scopes: sams::user.metadata.${NAMESPACE}::read for each of the
// requested namespaces.
func (s *UsersServiceV1) GetUserMetadata(ctx context.Context, userID string, namespaces []string) ([]*clientsv1.UserServiceMetadata, error) {
if userID == "" {
return nil, errors.New("user ID cannot be empty")
}
if len(namespaces) == 0 {
return nil, errors.New("at least one namespace must be provided")
}

req := &clientsv1.GetUserMetadataRequest{
Id: userID,
Namespaces: namespaces,
}
client := s.newClient(ctx)
resp, err := parseResponseAndError(client.GetUserMetadata(ctx, connect.NewRequest(req)))
if err != nil {
return nil, err
}
return resp.Msg.GetMetadata(), nil
}

// UpdateUserMetadata updates the metadata associated with the given user ID
// and metadata namespace.
//
// Required scopes: sams::user.metadata.${NAMESPACE}::read for the namespace
// being updated.
func (s *UsersServiceV1) UpdateUserMetadata(ctx context.Context, userID, namespace string, metadata map[string]any) (*clientsv1.UserServiceMetadata, error) {
if userID == "" || namespace == "" {
return nil, errors.New("user ID and namespace cannot be empty")
}

md, err := structpb.NewStruct(metadata)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal user metadata")
}
req := &clientsv1.UpdateUserMetadataRequest{
Metadata: &clientsv1.UserServiceMetadata{
UserId: userID,
Namespace: namespace,
Metadata: md,
},
}
client := s.newClient(ctx)
resp, err := parseResponseAndError(client.UpdateUserMetadata(ctx, connect.NewRequest(req)))
if err != nil {
return nil, err
}
return resp.Msg.GetMetadata(), nil
}

0 comments on commit 67ba7d2

Please sign in to comment.