code simplify for package httplib
Showing
1 changed file
with
37 additions
and
29 deletions
| ... | @@ -253,30 +253,20 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { | ... | @@ -253,30 +253,20 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { |
| 253 | return b | 253 | return b |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | 256 | func (b *BeegoHttpRequest) buildUrl(paramBody string) { |
| 257 | if b.resp.StatusCode != 0 { | 257 | // build GET url with query string |
| 258 | return b.resp, nil | ||
| 259 | } | ||
| 260 | var paramBody string | ||
| 261 | if len(b.params) > 0 { | ||
| 262 | var buf bytes.Buffer | ||
| 263 | for k, v := range b.params { | ||
| 264 | buf.WriteString(url.QueryEscape(k)) | ||
| 265 | buf.WriteByte('=') | ||
| 266 | buf.WriteString(url.QueryEscape(v)) | ||
| 267 | buf.WriteByte('&') | ||
| 268 | } | ||
| 269 | paramBody = buf.String() | ||
| 270 | paramBody = paramBody[0 : len(paramBody)-1] | ||
| 271 | } | ||
| 272 | |||
| 273 | if b.req.Method == "GET" && len(paramBody) > 0 { | 258 | if b.req.Method == "GET" && len(paramBody) > 0 { |
| 274 | if strings.Index(b.url, "?") != -1 { | 259 | if strings.Index(b.url, "?") != -1 { |
| 275 | b.url += "&" + paramBody | 260 | b.url += "&" + paramBody |
| 276 | } else { | 261 | } else { |
| 277 | b.url = b.url + "?" + paramBody | 262 | b.url = b.url + "?" + paramBody |
| 278 | } | 263 | } |
| 279 | } else if b.req.Method == "POST" && b.req.Body == nil { | 264 | return |
| 265 | } | ||
| 266 | |||
| 267 | // build POST url and body | ||
| 268 | if b.req.Method == "POST" && b.req.Body == nil { | ||
| 269 | // with files | ||
| 280 | if len(b.files) > 0 { | 270 | if len(b.files) > 0 { |
| 281 | pr, pw := io.Pipe() | 271 | pr, pw := io.Pipe() |
| 282 | bodyWriter := multipart.NewWriter(pw) | 272 | bodyWriter := multipart.NewWriter(pw) |
| ... | @@ -305,12 +295,35 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -305,12 +295,35 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 305 | }() | 295 | }() |
| 306 | b.Header("Content-Type", bodyWriter.FormDataContentType()) | 296 | b.Header("Content-Type", bodyWriter.FormDataContentType()) |
| 307 | b.req.Body = ioutil.NopCloser(pr) | 297 | b.req.Body = ioutil.NopCloser(pr) |
| 308 | } else if len(paramBody) > 0 { | 298 | return |
| 299 | } | ||
| 300 | |||
| 301 | // with params | ||
| 302 | if len(paramBody) > 0 { | ||
| 309 | b.Header("Content-Type", "application/x-www-form-urlencoded") | 303 | b.Header("Content-Type", "application/x-www-form-urlencoded") |
| 310 | b.Body(paramBody) | 304 | b.Body(paramBody) |
| 311 | } | 305 | } |
| 312 | } | 306 | } |
| 307 | } | ||
| 313 | 308 | ||
| 309 | func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ||
| 310 | if b.resp.StatusCode != 0 { | ||
| 311 | return b.resp, nil | ||
| 312 | } | ||
| 313 | var paramBody string | ||
| 314 | if len(b.params) > 0 { | ||
| 315 | var buf bytes.Buffer | ||
| 316 | for k, v := range b.params { | ||
| 317 | buf.WriteString(url.QueryEscape(k)) | ||
| 318 | buf.WriteByte('=') | ||
| 319 | buf.WriteString(url.QueryEscape(v)) | ||
| 320 | buf.WriteByte('&') | ||
| 321 | } | ||
| 322 | paramBody = buf.String() | ||
| 323 | paramBody = paramBody[0 : len(paramBody)-1] | ||
| 324 | } | ||
| 325 | |||
| 326 | b.buildUrl(paramBody) | ||
| 314 | url, err := url.Parse(b.url) | 327 | url, err := url.Parse(b.url) |
| 315 | if err != nil { | 328 | if err != nil { |
| 316 | return nil, err | 329 | return nil, err |
| ... | @@ -342,14 +355,12 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -342,14 +355,12 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 342 | } | 355 | } |
| 343 | } | 356 | } |
| 344 | 357 | ||
| 345 | var jar http.CookieJar | 358 | var jar http.CookieJar = nil |
| 346 | if b.setting.EnableCookie { | 359 | if b.setting.EnableCookie { |
| 347 | if defaultCookieJar == nil { | 360 | if defaultCookieJar == nil { |
| 348 | createDefaultCookie() | 361 | createDefaultCookie() |
| 349 | } | 362 | } |
| 350 | jar = defaultCookieJar | 363 | jar = defaultCookieJar |
| 351 | } else { | ||
| 352 | jar = nil | ||
| 353 | } | 364 | } |
| 354 | 365 | ||
| 355 | client := &http.Client{ | 366 | client := &http.Client{ |
| ... | @@ -402,12 +413,11 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { | ... | @@ -402,12 +413,11 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) { |
| 402 | return nil, nil | 413 | return nil, nil |
| 403 | } | 414 | } |
| 404 | defer resp.Body.Close() | 415 | defer resp.Body.Close() |
| 405 | data, err := ioutil.ReadAll(resp.Body) | 416 | b.body, err = ioutil.ReadAll(resp.Body) |
| 406 | if err != nil { | 417 | if err != nil { |
| 407 | return nil, err | 418 | return nil, err |
| 408 | } | 419 | } |
| 409 | b.body = data | 420 | return b.body, nil |
| 410 | return data, nil | ||
| 411 | } | 421 | } |
| 412 | 422 | ||
| 413 | // ToFile saves the body data in response to one file. | 423 | // ToFile saves the body data in response to one file. |
| ... | @@ -438,8 +448,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { | ... | @@ -438,8 +448,7 @@ func (b *BeegoHttpRequest) ToJson(v interface{}) error { |
| 438 | if err != nil { | 448 | if err != nil { |
| 439 | return err | 449 | return err |
| 440 | } | 450 | } |
| 441 | err = json.Unmarshal(data, v) | 451 | return json.Unmarshal(data, v) |
| 442 | return err | ||
| 443 | } | 452 | } |
| 444 | 453 | ||
| 445 | // ToXml returns the map that marshals from the body bytes as xml in response . | 454 | // ToXml returns the map that marshals from the body bytes as xml in response . |
| ... | @@ -449,8 +458,7 @@ func (b *BeegoHttpRequest) ToXml(v interface{}) error { | ... | @@ -449,8 +458,7 @@ func (b *BeegoHttpRequest) ToXml(v interface{}) error { |
| 449 | if err != nil { | 458 | if err != nil { |
| 450 | return err | 459 | return err |
| 451 | } | 460 | } |
| 452 | err = xml.Unmarshal(data, v) | 461 | return xml.Unmarshal(data, v) |
| 453 | return err | ||
| 454 | } | 462 | } |
| 455 | 463 | ||
| 456 | // Response executes request client gets response mannually. | 464 | // Response executes request client gets response mannually. | ... | ... |
-
Please register or sign in to post a comment