grafana/pkg/social/common.go
Dave Hall 0c70d271dc Support large github organisations (#8846)
* Add new HttpGetResponse struct type
* Modify HttpGet() return to use HttpGetResponse
* Look up _all_ the teams the user is a member of
2017-07-31 12:13:29 +02:00

56 lines
926 B
Go

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
}