Skip to content

Commit

Permalink
search: add SearchOpts to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 17, 2025
1 parent 22b54ed commit 1aecc2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/search/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Client interface {
SearchByEntity(ctx context.Context, entity Entity[Value]) (SearchResponse, error)
SearchByEntity(ctx context.Context, entity Entity[Value], opts SearchOpts) (SearchResponse, error)
}

func NewClient(httpClient *http.Client, baseAddress string) Client {
Expand All @@ -33,10 +33,19 @@ type SearchResponse struct {
Entities []SearchedEntity[Value] `json:"entities"`
}

func (c *client) SearchByEntity(ctx context.Context, entity Entity[Value]) (SearchResponse, error) {
type SearchOpts struct {
Limit int
MinMatch float64
}

func (c *client) SearchByEntity(ctx context.Context, entity Entity[Value], opts SearchOpts) (SearchResponse, error) {
addr := c.baseAddress + "/v2/search"
addr += "?name=" + entity.Name // TODO(adam): escape, use proper setters

if opts.Limit > 0 {
addr += fmt.Sprintf("&limit=%d", opts.Limit)
}

var out SearchResponse

req, err := http.NewRequest("GET", addr, nil)
Expand Down
23 changes: 23 additions & 0 deletions pkg/search/mock_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package search

import (
"context"
)

type MockClient struct {
Err error
SearchResponse SearchResponse
}

var _ Client = (&MockClient{})

func NewMockClient() *MockClient {
return &MockClient{}
}

func (c *MockClient) SearchByEntity(ctx context.Context, entity Entity[Value], opts SearchOpts) (SearchResponse, error) {
if c.Err != nil {
return SearchResponse{}, c.Err
}
return c.SearchResponse, c.Err
}

0 comments on commit 1aecc2e

Please sign in to comment.