Skip to content

Commit

Permalink
Merge pull request #61 from ArtisanCloud/feature/httpRequest
Browse files Browse the repository at this point in the history
feat(http): add ParseResponseBodyContent
  • Loading branch information
Matrix-X authored Dec 23, 2022
2 parents 3a4714c + bb58cf1 commit 1cf23e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# PowerLibs

作为ArtisanCloud开发Golang项目的通用库,我们把很多基础模块常用到的方法,放在了PowerLib中。


特别感谢 <a href="https://github.com/northseadl">Northseadl</a>的贡献

<a href="https://github.com/northseadl">
<img width="50" src="https://avatars.githubusercontent.com/u/99570485?v=4">
</a>
39 changes: 39 additions & 0 deletions http/helper/requestHelper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package helper

import (
"bytes"
"encoding/xml"
"github.com/ArtisanCloud/PowerLibs/v3/http/contract"
"github.com/ArtisanCloud/PowerLibs/v3/http/dataflow"
"github.com/ArtisanCloud/PowerLibs/v3/http/drivers/http"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"io"
"io/ioutil"
http2 "net/http"
)

type RequestDownload struct {
HashType string `json:"hash_type""`
HashValue string `json:"hash_value""`
DownloadURL string `json:"download_url""`
}

type RequestHelper struct {
client contract.ClientInterface
middlewareHandle contract.RequestMiddleware
Expand Down Expand Up @@ -59,3 +71,30 @@ func (r *RequestHelper) Df() contract.RequestDataflowInterface {
BaseUrl: r.config.BaseUrl,
})
}

func (r *RequestHelper) ParseResponseBodyContent(rs *http2.Response, outBody interface{}) error {

b, err := io.ReadAll(rs.Body)
if err != nil {
return err
}
rs.Body = ioutil.NopCloser(bytes.NewBuffer(b))

content := string(b)

if content[0:1] == "<" {
err = xml.Unmarshal(b, outBody)
if err != nil {
return err
}
} else {
// Handle JSON format.
err = object.JsonDecode(b, outBody)
if err != nil {
return err
}
}

return nil

}

0 comments on commit 1cf23e6

Please sign in to comment.