Skip to content

Commit

Permalink
feat: get sdk response types up to standards (#63)
Browse files Browse the repository at this point in the history
* removes signing key impl to break into its own package
* Closes #62
  • Loading branch information
eaddingtonwhite authored Jan 27, 2023
1 parent 9bd0e6d commit 3b183c8
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 568 deletions.
8 changes: 4 additions & 4 deletions auth/credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func NewStringMomentoTokenProvider(authToken string) (CredentialProvider, error)
const (
momentoControlEndpointPrefix = "control."
momentoCacheEndpointPrefix = "cache."
controlEndpointClaimId = "cp"
cacheEndpointClaimId = "c"
controlEndpointClaimID = "cp"
cacheEndpointClaimID = "c"
)

func resolve(request *ResolveRequest) (*Endpoints, momentoerrors.MomentoSvcErr) {
Expand All @@ -99,8 +99,8 @@ func getEndpointsFromToken(authToken string) (*Endpoints, momentoerrors.MomentoS
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
return &Endpoints{
ControlEndpoint: reflect.ValueOf(claims[controlEndpointClaimId]).String(),
CacheEndpoint: reflect.ValueOf(claims[cacheEndpointClaimId]).String(),
ControlEndpoint: reflect.ValueOf(claims[controlEndpointClaimID]).String(),
CacheEndpoint: reflect.ValueOf(claims[cacheEndpointClaimID]).String(),
}, nil
}
return nil, momentoerrors.NewMomentoSvcErr(
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Configuration interface {
//with the specified momento.TransportStrategy
WithTransportStrategy(transportStrategy TransportStrategy) Configuration

// GetClientSideTimeoutMillis Returns the current configuration options for client side timeout with the Momento service
GetClientSideTimeoutMillis() time.Duration
// GetClientSideTimeout Returns the current configuration options for client side timeout with the Momento service
GetClientSideTimeout() time.Duration

// WithClientTimeoutMillis Copy constructor for overriding TransportStrategy client side timeout. Returns a new Configuration object
// with the specified momento.TransportStrategy using passed client side timeout.
Expand All @@ -26,7 +26,7 @@ type SimpleCacheConfiguration struct {
transportStrategy TransportStrategy
}

func (s *SimpleCacheConfiguration) GetClientSideTimeoutMillis() time.Duration {
func (s *SimpleCacheConfiguration) GetClientSideTimeout() time.Duration {
return s.transportStrategy.GetClientSideTimeout()
}

Expand Down
4 changes: 2 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func main() {
var authToken = os.Getenv("MOMENTO_AUTH_TOKEN")
const (
cacheName = "cache"
itemDefaultTtlSeconds = 60
itemDefaultTTLSeconds = 60
)

if authToken == "" {
log.Fatal("Missing required environment variable MOMENTO_AUTH_TOKEN")
}

// Initializes Momento
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
client, err := momento.NewSimpleCacheClient(authToken, itemDefaultTTLSeconds)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion incubating/momento_local_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (t TestMomentoLocalServer) Subscribe(req *pb.XSubscriptionRequest, server p
if err != nil {
return err
}
count += 1
count++
}
return nil
}
23 changes: 2 additions & 21 deletions incubating/simple_cache_client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package incubating represents experimental packages and clients for Momento

package incubating

import (
Expand Down Expand Up @@ -112,39 +111,21 @@ func convertMomentoSvcErrorToCustomerError(e momentoerrors.MomentoSvcErr) moment
}
return momento.NewMomentoError(e.Code(), e.Message(), e.OriginalErr())
}

func (c *DefaultScsClient) CreateCache(ctx context.Context, request *momento.CreateCacheRequest) error {
return c.internalClient.CreateCache(ctx, request)
}

func (c *DefaultScsClient) DeleteCache(ctx context.Context, request *momento.DeleteCacheRequest) error {
return c.internalClient.DeleteCache(ctx, request)
}

func (c *DefaultScsClient) ListCaches(ctx context.Context, request *momento.ListCachesRequest) (*momento.ListCachesResponse, error) {
return c.internalClient.ListCaches(ctx, request)
}

func (c *DefaultScsClient) CreateSigningKey(ctx context.Context, request *momento.CreateSigningKeyRequest) (*momento.CreateSigningKeyResponse, error) {
return c.internalClient.CreateSigningKey(ctx, request)
}

func (c *DefaultScsClient) RevokeSigningKey(ctx context.Context, request *momento.RevokeSigningKeyRequest) error {
return c.internalClient.RevokeSigningKey(ctx, request)
}

func (c *DefaultScsClient) ListSigningKeys(ctx context.Context, request *momento.ListSigningKeysRequest) (*momento.ListSigningKeysResponse, error) {
return c.internalClient.ListSigningKeys(ctx, request)
}

func (c *DefaultScsClient) Set(ctx context.Context, request *momento.CacheSetRequest) (*momento.SetCacheResponse, error) {
func (c *DefaultScsClient) Set(ctx context.Context, request *momento.CacheSetRequest) error {
return c.internalClient.Set(ctx, request)
}

func (c *DefaultScsClient) Get(ctx context.Context, request *momento.CacheGetRequest) (*momento.GetCacheResponse, error) {
func (c *DefaultScsClient) Get(ctx context.Context, request *momento.CacheGetRequest) (*momento.CacheGetResponse, error) {
return c.internalClient.Get(ctx, request)
}

func (c *DefaultScsClient) Delete(ctx context.Context, request *momento.CacheDeleteRequest) error {
return c.internalClient.Delete(ctx, request)
}
20 changes: 6 additions & 14 deletions internal/models/requests.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package models

import (
"fmt"

"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/config"
"github.com/momentohq/client-sdk-go/internal/momentoerrors"
pb "github.com/momentohq/client-sdk-go/internal/protos"
)

Expand All @@ -20,6 +17,12 @@ type DataGrpcManagerRequest struct {
type LocalDataGrpcManagerRequest struct {
Endpoint string
}
type CacheGetRequest struct {
// CacheName Name of the cache to get the item from
CacheName string
// Key string or []byte key to be used to retrieve the item.
Key interface{}
}

type ControlClientRequest struct {
Configuration config.Configuration
Expand All @@ -46,14 +49,3 @@ type ConvertEcacheResultRequest struct {
Message string
OpName string
}

func ConvertEcacheResult(request ConvertEcacheResultRequest) momentoerrors.MomentoSvcErr {
return momentoerrors.NewMomentoSvcErr(
momentoerrors.InternalServerError,
fmt.Sprintf(
"CacheService returned an unexpected result: %v for operation: %s with message: %s",
request.ECacheResult, request.OpName, request.Message,
),
nil,
)
}
101 changes: 5 additions & 96 deletions internal/models/responses.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package models

import (
"encoding/json"
"time"

"github.com/momentohq/client-sdk-go/internal/momentoerrors"
pb "github.com/momentohq/client-sdk-go/internal/protos"
)

Expand Down Expand Up @@ -41,95 +37,16 @@ func NewCacheInfo(cache *pb.XCache) CacheInfo {
return CacheInfo{Name: cache.CacheName}
}

type CreateSigningKeyRequest struct {
TtlMinutes uint32
}

type CreateSigningKeyResponse struct {
KeyId string
Endpoint string
Key string
ExpiresAt time.Time
}

func NewCreateSigningKeyResponse(endpoint string, resp *pb.XCreateSigningKeyResponse) (*CreateSigningKeyResponse, error) {
var keyObj map[string]string
err := json.Unmarshal([]byte(resp.GetKey()), &keyObj)
if err != nil {
return nil, err
}
return &CreateSigningKeyResponse{
KeyId: keyObj["kid"],
Endpoint: endpoint,
Key: resp.GetKey(),
ExpiresAt: time.Unix(int64(resp.GetExpiresAt()), 0),
}, nil
}

type RevokeSigningKeyRequest struct {
KeyId string
}

type ListSigningKeysRequest struct {
NextToken string
}

type ListSigningKeysResponse struct {
NextToken string
SigningKeys []SigningKey
}

func NewListSigningKeysResponse(endpoint string, resp *pb.XListSigningKeysResponse) *ListSigningKeysResponse {
var signingKeys []SigningKey
for _, signingKey := range resp.SigningKey {
signingKeys = append(signingKeys, NewSigningKey(endpoint, signingKey))
}
return &ListSigningKeysResponse{NextToken: resp.GetNextToken(), SigningKeys: signingKeys}
}

type SigningKey struct {
KeyId string
Endpoint string
ExpiresAt time.Time
}

func NewSigningKey(endpoint string, signingKey *pb.XSigningKey) SigningKey {
return SigningKey{
KeyId: signingKey.GetKeyId(),
Endpoint: endpoint,
ExpiresAt: time.Unix(int64(signingKey.GetExpiresAt()), 0),
}
}
type CacheResult string

const (
HIT string = "HIT"
MISS string = "MISS"
HIT CacheResult = "HIT"
MISS CacheResult = "MISS"
)

type CacheGetRequest struct {
CacheName string
Key interface{}
}

type GetCacheResponse struct {
type CacheGetResponse struct {
Value []byte
Result string
}

func NewGetCacheResponse(resp *pb.XGetResponse) (*GetCacheResponse, momentoerrors.MomentoSvcErr) {
var result string
if resp.Result == pb.ECacheResult_Hit {
result = HIT
} else if resp.Result == pb.ECacheResult_Miss {
result = MISS
} else {
return nil, ConvertEcacheResult(ConvertEcacheResultRequest{
ECacheResult: resp.Result,
Message: resp.Message,
OpName: "GET",
})
}
return &GetCacheResponse{Value: resp.CacheBody, Result: result}, nil
Result CacheResult
}

type CacheSetRequest struct {
Expand All @@ -139,14 +56,6 @@ type CacheSetRequest struct {
TtlSeconds uint32
}

type SetCacheResponse struct {
Value []byte
}

func NewSetCacheResponse(resp *pb.XSetResponse, value []byte) *SetCacheResponse {
return &SetCacheResponse{Value: value}
}

type CacheDeleteRequest struct {
CacheName string
Key interface{}
Expand Down
42 changes: 4 additions & 38 deletions internal/services/scs_control_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (client *ScsControlClient) Close() momentoerrors.MomentoSvcErr {
}

func (client *ScsControlClient) CreateCache(ctx context.Context, request *models.CreateCacheRequest) momentoerrors.MomentoSvcErr {
if !utility.IsCacheNameValid(request.CacheName) {
return momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "Cache name cannot be empty", nil)
if err := utility.IsCacheNameValid(request.CacheName); err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, ControlCtxTimeout)
defer cancel()
Expand All @@ -46,8 +46,8 @@ func (client *ScsControlClient) CreateCache(ctx context.Context, request *models
}

func (client *ScsControlClient) DeleteCache(ctx context.Context, request *models.DeleteCacheRequest) momentoerrors.MomentoSvcErr {
if !utility.IsCacheNameValid(request.CacheName) {
return momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "Cache name cannot be empty", nil)
if err := utility.IsCacheNameValid(request.CacheName); err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, ControlCtxTimeout)
defer cancel()
Expand All @@ -67,37 +67,3 @@ func (client *ScsControlClient) ListCaches(ctx context.Context, request *models.
}
return models.NewListCacheResponse(resp), nil
}

func (client *ScsControlClient) CreateSigningKey(ctx context.Context, endpoint string, request *models.CreateSigningKeyRequest) (*models.CreateSigningKeyResponse, momentoerrors.MomentoSvcErr) {
ctx, cancel := context.WithTimeout(ctx, ControlCtxTimeout)
defer cancel()
resp, err := client.grpcClient.CreateSigningKey(ctx, &pb.XCreateSigningKeyRequest{TtlMinutes: request.TtlMinutes})
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
createResp, err := models.NewCreateSigningKeyResponse(endpoint, resp)
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
return createResp, nil
}

func (client *ScsControlClient) RevokeSigningKey(ctx context.Context, request *models.RevokeSigningKeyRequest) momentoerrors.MomentoSvcErr {
ctx, cancel := context.WithTimeout(ctx, ControlCtxTimeout)
defer cancel()
_, err := client.grpcClient.RevokeSigningKey(ctx, &pb.XRevokeSigningKeyRequest{KeyId: request.KeyId})
if err != nil {
return momentoerrors.ConvertSvcErr(err)
}
return nil
}

func (client *ScsControlClient) ListSigningKeys(ctx context.Context, endpoint string, request *models.ListSigningKeysRequest) (*models.ListSigningKeysResponse, momentoerrors.MomentoSvcErr) {
ctx, cancel := context.WithTimeout(ctx, ControlCtxTimeout)
defer cancel()
resp, err := client.grpcClient.ListSigningKeys(ctx, &pb.XListSigningKeysRequest{NextToken: request.NextToken})
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
return models.NewListSigningKeysResponse(endpoint, resp), nil
}
Loading

0 comments on commit 3b183c8

Please sign in to comment.