(r *request)
| 110 | type RespBody struct{} |
| 111 | |
| 112 | func (c *MetaHttpClient) serveRequest(r *request) (respData []byte, err error) { |
| 113 | var resp *http.Response |
| 114 | var schema string |
| 115 | if c.useSSL { |
| 116 | schema = "https" |
| 117 | } else { |
| 118 | schema = "http" |
| 119 | } |
| 120 | url := fmt.Sprintf("%s://%s%s", schema, c.host, |
| 121 | r.path) |
| 122 | resp, err = c.httpRequest(r.method, url, r.params, r.header, r.body) |
| 123 | log.LogInfof("resp %v,err %v", resp, err) |
| 124 | if err != nil { |
| 125 | log.LogErrorf("serveRequest: send http request fail: method(%v) url(%v) err(%v)", r.method, url, err) |
| 126 | return |
| 127 | } |
| 128 | stateCode := resp.StatusCode |
| 129 | respData, err = io.ReadAll(resp.Body) |
| 130 | _ = resp.Body.Close() |
| 131 | if err != nil { |
| 132 | log.LogErrorf("serveRequest: read http response body fail: err(%v)", err) |
| 133 | return |
| 134 | } |
| 135 | switch stateCode { |
| 136 | case http.StatusOK: |
| 137 | body := new(proto.HTTPReplyRaw) |
| 138 | if err := body.Unmarshal(respData); err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | // o represent proto.ErrCodeSuccess, TODO: 200 ??? |
| 142 | if body.Code != 200 { |
| 143 | return nil, proto.ParseErrorCode(body.Code) |
| 144 | } |
| 145 | return body.Bytes(), nil |
| 146 | default: |
| 147 | log.LogErrorf("serveRequest: unknown status: host(%v) uri(%v) status(%v) body(%s).", |
| 148 | resp.Request.URL.String(), c.host, stateCode, strings.Replace(string(respData), "\n", "", -1)) |
| 149 | } |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | func (c *MetaHttpClient) httpRequest(method, url string, param, header map[string]string, reqData []byte) (resp *http.Response, err error) { |
| 154 | client := http.DefaultClient |
no test coverage detected