chore: add templates search query to a filter (#13772)

* chore: add templates search query to a filter
This commit is contained in:
Steven Masley
2024-07-03 13:42:23 -05:00
committed by GitHub
parent 8778aa0f71
commit ccf34901bc
8 changed files with 246 additions and 23 deletions
+24 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/google/uuid"
@@ -362,11 +363,33 @@ func (c *Client) TemplatesByOrganization(ctx context.Context, organizationID uui
return templates, json.NewDecoder(res.Body).Decode(&templates)
}
type TemplateFilter struct {
OrganizationID uuid.UUID
}
// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (f TemplateFilter) asRequestOption() RequestOption {
return func(r *http.Request) {
var params []string
// Make sure all user input is quoted to ensure it's parsed as a single
// string.
if f.OrganizationID != uuid.Nil {
params = append(params, fmt.Sprintf("organization:%q", f.OrganizationID.String()))
}
q := r.URL.Query()
q.Set("q", strings.Join(params, " "))
r.URL.RawQuery = q.Encode()
}
}
// Templates lists all viewable templates
func (c *Client) Templates(ctx context.Context) ([]Template, error) {
func (c *Client) Templates(ctx context.Context, filter TemplateFilter) ([]Template, error) {
res, err := c.Request(ctx, http.MethodGet,
"/api/v2/templates",
nil,
filter.asRequestOption(),
)
if err != nil {
return nil, xerrors.Errorf("execute request: %w", err)