bouncer/internal/client/query_layer.go

76 lines
1.6 KiB
Go

package client
import (
"context"
"fmt"
"net/url"
"forge.cadoles.com/cadoles/bouncer/internal/admin"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
)
type QueryLayerOptionFunc func(*QueryLayerOptions)
type QueryLayerOptions struct {
Options []OptionFunc
Offset *int
Limit *int
Names []store.LayerName
}
func WithQueryLayerOptions(funcs ...OptionFunc) QueryLayerOptionFunc {
return func(opts *QueryLayerOptions) {
opts.Options = funcs
}
}
func WithQueryLayerLimit(limit int) QueryLayerOptionFunc {
return func(opts *QueryLayerOptions) {
opts.Limit = &limit
}
}
func WithQueryLayerOffset(offset int) QueryLayerOptionFunc {
return func(opts *QueryLayerOptions) {
opts.Offset = &offset
}
}
func WithQueryLayerNames(names ...store.LayerName) QueryLayerOptionFunc {
return func(opts *QueryLayerOptions) {
opts.Names = names
}
}
func (c *Client) QueryLayer(ctx context.Context, proxyName store.ProxyName, funcs ...QueryLayerOptionFunc) ([]*store.LayerHeader, error) {
options := &QueryLayerOptions{}
for _, fn := range funcs {
fn(options)
}
query := url.Values{}
if options.Names != nil && len(options.Names) > 0 {
query.Set("names", joinSlice(options.Names))
}
path := fmt.Sprintf("/api/v1/proxies/%s/layers?%s", proxyName, query.Encode())
response := withResponse[admin.QueryLayerResponse]()
if options.Options == nil {
options.Options = make([]OptionFunc, 0)
}
if err := c.apiGet(ctx, path, &response, options.Options...); err != nil {
return nil, errors.WithStack(err)
}
if response.Error != nil {
return nil, errors.WithStack(response.Error)
}
return response.Data.Layers, nil
}