1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package httpc
- import (
- "context"
- "net/http"
- "github.com/wuntsong-org/go-zero-plus/core/breaker"
- )
- type (
- // Option is used to customize the *http.Client.
- Option func(r *http.Request) *http.Request
- // 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 any) (*http.Response, error)
- // DoRequest sends a HTTP request to the service.
- DoRequest(r *http.Request) (*http.Response, error)
- }
- namedService struct {
- name string
- cli *http.Client
- opts []Option
- }
- )
- // NewService returns a remote service with the given name.
- // opts are used to customize the *http.Client.
- func NewService(name string, opts ...Option) Service {
- return NewServiceWithClient(name, http.DefaultClient, opts...)
- }
- // NewServiceWithClient returns a remote service with the given name.
- // opts are used to customize the *http.Client.
- func NewServiceWithClient(name string, cli *http.Client, opts ...Option) Service {
- return namedService{
- name: name,
- cli: cli,
- opts: opts,
- }
- }
- // 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 any) (*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)
- }
- func (s namedService) do(r *http.Request) (resp *http.Response, err error) {
- for _, opt := range s.opts {
- r = opt(r)
- }
- brk := breaker.GetBreaker(s.name)
- err = brk.DoWithAcceptable(func() error {
- resp, err = s.cli.Do(r)
- return err
- }, func(err error) bool {
- return err == nil && resp.StatusCode < http.StatusInternalServerError
- })
- return
- }
|