2020-04-19 21:53:53 +00:00
|
|
|
package httpdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultClient implements Client and wraps around the stdlib Client.
|
2020-05-03 21:02:03 +00:00
|
|
|
type DefaultClient http.Client
|
2020-04-19 21:53:53 +00:00
|
|
|
|
|
|
|
var _ Client = (*DefaultClient)(nil)
|
|
|
|
|
|
|
|
// WrapClient wraps around the standard library's http.Client and returns an
|
|
|
|
// implementation that's compatible with the Client driver interface.
|
|
|
|
func WrapClient(client http.Client) Client {
|
2020-05-03 21:02:03 +00:00
|
|
|
return DefaultClient(client)
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 04:33:00 +00:00
|
|
|
// NewClient creates a new client around the standard library's http.Client.
|
2020-04-19 21:53:53 +00:00
|
|
|
func NewClient() Client {
|
2024-09-16 04:33:00 +00:00
|
|
|
return WrapClient(http.Client{})
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
func (d DefaultClient) NewRequest(ctx context.Context, method, url string) (Request, error) {
|
2020-04-19 21:53:53 +00:00
|
|
|
r, err := http.NewRequestWithContext(ctx, method, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return (*DefaultRequest)(r), nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
func (d DefaultClient) Do(req Request) (Response, error) {
|
2020-04-19 21:53:53 +00:00
|
|
|
// Implementations can safely assert this.
|
|
|
|
request := req.(*DefaultRequest)
|
|
|
|
|
2020-05-03 21:02:03 +00:00
|
|
|
r, err := (*http.Client)(&d).Do((*http.Request)(request))
|
2020-04-19 21:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*DefaultResponse)(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultRequest wraps around the stdlib Request and satisfies the Request
|
|
|
|
// interface.
|
|
|
|
type DefaultRequest http.Request
|
|
|
|
|
|
|
|
var _ Request = (*DefaultRequest)(nil)
|
|
|
|
|
|
|
|
func (r *DefaultRequest) GetPath() string {
|
|
|
|
return r.URL.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRequest) GetContext() context.Context {
|
|
|
|
return (*http.Request)(r).Context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRequest) AddQuery(values url.Values) {
|
|
|
|
var qs = r.URL.Query()
|
|
|
|
for k, v := range values {
|
|
|
|
qs[k] = append(qs[k], v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.URL.RawQuery = qs.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRequest) AddHeader(header http.Header) {
|
|
|
|
for key, values := range header {
|
2022-04-03 05:34:41 +00:00
|
|
|
r.Header[key] = values
|
2020-04-19 21:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRequest) WithBody(body io.ReadCloser) {
|
|
|
|
r.Body = body
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultResponse wraps around the stdlib Response and satisfies the Response
|
|
|
|
// interface.
|
|
|
|
type DefaultResponse http.Response
|
|
|
|
|
|
|
|
var _ Response = (*DefaultResponse)(nil)
|
|
|
|
|
|
|
|
func (r *DefaultResponse) GetStatus() int {
|
|
|
|
return r.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultResponse) GetHeader() http.Header {
|
|
|
|
return r.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultResponse) GetBody() io.ReadCloser {
|
|
|
|
return r.Body
|
|
|
|
}
|