go-chatgpt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
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. }