Skip to content

Commit 05633f0

Browse files
authored
fix(clihttp): add the missing Providers function (#210)
* fix: add clihttp Providers * fix: add clihttp Providers * fix: add clihttp Providers * fix: add clihttp Providers
1 parent 9037750 commit 05633f0

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

clihttp/dependency.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package clihttp
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/DoNewsCode/core/contract"
7+
"github.com/DoNewsCode/core/di"
8+
"github.com/opentracing/opentracing-go"
9+
)
10+
11+
/*
12+
Providers provides the http client as a dependency.
13+
Depends On:
14+
opentracing.Tracer
15+
Provides:
16+
*clihttp.Client
17+
*/
18+
func Providers(options ...ProvidersOptionFunc) di.Deps {
19+
var p providersOption
20+
for _, f := range options {
21+
f(&p)
22+
}
23+
return di.Deps{
24+
func(tracer opentracing.Tracer, populator contract.DIPopulator) (*Client, error) {
25+
if p.clientConstructor != nil {
26+
doer, err := p.clientConstructor(ClientArgs{populator})
27+
if err != nil {
28+
return nil, fmt.Errorf("contructing contract.HttpDoer: %w", err)
29+
}
30+
return NewClient(tracer, append(p.clientOptions, WithDoer(doer))...), nil
31+
}
32+
return NewClient(tracer, p.clientOptions...), nil
33+
},
34+
di.Bind(new(*Client), new(contract.HttpDoer)),
35+
}
36+
}

clihttp/dependency_option.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package clihttp
2+
3+
import "github.com/DoNewsCode/core/contract"
4+
5+
type providersOption struct {
6+
clientConstructor func(args ClientArgs) (contract.HttpDoer, error)
7+
clientOptions []Option
8+
}
9+
10+
// ClientArgs is the constructor argument for WithClientConstructor. See WithClientConstructor for usage.
11+
type ClientArgs struct {
12+
Populator contract.DIPopulator
13+
}
14+
15+
// ProvidersOptionFunc is the type of functional providersOption for Providers. Use this type to change how Providers work.
16+
type ProvidersOptionFunc func(options *providersOption)
17+
18+
// WithClientConstructor instructs the Providers to accept an alternative
19+
// constructor for the contract.HttpDoer to be wrapped by clihttp.NewClient.
20+
func WithClientConstructor(f func(args ClientArgs) (contract.HttpDoer, error)) ProvidersOptionFunc {
21+
return func(options *providersOption) {
22+
options.clientConstructor = f
23+
}
24+
}
25+
26+
// WithClientOption instructs the Providers to accept additional options for the
27+
// NewClient call, such as WithRequestLogThreshold.
28+
func WithClientOption(options ...Option) ProvidersOptionFunc {
29+
return func(providerOption *providersOption) {
30+
providerOption.clientOptions = options
31+
}
32+
}

clihttp/dependency_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package clihttp_test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/DoNewsCode/core"
8+
"github.com/DoNewsCode/core/clihttp"
9+
"github.com/DoNewsCode/core/contract"
10+
"github.com/DoNewsCode/core/observability"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
type mockDoer bool
15+
16+
func (m *mockDoer) Do(req *http.Request) (*http.Response, error) {
17+
*m = true
18+
resp := http.Response{}
19+
return &resp, nil
20+
}
21+
22+
func TestProviders(t *testing.T) {
23+
t.Run("no panic", func(t *testing.T) {
24+
c := core.Default()
25+
c.Provide(observability.Providers())
26+
c.Provide(clihttp.Providers())
27+
c.Invoke(func(client *clihttp.Client) {})
28+
c.Invoke(func(client contract.HttpDoer) {})
29+
})
30+
t.Run("panic", func(t *testing.T) {
31+
defer func() {
32+
if r := recover(); r != nil {
33+
return
34+
}
35+
t.Fatal("test should panic")
36+
}()
37+
c := core.Default()
38+
c.Provide(clihttp.Providers())
39+
c.Invoke(func(client *clihttp.Client) {})
40+
})
41+
t.Run("replace doer", func(t *testing.T) {
42+
var mock mockDoer
43+
c := core.Default()
44+
c.Provide(observability.Providers())
45+
c.Provide(clihttp.Providers(clihttp.WithClientConstructor(func(args clihttp.ClientArgs) (contract.HttpDoer, error) {
46+
return &mock, nil
47+
})))
48+
c.Invoke(func(client *clihttp.Client) {
49+
req, _ := http.NewRequest(http.MethodGet, "", nil)
50+
client.Do(req)
51+
assert.True(t, bool(mock))
52+
})
53+
})
54+
t.Run("additional options", func(t *testing.T) {
55+
c := core.Default()
56+
c.Provide(observability.Providers())
57+
c.Provide(clihttp.Providers(clihttp.WithClientOption(clihttp.WithRequestLogThreshold(10))))
58+
c.Invoke(func(client *clihttp.Client) {})
59+
})
60+
}

leader/dependency.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Providers returns a set of dependency providers for *Election and *Status.
1919
contract.ConfigAccessor
2020
contract.Dispatcher
2121
contract.DIPopulator
22-
Provide:
22+
Provides:
2323
Election *Election
2424
Status *Status
2525
*/

0 commit comments

Comments
 (0)