feat: add httpc.Do & httpc.Service.Do (#1775)

* backup

* backup

* backup

* feat: add httpc.Do & httpc.Service.Do

* fix: not using strings.Cut, it's from Go 1.18

* chore: remove redudant code

* feat: httpc.Do finished

* chore: fix reviewdog

* chore: break loop if found

* add more tests
This commit is contained in:
Kevin Wan
2022-04-11 11:00:28 +08:00
committed by GitHub
parent fabea4c448
commit 50de01fb49
8 changed files with 680 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
package httpc
import (
"context"
"net/http"
"github.com/zeromicro/go-zero/core/breaker"
@@ -12,6 +13,8 @@ type (
// Service represents a remote HTTP service.
Service interface {
// Do sends an HTTP request with the given arguments and returns an HTTP response.
Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error)
// DoRequest sends a HTTP request to the service.
DoRequest(r *http.Request) (*http.Response, error)
}
@@ -39,6 +42,16 @@ func NewServiceWithClient(name string, cli *http.Client, opts ...Option) Service
}
}
// Do sends an HTTP request with the given arguments and returns an HTTP response.
func (s namedService) Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error) {
req, err := buildRequest(ctx, method, url, data)
if err != nil {
return nil, err
}
return s.DoRequest(req)
}
// DoRequest sends an HTTP request to the service.
func (s namedService) DoRequest(r *http.Request) (*http.Response, error) {
return request(r, s)