forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
55 lines (42 loc) · 926 Bytes
/
common.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package social
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/grafana/grafana/pkg/log"
)
type HttpGetResponse struct {
Body []byte
Headers http.Header
}
func isEmailAllowed(email string, allowedDomains []string) bool {
if len(allowedDomains) == 0 {
return true
}
valid := false
for _, domain := range allowedDomains {
emailSuffix := fmt.Sprintf("@%s", domain)
valid = valid || strings.HasSuffix(email, emailSuffix)
}
return valid
}
func HttpGet(client *http.Client, url string) (response HttpGetResponse, err error) {
r, err := client.Get(url)
if err != nil {
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
response = HttpGetResponse{body, r.Header}
if r.StatusCode >= 300 {
err = fmt.Errorf(string(response.Body))
return
}
log.Trace("HTTP GET %s: %s %s", url, r.Status, string(response.Body))
err = nil
return
}