ezhttp/request_builder.go

42 lines
664 B
Go
Raw Normal View History

2024-09-11 13:32:52 +02:00
package ezhttp
import (
"io"
"net/http"
urlpkg "net/url"
)
type RequestBuilder struct {
URL *urlpkg.URL
Method string
Header http.Header
Body io.Reader
}
func NewRequestBuilder() *RequestBuilder {
return &RequestBuilder{
URL: &urlpkg.URL{},
Header: make(http.Header),
Body: nil,
}
}
func NewRequestBuilderFromRequest(r *http.Request) *RequestBuilder {
return &RequestBuilder{
URL: r.URL,
Method: r.Method,
Header: r.Header,
Body: r.Body,
}
}
2024-09-11 19:19:00 +02:00
func (b *RequestBuilder) Build() *http.Request {
2024-09-11 13:32:52 +02:00
r, err := http.NewRequest(b.Method, b.URL.String(), b.Body)
if err != nil {
2024-09-11 19:19:00 +02:00
return nil
2024-09-11 13:32:52 +02:00
}
r.Header = b.Header
2024-09-11 19:19:00 +02:00
return r
2024-09-11 13:32:52 +02:00
}