github.com/yandex/pandora@v0.5.32/components/providers/http/decoders/ammo/ammo.go (about) 1 package ammo 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io" 8 "net/http" 9 url2 "net/url" 10 11 "github.com/yandex/pandora/components/providers/http/util" 12 "github.com/yandex/pandora/lib/netutil" 13 ) 14 15 type Ammo struct { 16 method string 17 body []byte 18 url string 19 tag string 20 header http.Header 21 } 22 23 func (a *Ammo) BuildRequest() (*http.Request, error) { 24 var buff io.Reader 25 if a.body != nil { 26 buff = bytes.NewReader(a.body) 27 } 28 req, err := http.NewRequest(a.method, a.url, buff) 29 if err != nil { 30 return nil, fmt.Errorf("cant create request: %w", err) 31 } 32 util.EnrichRequestWithHeaders(req, a.header) 33 return req, nil 34 } 35 36 func (a *Ammo) Tag() string { 37 return a.tag 38 } 39 40 func (a *Ammo) Setup(method string, url string, body []byte, header http.Header, tag string) error { 41 if ok := netutil.ValidHTTPMethod(method); !ok { 42 return errors.New("invalid HTTP method " + method) 43 } 44 if _, err := url2.Parse(url); err != nil { 45 return fmt.Errorf("invalid URL %s; err %w ", url, err) 46 } 47 48 a.method = method 49 a.body = body 50 a.url = url 51 a.tag = tag 52 a.header = header 53 return nil 54 } 55 56 func (a *Ammo) Reset() { 57 a.method = "" 58 a.body = nil 59 a.url = "" 60 a.tag = "" 61 a.header = nil 62 }