bouncer/internal/client/query_layer_definition.go

62 lines
1.5 KiB
Go
Raw Normal View History

2024-05-17 15:44:28 +02:00
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 QueryLayerDefinitionOptionFunc func(*QueryLayerDefinitionOptions)
type QueryLayerDefinitionOptions struct {
Options []OptionFunc
Types []store.LayerType
}
func WithQueryLayerDefinitionOptions(funcs ...OptionFunc) QueryLayerDefinitionOptionFunc {
return func(opts *QueryLayerDefinitionOptions) {
opts.Options = funcs
}
}
func WithQueryLayerDefinitionTypes(types ...store.LayerType) QueryLayerDefinitionOptionFunc {
return func(opts *QueryLayerDefinitionOptions) {
opts.Types = types
}
}
func (c *Client) QueryLayerDefinition(ctx context.Context, funcs ...QueryLayerDefinitionOptionFunc) ([]store.LayerDefinition, error) {
options := &QueryLayerDefinitionOptions{}
for _, fn := range funcs {
fn(options)
}
query := url.Values{}
if options.Types != nil && len(options.Types) > 0 {
query.Set("types", joinSlice(options.Types))
}
path := fmt.Sprintf("/api/v1/definitions/layers?%s", query.Encode())
response := withResponse[admin.QueryLayerDefinitionResponse]()
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.Definitions, nil
}