Skip to content

Commit

Permalink
feat: expose request builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Jul 16, 2024
1 parent 4844fb0 commit 611fb82
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions httpreq/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/ahuigo/gohttptool/shell"
)

func (r *request) FromCurl(curl string) {
func (r *RequestBuilder) FromCurl(curl string) {

}
func (r *request) ToCurl() (curl string, err error) {
func (r *RequestBuilder) ToCurl() (curl string, err error) {
if httpreq, err := r.ToRequest(); err != nil {
return "", err
} else {
Expand Down
54 changes: 27 additions & 27 deletions httpreq/req-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"github.com/pkg/errors"
)

func (session *request) ToRequest() (*http.Request, error) {
var dataType = ContentType(session.rawreq.Header.Get("Content-Type"))
var origurl = session.url
if len(session.files) > 0 || len(session.fileHeaders) > 0 {
func (rb *RequestBuilder) ToRequest() (*http.Request, error) {
var dataType = ContentType(rb.rawreq.Header.Get("Content-Type"))
var origurl = rb.url
if len(rb.files) > 0 || len(rb.fileHeaders) > 0 {
dataType = ContentTypeFormData
}

URL, err := session.buildURLParams(origurl)
URL, err := rb.buildURLParams(origurl)
if err != nil {
return nil, err
}
Expand All @@ -31,26 +31,26 @@ func (session *request) ToRequest() (*http.Request, error) {

switch dataType {
case ContentTypeFormEncode:
if len(session.datas) > 0 {
formEncodeValues := session.buildFormEncode(session.datas)
session.setBodyFormEncode(formEncodeValues)
if len(rb.datas) > 0 {
formEncodeValues := rb.buildFormEncode(rb.datas)
rb.setBodyFormEncode(formEncodeValues)
}
case ContentTypeFormData:
// multipart/form-data
session.buildFilesAndForms()
rb.buildFilesAndForms()
}

if session.rawreq.Body == nil && session.rawreq.Method != "GET" {
session.rawreq.Body = http.NoBody
if rb.rawreq.Body == nil && rb.rawreq.Method != "GET" {
rb.rawreq.Body = http.NoBody
}

session.rawreq.URL = URL
rb.rawreq.URL = URL

return session.rawreq, nil
return rb.rawreq, nil
}

// build post Form encode
func (session *request) buildFormEncode(datas map[string]string) (Forms url.Values) {
func (rb *RequestBuilder) buildFormEncode(datas map[string]string) (Forms url.Values) {
Forms = url.Values{}
for key, value := range datas {
Forms.Add(key, value)
Expand All @@ -59,15 +59,15 @@ func (session *request) buildFormEncode(datas map[string]string) (Forms url.Valu
}

// set form urlencode
func (session *request) setBodyFormEncode(Forms url.Values) {
func (rb *RequestBuilder) setBodyFormEncode(Forms url.Values) {
data := Forms.Encode()
session.rawreq.Body = io.NopCloser(strings.NewReader(data))
session.rawreq.ContentLength = int64(len(data))
rb.rawreq.Body = io.NopCloser(strings.NewReader(data))
rb.rawreq.ContentLength = int64(len(data))
}

func (r *request) buildURLParams(userURL string) (*url.URL, error) {
params := r.params
paramsArray := r.paramsList
func (rb *RequestBuilder) buildURLParams(userURL string) (*url.URL, error) {
params := rb.params
paramsArray := rb.paramsList
if strings.HasPrefix(userURL, "/") {
userURL = "http://localhost" + userURL
} else if userURL == "" {
Expand All @@ -93,10 +93,10 @@ func (r *request) buildURLParams(userURL string) (*url.URL, error) {
return parsedURL, nil
}

func (r *request) buildFilesAndForms() error {
files := r.files
datas := r.datas
filesHeaders := r.fileHeaders
func (rb *RequestBuilder) buildFilesAndForms() error {
files := rb.files
datas := rb.datas
filesHeaders := rb.fileHeaders
//handle file multipart
var b bytes.Buffer
w := multipart.NewWriter(&b)
Expand Down Expand Up @@ -136,8 +136,8 @@ func (r *request) buildFilesAndForms() error {
w.Close()
// set file header example:
// "Content-Type": "multipart/form-data; boundary=------------------------7d87eceb5520850c",
r.rawreq.Body = io.NopCloser(bytes.NewReader(b.Bytes()))
r.rawreq.ContentLength = int64(b.Len())
r.rawreq.Header.Set("Content-Type", w.FormDataContentType())
rb.rawreq.Body = io.NopCloser(bytes.NewReader(b.Bytes()))
rb.rawreq.ContentLength = int64(b.Len())
rb.rawreq.Header.Set("Content-Type", w.FormDataContentType())
return nil
}
34 changes: 17 additions & 17 deletions httpreq/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type fileHeader struct {
// tmpshared bool
}

type request struct {
type RequestBuilder struct {
rawreq *http.Request
url string
files map[string]string // field -> path
Expand All @@ -36,8 +36,8 @@ type request struct {
paramsList map[string][]string // key -> value list
}

func R() *request {
return &request{
func R() *RequestBuilder {
return &RequestBuilder{
rawreq: &http.Request{
Method: "GET",
Header: make(http.Header),
Expand All @@ -54,28 +54,28 @@ func R() *request {
}

/******************header *************************/
func (r *request) SetHeader(key, value string) *request {
func (r *RequestBuilder) SetHeader(key, value string) *RequestBuilder {
r.rawreq.Header.Set(key, value)
return r
}

func (r *request) SetAuthBasic(username, password string) *request {
func (r *RequestBuilder) SetAuthBasic(username, password string) *RequestBuilder {
r.rawreq.SetBasicAuth(username, password)
return r
}

func (r *request) SetAuthBearer(token string) *request {
func (r *RequestBuilder) SetAuthBearer(token string) *RequestBuilder {
r.rawreq.Header.Set("Authorization", "Bearer "+token)
return r
}

func (r *request) AddCookies(cookies []*http.Cookie) *request {
func (r *RequestBuilder) AddCookies(cookies []*http.Cookie) *RequestBuilder {
for _, cookie := range cookies {
r.rawreq.AddCookie(cookie)
}
return r
}
func (r *request) AddCookieKV(name, value string) *request {
func (r *RequestBuilder) AddCookieKV(name, value string) *RequestBuilder {
cookie := &http.Cookie{
Name: name,
Value: value,
Expand All @@ -85,12 +85,12 @@ func (r *request) AddCookieKV(name, value string) *request {
}

/************** file **********************/
func (r *request) AddFile(fieldname, path string) *request {
func (r *RequestBuilder) AddFile(fieldname, path string) *RequestBuilder {
r.files[fieldname] = path
return r
}

func (r *request) AddFileHeader(fieldname, filename string, content []byte) *request {
func (r *RequestBuilder) AddFileHeader(fieldname, filename string, content []byte) *RequestBuilder {
r.fileHeaders[fieldname] = fileHeader{
Filename: filename,
content: content,
Expand All @@ -99,37 +99,37 @@ func (r *request) AddFileHeader(fieldname, filename string, content []byte) *req
return r
}

func (r *request) SetUrl(url string) *request {
func (r *RequestBuilder) SetUrl(url string) *RequestBuilder {
r.url = url
return r
}

func (r *request) SetReq(method string, url string) *request {
func (r *RequestBuilder) SetReq(method string, url string) *RequestBuilder {
r.rawreq.Method = method
r.url = url
return r
}

func (r *request) SetParams(params map[string]string) *request {
func (r *RequestBuilder) SetParams(params map[string]string) *RequestBuilder {
r.params = params
return r
}

func (r *request) SetData(data map[string]string) *request {
func (r *RequestBuilder) SetData(data map[string]string) *RequestBuilder {
r.datas = data
return r
}

func (r *request) GetRawreq() *http.Request {
func (r *RequestBuilder) GetRawreq() *http.Request {
return r.rawreq
}

func (r *request) SetCtx(ctx context.Context) *request {
func (r *RequestBuilder) SetCtx(ctx context.Context) *RequestBuilder {
r.rawreq = r.rawreq.WithContext(ctx)
return r
}

func (r *request) EnableTrace(ctx context.Context) *request {
func (r *RequestBuilder) EnableTrace(ctx context.Context) *RequestBuilder {
trace := clientTraceNew(r.rawreq.Context())
r.rawreq = r.rawreq.WithContext(trace.ctx)
return r
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.4
v0.0.5

0 comments on commit 611fb82

Please sign in to comment.