Skip to content

Commit

Permalink
Merge pull request #68 from ArtisanCloud/feature/httpRequest
Browse files Browse the repository at this point in the history
Feature/http request
  • Loading branch information
Matrix-X authored Jan 5, 2023
2 parents dcc8dfd + 6c2c4bc commit ecd5054
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
48 changes: 42 additions & 6 deletions http/dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"reflect"
"strings"
)

type Dataflow struct {
Expand All @@ -35,8 +36,13 @@ func NewDataflow(client contract.ClientInterface, middlewareHandle contract.Requ
df := Dataflow{
client: client,
middlewareHandle: middlewareHandle,
request: &http.Request{},
option: option,
request: &http.Request{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
},
option: option,
}
if option == nil {
return &df
Expand All @@ -47,6 +53,7 @@ func NewDataflow(client contract.ClientInterface, middlewareHandle contract.Requ
df.err = append(df.err, errors.Wrap(err, "base url invalid"))
}
df.request.URL = u
df.request.Host = u.Host
}
return &df
}
Expand Down Expand Up @@ -141,19 +148,48 @@ func (d *Dataflow) Json(jsonAny interface{}) contract.RequestDataflowInterface {
d.Header("content-type", "application/json")
// 标准库Json编码 body reader
var buf bytes.Buffer
reader := io.NopCloser(&buf)
encoder := json.NewEncoder(&buf)
d.request.Body = reader
if err := encoder.Encode(jsonAny); err != nil {
d.err = append(d.err, errors.Wrap(err, "json body encode failed"))
return d
}
d.Body(&buf)
return d
}

func (d *Dataflow) Body(body io.Reader) contract.RequestDataflowInterface {
if body != nil {
d.request.Body = io.NopCloser(body)

switch v := body.(type) {
case *bytes.Buffer:
d.request.ContentLength = int64(v.Len())
buf := v.Bytes()
d.request.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(buf)
return io.NopCloser(r), nil
}
case *bytes.Reader:
d.request.ContentLength = int64(v.Len())
snapshot := *v
d.request.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
case *strings.Reader:
d.request.ContentLength = int64(v.Len())
snapshot := *v
d.request.GetBody = func() (io.ReadCloser, error) {
r := snapshot
return io.NopCloser(&r), nil
}
default:
}

if d.request.GetBody != nil && d.request.ContentLength == 0 {
d.request.Body = http.NoBody
d.request.GetBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
}
}
return d
}
Expand All @@ -163,7 +199,7 @@ func (d *Dataflow) Any(data contract.BodyEncoder) contract.RequestDataflowInterf
if err != nil {
d.err = append(d.err, errors.Wrap(err, "body encode failed"))
}
d.request.Body = io.NopCloser(body)
d.Body(body)
return d
}

Expand All @@ -177,7 +213,7 @@ func (d *Dataflow) Xml(xmlAny interface{}) contract.RequestDataflowInterface {
if err != nil {
d.err = append(d.err, err)
}
d.request.Body = io.NopCloser(&buf)
d.Body(&buf)
return d
}

Expand Down
16 changes: 16 additions & 0 deletions http/helper/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@ func HttpDebugMiddleware(debug bool) contract.RequestMiddleware {
var output bytes.Buffer
if debug {
output.WriteString(fmt.Sprintf("%s %s ", request.Method, request.URL.String()))

// print out request header
output.Write([]byte("\r\nrequest header: { \r\n"))
for k, vals := range request.Header {
for _, v := range vals {
output.Write([]byte(fmt.Sprintf("\t%s:%s\r\n", k, v)))
}
}
output.Write([]byte("} \r\n"))

// print out request body
if request.Body != nil {

output.Write([]byte("request body:"))
var buf bytes.Buffer
reader := io.TeeReader(request.Body, &buf)
body, _ := io.ReadAll(reader)
Expand All @@ -28,6 +41,9 @@ func HttpDebugMiddleware(debug bool) contract.RequestMiddleware {
response, err = handle(request)

if debug {

output.Write([]byte("\r\n------------------\r\n"))
output.Write([]byte("response content:\r\n"))
dumpRes, _ := httputil.DumpResponse(response, true)
output.Write(dumpRes)
log.Println(output.String())
Expand Down

0 comments on commit ecd5054

Please sign in to comment.