42 lines
683 B
Go
42 lines
683 B
Go
|
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,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *RequestBuilder) Build() (*http.Request, error) {
|
||
|
r, err := http.NewRequest(b.Method, b.URL.String(), b.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
r.Header = b.Header
|
||
|
return r, nil
|
||
|
}
|