http_client.go 712 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package http_client
  2. import (
  3. "github.com/go-resty/resty/v2"
  4. "time"
  5. )
  6. type NewRequestOption func(client *Client)
  7. func WithNewRequestTimeout(timeout time.Duration) NewRequestOption {
  8. return func(client *Client) {
  9. client.setTimeout(timeout)
  10. }
  11. }
  12. type Client struct {
  13. client *resty.Client
  14. }
  15. func New() *Client {
  16. return &Client{
  17. client: resty.New(),
  18. }
  19. }
  20. func Destroy(client *Client) {
  21. if client == nil {
  22. return
  23. }
  24. client.client = nil
  25. }
  26. func (client *Client) NewRequest(opts ...NewRequestOption) *Request {
  27. for _, opt := range opts {
  28. opt(client)
  29. }
  30. return &Request{request: client.client.R()}
  31. }
  32. func (client *Client) setTimeout(timeout time.Duration) {
  33. client.client.SetTimeout(timeout)
  34. }