MCPcopy Index your code
hub / github.com/CodisLabs/codis / apiRequestJson

Function apiRequestJson

pkg/utils/rpc/api.go:111–166  ·  view source on GitHub ↗
(method string, url string, args, reply interface{})

Source from the content-addressed store, hash-verified

109}
110
111func apiRequestJson(method string, url string, args, reply interface{}) error {
112 var body []byte
113 if args != nil {
114 b, err := apiMarshalJson(args)
115 if err != nil {
116 return errors.Trace(err)
117 }
118 body = b
119 }
120
121 req, err := http.NewRequest(method, url, bytes.NewReader(body))
122 if err != nil {
123 return errors.Trace(err)
124 }
125 if body != nil {
126 req.Header.Set("Content-Type", "application/json")
127 req.Header.Set("Content-Length", strconv.Itoa(len(body)))
128 }
129
130 var start = time.Now()
131
132 rsp, err := client.Do(req)
133 if err != nil {
134 return errors.Trace(err)
135 }
136 defer func() {
137 io.Copy(ioutil.Discard, rsp.Body)
138 rsp.Body.Close()
139 log.Debugf("call rpc [%s] %s in %v", method, url, time.Since(start))
140 }()
141
142 switch rsp.StatusCode {
143 case 200:
144 b, err := responseBodyAsBytes(rsp)
145 if err != nil {
146 return err
147 }
148 if reply == nil {
149 return nil
150 }
151 if err := json.Unmarshal(b, reply); err != nil {
152 return errors.Trace(err)
153 } else {
154 return nil
155 }
156 case 800, 1500:
157 e, err := responseBodyAsError(rsp)
158 if err != nil {
159 return err
160 } else {
161 return e
162 }
163 default:
164 return errors.Errorf("[%d] %s - %s", rsp.StatusCode, http.StatusText(rsp.StatusCode), url)
165 }
166}
167
168func ApiGetJson(url string, reply interface{}) error {

Callers 3

ApiGetJsonFunction · 0.85
ApiPutJsonFunction · 0.85
ApiPostJsonFunction · 0.85

Calls 8

apiMarshalJsonFunction · 0.85
responseBodyAsBytesFunction · 0.85
responseBodyAsErrorFunction · 0.85
DebugfMethod · 0.80
ErrorfMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45
DoMethod · 0.45

Tested by

no test coverage detected