76 lines
1.6 KiB
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 QueryProxyOptionFunc func(*QueryProxyOptions)
|
|
|
|
type QueryProxyOptions struct {
|
|
Options []OptionFunc
|
|
Limit *int
|
|
Offset *int
|
|
Names []store.ProxyName
|
|
}
|
|
|
|
func WithQueryProxyOptions(funcs ...OptionFunc) QueryProxyOptionFunc {
|
|
return func(opts *QueryProxyOptions) {
|
|
opts.Options = funcs
|
|
}
|
|
}
|
|
|
|
func WithQueryProxyLimit(limit int) QueryProxyOptionFunc {
|
|
return func(opts *QueryProxyOptions) {
|
|
opts.Limit = &limit
|
|
}
|
|
}
|
|
|
|
func WithQueryProxyOffset(offset int) QueryProxyOptionFunc {
|
|
return func(opts *QueryProxyOptions) {
|
|
opts.Offset = &offset
|
|
}
|
|
}
|
|
|
|
func WithQueryProxyNames(names ...store.ProxyName) QueryProxyOptionFunc {
|
|
return func(opts *QueryProxyOptions) {
|
|
opts.Names = names
|
|
}
|
|
}
|
|
|
|
func (c *Client) QueryProxy(ctx context.Context, funcs ...QueryProxyOptionFunc) ([]*store.ProxyHeader, error) {
|
|
options := &QueryProxyOptions{}
|
|
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", query.Encode())
|
|
|
|
response := withResponse[admin.QueryProxyResponse]()
|
|
|
|
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.Proxies, nil
|
|
}
|