diff --git a/httpclient.go b/httpclient.go new file mode 100644 index 0000000..9bec559 --- /dev/null +++ b/httpclient.go @@ -0,0 +1,67 @@ +package main + +import ( + "crypto/tls" + "net/http" + "net/url" + "strings" + + log "github.com/sirupsen/logrus" +) + +func createHttpClient() (*http.Client, error) { + httpClient := new(http.Client) + + if config.HttpProxyAddr != "" { + proxyURL, err := url.Parse(config.HttpProxyAddr) + if err != nil { + return nil, err + } + + httpClient.Transport = &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} + } + + return httpClient, nil +} + +func httpPost(addr string, text string) error { + httpClient, err := createHttpClient() + if err != nil { + return err + } + + resp, err := httpClient.Post(addr, "text/plain", strings.NewReader(text)) + defer resp.Body.Close() // TODO: нужно ли закрывать Body при наличии ошибки? + if err != nil { + return err + } + + return nil // TODO: вернуть ошибку Post при наличии +} + +func httpGet(addrFmt, jobName, text string) error { + httpClient, err := createHttpClient() + if err != nil { + return err + } + + v := struct { + JobName string + Error string + }{ + JobName: url.QueryEscape(jobName), + Error: url.QueryEscape(text)} + + urlStr := format(addrFmt, v) + log.Println(urlStr) + + resp, err := httpClient.Get(urlStr) + if err != nil { + return err + } + defer resp.Body.Close() // TODO: нужно ли закрывать Body при наличии ошибки? + + return nil +}