prepare options
Some checks failed
lint / lint (pull_request) Failing after 1m20s
test / test (pull_request) Successful in 1m59s

This commit is contained in:
2025-02-21 20:13:34 +03:00
parent c031230888
commit 334a10e26d
2 changed files with 20 additions and 52 deletions

View File

@@ -6,7 +6,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -199,7 +199,7 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client.
var hreq *http.Request var hreq *http.Request
if len(b) > 0 { if len(b) > 0 {
hreq, err = http.NewRequestWithContext(ctx, method, u.String(), ioutil.NopCloser(bytes.NewBuffer(b))) hreq, err = http.NewRequestWithContext(ctx, method, u.String(), io.NopCloser(bytes.NewBuffer(b)))
hreq.ContentLength = int64(len(b)) hreq.ContentLength = int64(len(b))
header.Set("Content-Length", fmt.Sprintf("%d", hreq.ContentLength)) header.Set("Content-Length", fmt.Sprintf("%d", hreq.ContentLength))
} else { } else {

View File

@@ -1,7 +1,6 @@
package http package http
import ( import (
"go.unistack.org/micro/v4/client"
"net" "net"
"net/http" "net/http"
@@ -27,104 +26,73 @@ var (
DefaultMaxSendMsgSize = 1024 * 1024 * 4 DefaultMaxSendMsgSize = 1024 * 1024 * 4
) )
type poolMaxStreams struct{}
// PoolMaxStreams maximum streams on a connectioin // PoolMaxStreams maximum streams on a connectioin
func PoolMaxStreams(n int) options.Option { func PoolMaxStreams(n int) options.Option {
return func(i interface{}) error { return options.NewOption("PoolMaxStreams")(n)
}
return options.ContextOption(poolMaxStreams{}, n)
} }
type poolMaxIdle struct{}
// PoolMaxIdle maximum idle conns of a pool // PoolMaxIdle maximum idle conns of a pool
func PoolMaxIdle(d int) options.Option { func PoolMaxIdle(n int) options.Option {
return options.ContextOption(poolMaxIdle{}, d) return options.NewOption("PoolMaxIdle")(n)
} }
type maxRecvMsgSizeKey struct{}
// MaxRecvMsgSize set the maximum size of message that client can receive. // MaxRecvMsgSize set the maximum size of message that client can receive.
func MaxRecvMsgSize(s int) options.Option { func MaxRecvMsgSize(n int) options.Option {
return options.ContextOption(maxRecvMsgSizeKey{}, s) return options.NewOption("MaxRecvMsgSize")(n)
} }
type maxSendMsgSizeKey struct{}
// MaxSendMsgSize set the maximum size of message that client can send. // MaxSendMsgSize set the maximum size of message that client can send.
func MaxSendMsgSize(s int) options.Option { func MaxSendMsgSize(n int) options.Option {
return options.ContextOption(maxSendMsgSizeKey{}, s) return options.NewOption("MaxSendMsgSize")(n)
} }
type httpClientKey struct{}
// nolint: golint // nolint: golint
// HTTPClient pass http.Client option to client Call // HTTPClient pass http.Client option to client Call
func HTTPClient(c *http.Client) options.Option { func HTTPClient(c *http.Client) options.Option {
return options.ContextOption(httpClientKey{}, c) return options.NewOption("HTTPClient")(c)
} }
type httpDialerKey struct{}
// nolint: golint // nolint: golint
// HTTPDialer pass net.Dialer option to client // HTTPDialer pass net.Dialer option to client
func HTTPDialer(d *net.Dialer) options.Option { func HTTPDialer(d *net.Dialer) options.Option {
return options.ContextOption(httpDialerKey{}, d) return options.NewOption("HTTPDialer")(d)
} }
type methodKey struct{}
// Method pass method option to client Call // Method pass method option to client Call
func Method(m string) client.CallOptions { func Method(m string) options.Option {
return options.Context(methodKey{}, m) return options.NewOption("Method")(m)
} }
type pathKey struct{}
// Path spcecifies path option to client Call // Path spcecifies path option to client Call
func Path(p string) options.Option { func Path(p string) options.Option {
return options.ContextOption(pathKey{}, p) return options.NewOption("Path")(p)
} }
type bodyKey struct{}
// Body specifies body option to client Call // Body specifies body option to client Call
func Body(b string) options.Option { func Body(b string) options.Option {
return options.ContextOption(bodyKey{}, b) return options.NewOption("Body")(b)
} }
type errorMapKey struct{}
func ErrorMap(m map[string]interface{}) options.Option { func ErrorMap(m map[string]interface{}) options.Option {
return options.ContextOption(errorMapKey{}, m) return options.NewOption("ErrorMap")(m)
} }
type structTagsKey struct{}
// StructTags pass tags slice option to client Call // StructTags pass tags slice option to client Call
func StructTags(tags []string) options.Option { func StructTags(tags []string) options.Option {
return options.ContextOption(structTagsKey{}, tags) return options.NewOption("StructTags")(tags)
} }
type metadataKey struct{}
// Metadata pass metadata to client Call // Metadata pass metadata to client Call
func Metadata(md metadata.Metadata) options.Option { func Metadata(md metadata.Metadata) options.Option {
return options.ContextOption(metadataKey{}, md) return options.NewOption("Metadata")(md)
} }
type cookieKey struct{}
// Cookie pass cookie to client Call // Cookie pass cookie to client Call
func Cookie(cookies ...string) options.Option { func Cookie(cookies ...string) options.Option {
return options.ContextOption(cookieKey{}, cookies) return options.NewOption("Cookie")(cookies)
} }
type headerKey struct{}
// Header pass cookie to client Call // Header pass cookie to client Call
func Header(headers ...string) options.Option { func Header(headers ...string) options.Option {
return options.ContextOption(headerKey{}, headers) return options.NewOption("Header")(headers)
} }