snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
164
server/pkg/http/client.go
Normal file
164
server/pkg/http/client.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"errors"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
BaseURL string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
Client struct {
|
||||
Transport *http.Transport
|
||||
Client *http.Client
|
||||
|
||||
debugLevel DebugLevel
|
||||
config *Config
|
||||
}
|
||||
|
||||
DebugLevel string
|
||||
)
|
||||
|
||||
const (
|
||||
INFO DebugLevel = "info"
|
||||
FULL DebugLevel = "full"
|
||||
)
|
||||
|
||||
func New(flags *Config) (*Client, error) {
|
||||
timeout := time.Duration(flags.Timeout) * time.Second
|
||||
|
||||
transport := &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: timeout,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: timeout,
|
||||
}
|
||||
|
||||
// @todo migrate to http.DefaultClient & http.DefaultTransport, see internal/http.SetupDefaults
|
||||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Transport: transport,
|
||||
Client: client,
|
||||
config: flags,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Debug(level DebugLevel) *Client {
|
||||
c.debugLevel = level
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Get(url string) (*http.Request, error) {
|
||||
return c.Request("GET", url, nil)
|
||||
}
|
||||
|
||||
func (c *Client) Post(url string, body interface{}) (*http.Request, error) {
|
||||
return c.Request("POST", url, body)
|
||||
}
|
||||
|
||||
func (c *Client) Patch(url string, body interface{}) (*http.Request, error) {
|
||||
return c.Request("PATCH", url, body)
|
||||
}
|
||||
|
||||
func (c *Client) Delete(url string) (*http.Request, error) {
|
||||
return c.Request("DELETE", url, nil)
|
||||
}
|
||||
|
||||
func (c *Client) Request(method, url string, body interface{}) (*http.Request, error) {
|
||||
if c.config.BaseURL != "" {
|
||||
url = strings.TrimRight(c.config.BaseURL, "/") + "/" + strings.TrimLeft(url, "/")
|
||||
}
|
||||
|
||||
if c.debugLevel == "info" {
|
||||
fmt.Println("HTTP >>>", method, url)
|
||||
}
|
||||
|
||||
request := func() (*http.Request, error) {
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return http.NewRequest(method, url, bytes.NewBuffer(b))
|
||||
}
|
||||
return http.NewRequest(method, url, nil)
|
||||
}
|
||||
|
||||
req, err := request()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request failed: %w", err)
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
if c.debugLevel == FULL {
|
||||
fmt.Println("HTTP >>> (request)")
|
||||
b, err := httputil.DumpRequestOut(req, true)
|
||||
if err != nil {
|
||||
fmt.Println("HTTP >>> Error:", err)
|
||||
} else {
|
||||
if b != nil {
|
||||
fmt.Println(strings.TrimSpace(string(b)))
|
||||
}
|
||||
}
|
||||
fmt.Println("---")
|
||||
}
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if c.debugLevel == FULL {
|
||||
fmt.Println("HTTP <<< (response)")
|
||||
if err != nil {
|
||||
fmt.Println("HTTP <<< Error:", err)
|
||||
} else {
|
||||
|
||||
b, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
fmt.Println("HTTP <<< Error:", err)
|
||||
} else {
|
||||
if b != nil {
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("-----------------")
|
||||
}
|
||||
if err != nil {
|
||||
if c.debugLevel == INFO {
|
||||
fmt.Println("HTTP <<< Response error", err)
|
||||
}
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
if c.debugLevel == INFO {
|
||||
fmt.Println("HTTP <<< Response", resp.StatusCode)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func ToError(resp *http.Response) error {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if body == nil || err != nil {
|
||||
return fmt.Errorf("unexpected response (%d, %s)", resp.StatusCode, err)
|
||||
}
|
||||
return errors.New(string(body))
|
||||
}
|
||||
Reference in New Issue
Block a user