-
Notifications
You must be signed in to change notification settings - Fork 29
/
tools.go
37 lines (34 loc) · 1023 Bytes
/
tools.go
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
package connect
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
// date formats a time.Time as a date usable in the Garmin Connect API.
func formatDate(t time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())
}
// drainBody reads all of b to memory and then returns two equivalent
// ReadClosers yielding the same bytes.
//
// It returns an error if the initial slurp of all bytes fails. It does not attempt
// to make the returned ReadClosers have identical error-matching behavior.
//
// Liberated from net/http/httputil/dump.go.
func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if b == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(b); err != nil {
return nil, b, err
}
if err = b.Close(); err != nil {
return nil, b, err
}
return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}