ResponseProcessor.go raw
1 package core
2
3 import (
4 "errors"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8 )
9
10 type ResponseProcessor interface {
11 Process(response *http.Response) ([]byte, error)
12 }
13
14 func GetResponseProcessor(method string) ResponseProcessor {
15 if method == MethodHead {
16 return &WithoutBodyResponseProcessor{}
17 } else {
18 return &WithBodyResponseProcessor{}
19 }
20 }
21
22 type WithBodyResponseProcessor struct {
23 }
24
25 func (p WithBodyResponseProcessor) Process(response *http.Response) ([]byte, error) {
26 defer response.Body.Close()
27 return ioutil.ReadAll(response.Body)
28 }
29
30 type WithoutBodyResponseProcessor struct {
31 }
32
33 func (p WithoutBodyResponseProcessor) Process(response *http.Response) ([]byte, error) {
34 requestId := response.Header.Get(HeaderJdcloudRequestId)
35 if requestId != "" {
36 return []byte(fmt.Sprintf(`{"requestId":"%s"}`, requestId)), nil
37 }
38
39 return nil, errors.New("can not get requestId in HEAD response")
40 }
41