120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
|
package push_client
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/go-logr/logr"
|
||
|
|
||
|
"git.bit5.ru/backend/errors"
|
||
|
)
|
||
|
|
||
|
func httpGet(url string, result any) error {
|
||
|
resp, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "Got error from http.Get. url: %q.", url)
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "HTTP GET response body reading error. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != 200 {
|
||
|
return errors.New(string(body))
|
||
|
}
|
||
|
|
||
|
if err := json.Unmarshal(body, result); err != nil {
|
||
|
return errors.Wrapf(err, "HTTP GET response body parsing error. url: %q.", url)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func httpPost(url string) error {
|
||
|
resp, err := http.Post(url, "application/text", nil)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "Can not send POST. Network error. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "HTTP POST response body reading error. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != 200 {
|
||
|
return errors.New(string(body))
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func httpPostData(logger logr.Logger, url string, data any) (s string, resultErr error) {
|
||
|
|
||
|
defer func() {
|
||
|
if r := recover(); r != nil {
|
||
|
logger.Error(nil, "Can not send POST request", "Panic", r)
|
||
|
resultErr = errors.New("panic in httpPostData")
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
reqBytes, err := json.Marshal(data)
|
||
|
if err != nil {
|
||
|
return "", errors.Wrapf(err, "Can not send POST. Can not convert data to bytes. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(reqBytes))
|
||
|
if err != nil {
|
||
|
return "", errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
req.Header.Set("Content-Type", "application/json")
|
||
|
|
||
|
client := &http.Client{}
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
logger.Info("Got error from client.Do")
|
||
|
return "", errors.Wrapf(err, "Can not send POST. Got error from client.Do(). url: %q.", url)
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", errors.Wrapf(err, "HTTP POST response body reading error. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != 200 {
|
||
|
return "", errors.New(string(body))
|
||
|
}
|
||
|
|
||
|
return string(body), nil
|
||
|
}
|
||
|
|
||
|
func httpPostDataDeprecated(url string, data any) (string, error) {
|
||
|
jsbytes, err := json.Marshal(data)
|
||
|
if err != nil {
|
||
|
return "", errors.Wrapf(err, "Can not send POST. Can not convert data to bytes. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsbytes))
|
||
|
if err != nil {
|
||
|
return "", errors.Wrapf(err, "Can not send POST. Network error. url: %q.", url)
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", errors.Wrapf(err, "HTTP POST response body reading error. url: %q.", url)
|
||
|
}
|
||
|
|
||
|
if resp.StatusCode != 200 {
|
||
|
return "", errors.New(string(body))
|
||
|
}
|
||
|
|
||
|
return string(body), nil
|
||
|
}
|