mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
feat: add httpc/Get httpc/Post (#1640)
This commit is contained in:
parent
3279a7ef0f
commit
85cf662c6f
@ -1,6 +1,7 @@
|
|||||||
package httpc
|
package httpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/breaker"
|
"github.com/zeromicro/go-zero/core/breaker"
|
||||||
@ -36,6 +37,26 @@ func Do(key string, r *http.Request, opts ...Option) (resp *http.Response, err e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get sends an HTTP GET request to the service assocated with the given key.
|
||||||
|
func Get(key, url string, opts ...Option) (*http.Response, error) {
|
||||||
|
r, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Do(key, r, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post sends an HTTP POST request to the service assocated with the given key.
|
||||||
|
func Post(key, url, contentType string, body io.Reader, opts ...Option) (*http.Response, error) {
|
||||||
|
r, err := http.NewRequest(http.MethodPost, url, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Do(key, r, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
func doRequest(key string, r *http.Request, opts ...Option) (resp *http.Response, err error) {
|
func doRequest(key string, r *http.Request, opts ...Option) (resp *http.Response, err error) {
|
||||||
brk := breaker.GetBreaker(key)
|
brk := breaker.GetBreaker(key)
|
||||||
err = brk.DoWithAcceptable(func() error {
|
err = brk.DoWithAcceptable(func() error {
|
||||||
|
@ -11,9 +11,9 @@ import (
|
|||||||
func TestDo(t *testing.T) {
|
func TestDo(t *testing.T) {
|
||||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
}))
|
}))
|
||||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
_, err := Get("foo", "tcp://bad request")
|
||||||
assert.Nil(t, err)
|
assert.NotNil(t, err)
|
||||||
resp, err := Do("foo", req, func(cli *http.Client) {
|
resp, err := Get("foo", svr.URL, func(cli *http.Client) {
|
||||||
cli.Transport = http.DefaultTransport
|
cli.Transport = http.DefaultTransport
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -22,9 +22,9 @@ func TestDo(t *testing.T) {
|
|||||||
|
|
||||||
func TestDoNotFound(t *testing.T) {
|
func TestDoNotFound(t *testing.T) {
|
||||||
svr := httptest.NewServer(http.NotFoundHandler())
|
svr := httptest.NewServer(http.NotFoundHandler())
|
||||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
_, err := Post("foo", "tcp://bad request", "application/json", nil)
|
||||||
assert.Nil(t, err)
|
assert.NotNil(t, err)
|
||||||
resp, err := Do("foo", req)
|
resp, err := Post("foo", svr.URL, "application/json", nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user