bouncer/internal/client/query_layer_definition.go
William Petit 449fb69c02
Some checks are pending
Cadoles/bouncer/pipeline/pr-develop Build started...
Cadoles/bouncer/pipeline/head This commit looks good
feat: add layer definition api
2024-05-17 15:44:28 +02:00

62 lines
1.5 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 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
}