Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Config.QueryOptions #23

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -378,7 +379,11 @@ func TestClient_With(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/echo":
err := json.NewEncoder(w).Encode(map[string]string{"message": r.Header.Get("Message")})
cnt, _ := strconv.Atoi(r.URL.Query().Get("count"))
if cnt == 0 {
cnt = 1
}
err := json.NewEncoder(w).Encode(map[string]string{"message": strings.Repeat(r.Header.Get("Message"), cnt)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down Expand Up @@ -412,14 +417,15 @@ func TestClient_With(t *testing.T) {
var resp2 Response
cli = cli.With(
hx.Header("Message", "bar"),
hx.Query("count", "3"),
)
err = cli.Get(context.Background(), ts.URL+"/echo",
hx.WhenSuccess(hx.AsJSON(&resp2)),
)
if err != nil {
t.Errorf("returned %v, want nil", err)
}
if got, want := resp2.Message, "bar"; got != want {
if got, want := resp2.Message, "barbarbar"; got != want {
t.Errorf("returned %q, want %q", got, want)
}
}
13 changes: 13 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Config struct {
URLOptions []func(context.Context, *url.URL) error
QueryOptions []func(context.Context, url.Values) error
BodyOption func(context.Context) (io.Reader, error)
ClientOptions []func(context.Context, *http.Client) error
RequestHandlers []RequestHandler
Expand Down Expand Up @@ -78,6 +79,18 @@ func (cfg *Config) buildURL(ctx context.Context) (*url.URL, error) {
return nil, err
}
}
if n := len(cfg.QueryOptions); n > 0 {
q := make(url.Values, n)
for _, f := range cfg.QueryOptions {
err := f(ctx, q)
if err != nil {
return nil, err
}
}
if len(q) > 0 {
u.RawQuery = q.Encode()
}
}

return u, nil
}
Expand Down
10 changes: 5 additions & 5 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func URL(urlStr string) Option {

// Query sets an url query parameter.
func Query(k, v string) Option {
return newURLOption(func(_ context.Context, u *url.URL) error {
q := u.Query()
q.Set(k, v)
u.RawQuery = q.Encode()
return nil
return OptionFunc(func(c *Config) {
c.QueryOptions = append(c.QueryOptions, func(_ context.Context, q url.Values) error {
q.Set(k, v)
return nil
})
})
}

Expand Down