Byte-snake-engine/cli/commands/http.go
Rob O'Dwyer 5f60ccbba8
DEV 1793: Track latency, status codes, and errors from CLI games (#113)
* track latency and status code from CLI games

* actually track errors from snake responses
2022-10-04 15:53:29 -07:00

28 lines
726 B
Go

package commands
import (
"io"
"net/http"
"time"
)
type TimedHttpClient interface {
Get(url string) (*http.Response, time.Duration, error)
Post(url string, contentType string, body io.Reader) (*http.Response, time.Duration, error)
}
type timedHTTPClient struct {
*http.Client
}
func (client timedHTTPClient) Get(url string) (*http.Response, time.Duration, error) {
startTime := time.Now()
res, err := client.Client.Get(url)
return res, time.Since(startTime), err
}
func (client timedHTTPClient) Post(url string, contentType string, body io.Reader) (*http.Response, time.Duration, error) {
startTime := time.Now()
res, err := client.Client.Post(url, contentType, body)
return res, time.Since(startTime), err
}