1- // Package request provides clients for requesting data .
2- package request
1+ // Package http provides a http client .
2+ package http
33
44import (
5- "bytes"
65 "context"
76 "fmt"
87 "io/ioutil"
@@ -29,7 +28,7 @@ func (e ErrHTTP) Error() string {
2928 return fmt .Sprintf ("http error %d" , e .StatusCode )
3029}
3130
32- func client () * http.Client {
31+ func httpClient () * http.Client {
3332 // TODO: Longer timeout?
3433 transport := & http.Transport {
3534 Dial : (& net.Dialer {
@@ -57,12 +56,8 @@ func client() *http.Client {
5756 return client
5857}
5958
60- func doRequest (client * http.Client , method string , urs string , headers []Header , body []byte , options ... func (* http.Request )) (http.Header , []byte , error ) {
61- logger .Debugf ("Requesting %s %s" , method , urs )
62- req , err := http .NewRequest (method , urs , bytes .NewReader (body ))
63- if err != nil {
64- return nil , nil , err
65- }
59+ func doRequest (client * http.Client , req * Request , headers []Header , options ... func (* http.Request )) (http.Header , []byte , error ) {
60+ logger .Debugf ("Requesting %s %s" , req .Method , req .URL )
6661
6762 req .Header .Set ("User-Agent" , "keys.pub" )
6863 for _ , header := range headers {
@@ -139,21 +134,21 @@ type Header struct {
139134 Value string
140135}
141136
142- // Requestor defines how to get bytes from a URL .
143- type Requestor interface {
144- RequestURLString (ctx context.Context , urs string , headers []Header ) ([]byte , error )
137+ // Client defines how to request a resource .
138+ type Client interface {
139+ Request (ctx context.Context , req * Request , headers []Header ) ([]byte , error )
145140}
146141
147- type requestor struct {}
142+ type client struct {}
148143
149- // NewHTTPRequestor creates a Requestor for HTTP URLs.
150- func NewHTTPRequestor () Requestor {
151- return requestor {}
144+ // NewClient creates a Requestor for HTTP URLs.
145+ func NewClient () Client {
146+ return client {}
152147}
153148
154- // RequestURLString requests an URL string .
155- func (r requestor ) RequestURLString (ctx context.Context , urs string , headers []Header ) ([]byte , error ) {
156- _ , body , err := doRequest (client (), "GET" , urs , headers , nil )
149+ // Request an URL.
150+ func (c client ) Request (ctx context.Context , req * Request , headers []Header ) ([]byte , error ) {
151+ _ , body , err := doRequest (httpClient (), req , headers )
157152 if err != nil {
158153 logger .Warningf ("Failed request: %s" , err )
159154 }
@@ -165,40 +160,41 @@ type mockResponse struct {
165160 err error
166161}
167162
168- var _ Requestor = & MockRequestor {}
163+ var _ Client = & Mock {}
169164
170- // MockRequestor ...
171- type MockRequestor struct {
165+ // Mock ...
166+ type Mock struct {
172167 resp map [string ]* mockResponse
173168}
174169
175- // NewMockRequestor with mocked responses.
176- func NewMockRequestor () * MockRequestor {
177- return & MockRequestor {resp : map [string ]* mockResponse {}}
170+ // NewMock with mocked responses.
171+ func NewMock () * Mock {
172+ return & Mock {resp : map [string ]* mockResponse {}}
178173}
179174
180175// SetResponse ...
181- func (r * MockRequestor ) SetResponse (url string , b []byte ) {
176+ func (r * Mock ) SetResponse (url string , b []byte ) {
182177 r .resp [url ] = & mockResponse {data : b }
183178}
184179
185180// Response returns mocked response.
186- func (r * MockRequestor ) Response (url string ) ([]byte , error ) {
181+ func (r * Mock ) Response (url string ) ([]byte , error ) {
182+ // TODO: Match on method without params, etc.
187183 resp , ok := r .resp [url ]
188184 if ! ok {
189- return nil , errors .Errorf ("no mock response for %s" , url )
185+ panic ( errors .Errorf ("no mock response for %s" , url ) )
190186 }
191187 logger .Debugf ("Mock response %s, data=%d; err=%s" , url , len (resp .data ), resp .err )
192188 // logger.Debugf("Mock data: %s", string(resp.data))
193189 return resp .data , resp .err
194190}
195191
196- // SetError ...
197- func (r * MockRequestor ) SetError (url string , err error ) {
192+ // SetError sets response error for ur
193+ func (r * Mock ) SetError (url string , err error ) {
198194 r .resp [url ] = & mockResponse {err : err }
199195}
200196
201- // RequestURLString .. .
202- func (r * MockRequestor ) RequestURLString (ctx context.Context , urs string , headers []Header ) ([]byte , error ) {
203- return r .Response (urs )
197+ // Request mock response .
198+ func (r * Mock ) Request (ctx context.Context , req * Request , headers []Header ) ([]byte , error ) {
199+ return r .Response (req . URL . String () )
204200}
0 commit comments