Skip to content
Snippets Groups Projects
Commit 68e79749 authored by Alena Petraki's avatar Alena Petraki
Browse files

Добавлен клиент Delivery

parent 099c03e7
No related branches found
No related tags found
No related merge requests found
package client
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/delivery"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/locales"
)
type envsAdapter struct {
environments.Environments
dv delivery.Delivery
}
func (a envsAdapter) Get(ctx context.Context, spaceId, envId string) (env *environments.Environment, err error) {
return a.dv.GetEnvironment(ctx, spaceId, envId)
}
func (a envsAdapter) List(ctx context.Context, spaceId string) (envs []*environments.Environment, err error) {
return a.dv.ListEnvironments(ctx, spaceId)
}
type localesAdapter struct {
locales.Locales
dv delivery.Delivery
}
func (a localesAdapter) List(ctx context.Context, spaceId string) (locales []*locales.Locale, err error) {
return a.dv.ListLocales(ctx, spaceId)
}
type collectionsAdapter struct {
collections.Collections
dv delivery.Delivery
}
func (a collectionsAdapter) Get(ctx context.Context, spaceId, envId, collectionId string, opts ...*collections.GetOptions) (collection *collections.Collection, err error) {
return a.dv.GetCollection(ctx, spaceId, envId, collectionId)
}
func (a collectionsAdapter) List(ctx context.Context, spaceId, envId string, filter *collections.Filter) (collections []*collections.Collection, err error) {
return a.dv.ListCollections(ctx, spaceId, envId)
}
type itemsAdapter struct {
items.Items
dv delivery.Delivery
}
func (a itemsAdapter) GetPublished(ctx context.Context, spaceId, envId, collectionId, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
return a.dv.GetItem(ctx, spaceId, envId, collectionId, itemId, options...)
}
func (a itemsAdapter) FindPublished(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
return a.dv.FindItems(ctx, spaceId, envId, collectionId, filter, options...)
}
func (a itemsAdapter) AggregatePublished(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
return a.dv.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
}
package client
import (
"context"
"time"
"git.perx.ru/perxis/perxis-go/pkg/cache"
collections "git.perx.ru/perxis/perxis-go/pkg/collections/middleware"
"git.perx.ru/perxis/perxis-go/pkg/delivery"
deliveryservice "git.perx.ru/perxis/perxis-go/pkg/delivery/service"
deliverytransportgrpc "git.perx.ru/perxis/perxis-go/pkg/delivery/transport/grpc"
environments "git.perx.ru/perxis/perxis-go/pkg/environments/middleware"
items "git.perx.ru/perxis/perxis-go/pkg/items/middleware"
locales "git.perx.ru/perxis/perxis-go/pkg/locales/middleware"
"google.golang.org/grpc"
)
func NewClient(addr string, opts ...Option) (delivery.Delivery, error) {
ctx := context.Background()
c := new(Config)
dialOpts := make([]grpc.DialOption, 0)
for _, o := range opts {
o(&c.Config)
}
authDialOpts, err := c.GetAuthDialOpts(ctx)
if err != nil {
return nil, err
}
conn, err := grpc.Dial(addr, append(dialOpts, authDialOpts...)...)
if err != nil {
return nil, err
}
client := deliverytransportgrpc.NewGRPCClient(conn, "", c.ClientOptions...)
cfg := &deliveryservice.Config{
Locales: localesAdapter{dv: client},
Environments: envsAdapter{dv: client},
Collections: collectionsAdapter{dv: client},
Items: itemsAdapter{dv: client},
}
if !c.NoDecode {
cfg.Items = items.ClientEncodeMiddleware(cfg.Collections)(cfg.Items)
}
if !c.NoCache {
cfg = WithCaching(cfg, 0, 0)
}
return deliveryservice.NewService(cfg), nil
}
func WithCaching(cfg *deliveryservice.Config, size int, ttl time.Duration) *deliveryservice.Config {
c := *cfg
c.Environments = environments.CachingMiddleware(cache.NewCache(size, ttl))(cfg.Environments)
c.Locales = locales.CachingMiddleware(cache.NewCache(size, ttl))(cfg.Locales)
c.Items = items.CachingMiddleware(cache.NewCache(size, ttl), cache.NewCache(size, ttl), c.Environments)(cfg.Items)
c.Collections = collections.CachingMiddleware(cache.NewCache(size, ttl), c.Environments)(cfg.Collections)
return &c
}
package client
import (
contentclient "git.perx.ru/perxis/perxis-go/pkg/content"
"github.com/go-kit/kit/transport/grpc"
)
type Option contentclient.Option
type Config struct {
contentclient.Config
}
func AuthOAuth2(tokenUrl, clientID, clientSecret, audience string) Option {
return Option(contentclient.AuthOAuth2(tokenUrl, clientID, clientSecret, audience))
}
func AuthTLS(cacert, cert, key []byte) Option {
return Option(contentclient.AuthTLS(cacert, cert, key))
}
func AuthAPIKey(key string) Option {
return Option(contentclient.AuthAPIKey(key))
}
func AuthInsecure() Option {
return Option(contentclient.AuthInsecure())
}
func GrpcClientOptions(opts ...grpc.ClientOption) Option {
return Option(contentclient.GrpcClientOptions(opts...))
}
func NoCache() Option {
return Option(contentclient.NoCache())
}
package service
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/delivery"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/locales"
)
type Config struct {
locales.Locales
environments.Environments
collections.Collections
items.Items
}
func NewService(config *Config) delivery.Delivery {
return &deliveryService{
Locales: config.Locales,
Environments: config.Environments,
Collections: config.Collections,
Items: config.Items,
}
}
type deliveryService struct {
locales.Locales
environments.Environments
collections.Collections
items.Items
}
func (s deliveryService) ListLocales(ctx context.Context, spaceId string) (locales []*locales.Locale, err error) {
return s.Locales.List(ctx, spaceId)
}
func (s deliveryService) GetEnvironment(ctx context.Context, spaceId, envId string) (env *environments.Environment, err error) {
return s.Environments.Get(ctx, spaceId, envId)
}
func (s deliveryService) ListEnvironments(ctx context.Context, spaceId string) (envs []*environments.Environment, err error) {
return s.Environments.List(ctx, spaceId)
}
func (s deliveryService) GetCollection(ctx context.Context, spaceId, envId, collectionId string) (collection *collections.Collection, err error) {
// Для Delivery всегда загружается коллекция для последующего использования, так как нет кейсов использования незагруженной коллекции
return s.Collections.Get(ctx, spaceId, envId, collectionId)
}
func (s deliveryService) ListCollections(ctx context.Context, spaceId, envId string) (collections []*collections.Collection, err error) {
return s.Collections.List(ctx, spaceId, envId, nil)
}
func (s deliveryService) GetItem(ctx context.Context, spaceId, envId, collectionId, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
return s.Items.GetPublished(ctx, spaceId, envId, collectionId, itemId, options...)
}
func (s deliveryService) FindItems(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
return s.Items.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
}
func (s deliveryService) Aggregate(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
return s.Items.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment