http_client.go 704 B

1234567891011121314151617181920212223242526272829303132333435
  1. // utils/http_client.go
  2. package utils
  3. import (
  4. "context"
  5. "time"
  6. "net/http"
  7. )
  8. type HTTPClient interface {
  9. PostJSON(ctx context.Context, url string, body interface{}) ([]byte, error)
  10. }
  11. type httpClient struct {
  12. client *http.Client
  13. }
  14. func NewHttpClient(timeout time.Duration) HTTPClient {
  15. return &httpClient{
  16. client: &http.Client{
  17. Timeout: timeout,
  18. Transport: &http.Transport{
  19. MaxIdleConns: 100,
  20. IdleConnTimeout: 90 * time.Second,
  21. DisableCompression: true,
  22. },
  23. },
  24. }
  25. }
  26. func (c *httpClient) PostJSON(ctx context.Context, url string, body interface{}) ([]byte, error) {
  27. // 实现具体的HTTP请求逻辑
  28. // 示例:返回空响应
  29. return []byte("{}"), nil
  30. }