go-chatgpt
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

41 行
752 B

  1. package gogpt
  2. import (
  3. "bytes"
  4. "context"
  5. "net/http"
  6. )
  7. type requestBuilder interface {
  8. build(ctx context.Context, method, url string, request any) (*http.Request, error)
  9. }
  10. type httpRequestBuilder struct {
  11. marshaller marshaller
  12. }
  13. func newRequestBuilder() *httpRequestBuilder {
  14. return &httpRequestBuilder{
  15. marshaller: &jsonMarshaller{},
  16. }
  17. }
  18. func (b *httpRequestBuilder) build(ctx context.Context, method, url string, request any) (*http.Request, error) {
  19. if request == nil {
  20. return http.NewRequestWithContext(ctx, method, url, nil)
  21. }
  22. var reqBytes []byte
  23. reqBytes, err := b.marshaller.marshal(request)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return http.NewRequestWithContext(
  28. ctx,
  29. method,
  30. url,
  31. bytes.NewBuffer(reqBytes),
  32. )
  33. }