Skip to content

Commit

Permalink
x/httputil
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jun 13, 2022
1 parent ac02073 commit 2312b28
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
48 changes: 48 additions & 0 deletions httputil/httputil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package httputil

import (
"encoding/json"
"io"
"log"
"net/http"
"strconv"
)

// ----------------------------------------------------------

// Reply replies a http request with a json response.
func Reply(w http.ResponseWriter, code int, data interface{}) {
msg, err := json.Marshal(data)
if err != nil {
panic(err)
}
h := w.Header()
h.Set("Content-Length", strconv.Itoa(len(msg)))
h.Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(msg)
}

// ReplyWith replies a http request with a bodyType response.
func ReplyWith(w http.ResponseWriter, code int, bodyType string, msg []byte) {
h := w.Header()
h.Set("Content-Length", strconv.Itoa(len(msg)))
h.Set("Content-Type", bodyType)
w.WriteHeader(code)
w.Write(msg)
}

// ReplyWithStream replies a http request with a streaming response.
func ReplyWithStream(w http.ResponseWriter, code int, bodyType string, body io.Reader, bytes int64) {
h := w.Header()
h.Set("Content-Length", strconv.FormatInt(bytes, 10))
h.Set("Content-Type", bodyType)
w.WriteHeader(code)
// We don't use io.CopyN: if you need, call io.LimitReader(body, bytes) by yourself
written, err := io.Copy(w, body)
if err != nil || written != bytes {
log.Printf("ReplyWithStream (bytes=%v): written=%v, err=%v\n", bytes, written, err)
}
}

// ----------------------------------------------------------
11 changes: 0 additions & 11 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
// --------------------------------------------------------------------

func NewRequest(method, url1 string, body io.Reader) (req *http.Request, err error) {

var host string

// url1 = "-H <Host> http://<ip>[:<port>]/<path>"
Expand All @@ -62,7 +61,6 @@ func NewRequest(method, url1 string, body io.Reader) (req *http.Request, err err
}

func (r Client) DoRequest(ctx Context, method, url string) (resp *http.Response, err error) {

req, err := NewRequest(method, url, nil)
if err != nil {
return
Expand Down Expand Up @@ -124,7 +122,6 @@ func (r Client) DoRequestWithJson(
}

func (r Client) Do(ctx Context, req *http.Request) (resp *http.Response, err error) {

if ctx == nil {
ctx = Background()
}
Expand Down Expand Up @@ -180,30 +177,25 @@ type ErrorInfo struct {
}

func (r *ErrorInfo) ErrorDetail() string {

msg, _ := json.Marshal(r)
return string(msg)
}

func (r *ErrorInfo) Error() string {

return r.Err
}

func (r *ErrorInfo) RpcError() (code, errno int, key, err string) {

return r.Code, r.Errno, r.Key, r.Err
}

func (r *ErrorInfo) HttpCode() int {

return r.Code
}

// --------------------------------------------------------------------

func parseError(e *ErrorInfo, r io.Reader) {

body, err1 := ioutil.ReadAll(r)
if err1 != nil {
e.Err = err1.Error()
Expand All @@ -224,7 +216,6 @@ func parseError(e *ErrorInfo, r io.Reader) {
}

func ResponseError(resp *http.Response) (err error) {

e := &ErrorInfo{
Reqid: resp.Header.Get("X-Reqid"),
Code: resp.StatusCode,
Expand All @@ -241,7 +232,6 @@ func ResponseError(resp *http.Response) (err error) {
}

func CallRet(ctx Context, ret interface{}, resp *http.Response) (err error) {

defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
Expand Down Expand Up @@ -322,7 +312,6 @@ type nestedObjectGetter interface {
}

func getRequestCanceler(tp http.RoundTripper) (rc requestCanceler, ok bool) {

if rc, ok = tp.(requestCanceler); ok {
return
}
Expand Down

0 comments on commit 2312b28

Please sign in to comment.