1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/jenkins/client.go
skymeyer 39c3daee23 Honor system http proxies when using non-default transport
This implements the same proxy behavior as per http.DefaultTransport
using HTTP proxies as directed by the $HTTP_PROXY and $NO_PROXY (or
$http_proxy and $no_proxy) environment variables.
2019-01-19 10:46:08 +00:00

74 lines
1.5 KiB
Go

package jenkins
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/wtfutil/wtf/wtf"
)
func Create(jenkinsURL string, username string, apiKey string) (*View, error) {
const apiSuffix = "api/json?pretty=true"
parsedSuffix, err := url.Parse(apiSuffix)
if err != nil {
return &View{}, err
}
parsedJenkinsURL, err := url.Parse(ensureLastSlash(jenkinsURL))
if err != nil {
return &View{}, err
}
jenkinsAPIURL := parsedJenkinsURL.ResolveReference(parsedSuffix)
req, _ := http.NewRequest("GET", jenkinsAPIURL.String(), nil)
req.SetBasicAuth(username, apiKey)
verifyServerCertificate := wtf.Config.UBool("wtf.mods.jenkins.verifyServerCertificate", true)
httpClient := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !verifyServerCertificate,
},
Proxy: http.ProxyFromEnvironment,
},
}
resp, err := httpClient.Do(req)
if err != nil {
return &View{}, err
}
view := &View{}
parseJson(view, resp.Body)
return view, nil
}
func ensureLastSlash(URL string) string {
return strings.TrimRight(URL, "/") + "/"
}
/* -------------------- Unexported Functions -------------------- */
func parseJson(obj interface{}, text io.Reader) {
jsonStream, err := ioutil.ReadAll(text)
if err != nil {
panic(err)
}
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
for {
if err := decoder.Decode(obj); err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
}