-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client_std.go
More file actions
42 lines (36 loc) · 962 Bytes
/
http_client_std.go
File metadata and controls
42 lines (36 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package couchdb
import (
"context"
"fmt"
"io"
"net/http"
)
// HTTPClientStd implements a `HTTPClient` using go's standart library.
type HTTPClientStd struct {
client *http.Client
}
// NewHTTPClientStd returns a new http client.
func NewHTTPClientStd(client *http.Client) *HTTPClientStd {
if client == nil {
client = http.DefaultClient
}
return &HTTPClientStd{client: client}
}
// Request performs a http request using the provided parameters.
func (c *HTTPClientStd) Request(
ctx context.Context,
method, url string,
header http.Header,
body io.Reader,
) (int, http.Header, io.ReadCloser, error) {
request, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return 0, nil, nil, fmt.Errorf("new request: %w", err)
}
request.Header = header
response, err := c.client.Do(request)
if err != nil {
return 0, nil, nil, fmt.Errorf("do: %w", err)
}
return response.StatusCode, response.Header, response.Body, nil
}