summaryrefslogtreecommitdiffstats
path: root/lib/common/httpclient.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-17 11:28:57 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-05-17 14:04:32 +0200
commit40a7183f3b4aa32379aa8b4949f5f9c5e32f79f6 (patch)
tree25a98c1b6c6c7b5e186ae3cf0dc11807b2fa088a /lib/common/httpclient.go
parentc07adb807c41a1545a9a0f5bbf40080d86946538 (diff)
Add SDKs support.
Don't allow to install SDKs through XDS for now. Only probe existing SDKs that have been manually installed using scripts/agl/install-agl-sdks.sh. Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/common/httpclient.go')
-rw-r--r--lib/common/httpclient.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/common/httpclient.go b/lib/common/httpclient.go
index 40d7bc2..72132bf 100644
--- a/lib/common/httpclient.go
+++ b/lib/common/httpclient.go
@@ -9,6 +9,8 @@ import (
"io/ioutil"
"net/http"
"strings"
+
+ "github.com/Sirupsen/logrus"
)
type HTTPClient struct {
@@ -20,6 +22,7 @@ type HTTPClient struct {
id string
csrf string
conf HTTPClientConfig
+ logger *logrus.Logger
}
type HTTPClientConfig struct {
@@ -30,6 +33,13 @@ type HTTPClientConfig struct {
CsrfDisable bool
}
+const (
+ logError = 1
+ logWarning = 2
+ logInfo = 3
+ logDebug = 4
+)
+
// Inspired by syncthing/cmd/cli
const insecure = false
@@ -64,6 +74,30 @@ func HTTPNewClient(baseURL string, cfg HTTPClientConfig) (*HTTPClient, error) {
return &client, nil
}
+// SetLogger Define the logger to use
+func (c *HTTPClient) SetLogger(log *logrus.Logger) {
+ c.logger = log
+}
+
+func (c *HTTPClient) log(level int, format string, args ...interface{}) {
+ if c.logger != nil {
+ switch level {
+ case logError:
+ c.logger.Errorf(format, args...)
+ break
+ case logWarning:
+ c.logger.Warningf(format, args...)
+ break
+ case logInfo:
+ c.logger.Infof(format, args...)
+ break
+ default:
+ c.logger.Debugf(format, args...)
+ break
+ }
+ }
+}
+
// Send request to retrieve Client id and/or CSRF token
func (c *HTTPClient) getCidAndCsrf() error {
request, err := http.NewRequest("GET", c.endpoint, nil)
@@ -171,6 +205,8 @@ func (c *HTTPClient) handleRequest(request *http.Request) (*http.Response, error
request.Header.Set("X-CSRF-Token-"+c.id[:5], c.csrf)
}
+ c.log(logDebug, "HTTP %s %v", request.Method, request.URL)
+
response, err := c.httpClient.Do(request)
if err != nil {
return nil, err