feat: HTTP debug logging in client.do()
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,9 +18,10 @@ type client struct {
|
|||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
baseURL string
|
baseURL string
|
||||||
apiKey string
|
apiKey string
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newClient(endpoint, apiKey string) *client {
|
func newClient(endpoint, apiKey string, debug bool) *client {
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
endpoint = defaultEndpoint
|
endpoint = defaultEndpoint
|
||||||
}
|
}
|
||||||
@@ -27,6 +29,7 @@ func newClient(endpoint, apiKey string) *client {
|
|||||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||||
baseURL: endpoint,
|
baseURL: endpoint,
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
|
debug: debug,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,14 +88,23 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
|||||||
}
|
}
|
||||||
|
|
||||||
var bodyReader io.Reader
|
var bodyReader io.Reader
|
||||||
|
var bodyBytes []byte
|
||||||
if body != nil {
|
if body != nil {
|
||||||
b, err := json.Marshal(body)
|
b, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal request: %w", err)
|
return fmt.Errorf("marshal request: %w", err)
|
||||||
}
|
}
|
||||||
|
bodyBytes = b
|
||||||
bodyReader = bytes.NewReader(b)
|
bodyReader = bytes.NewReader(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.debug {
|
||||||
|
fmt.Fprintf(os.Stderr, "[debug] > %s %s\n", method, u)
|
||||||
|
if bodyBytes != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "[debug] %s\n", bodyBytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create request: %w", err)
|
return fmt.Errorf("create request: %w", err)
|
||||||
@@ -101,6 +113,7 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
|||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("request failed: %w", err)
|
return fmt.Errorf("request failed: %w", err)
|
||||||
@@ -112,6 +125,11 @@ func (c *client) do(ctx context.Context, method, path string, body, result any,
|
|||||||
return fmt.Errorf("read response: %w", err)
|
return fmt.Errorf("read response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.debug {
|
||||||
|
fmt.Fprintf(os.Stderr, "[debug] < %d %s %s\n", resp.StatusCode, http.StatusText(resp.StatusCode), time.Since(start).Round(time.Millisecond))
|
||||||
|
fmt.Fprintf(os.Stderr, "[debug] %s\n", respBody)
|
||||||
|
}
|
||||||
|
|
||||||
var apiResp apiResponse
|
var apiResp apiResponse
|
||||||
if err := json.Unmarshal(respBody, &apiResp); err == nil {
|
if err := json.Unmarshal(respBody, &apiResp); err == nil {
|
||||||
// Rainyun uses code:200 and code:0 both as success indicators
|
// Rainyun uses code:200 and code:0 both as success indicators
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func TestClientAuthHeader(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
c := newClient(srv.URL, "sk-test")
|
c := newClient(srv.URL, "sk-test", false)
|
||||||
var result struct {
|
var result struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -40,7 +40,7 @@ func TestClientAPIError(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
c := newClient(srv.URL, "sk-test")
|
c := newClient(srv.URL, "sk-test", false)
|
||||||
var result any
|
var result any
|
||||||
err := c.get(context.Background(), "/error", &result)
|
err := c.get(context.Background(), "/error", &result)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type Rainyun struct {
|
|||||||
|
|
||||||
// New creates a Rainyun provider from config.
|
// New creates a Rainyun provider from config.
|
||||||
func New(cfg provider.ProviderConfig) (provider.Provider, error) {
|
func New(cfg provider.ProviderConfig) (provider.Provider, error) {
|
||||||
c := newClient(cfg.Endpoint, cfg.APIKey)
|
c := newClient(cfg.Endpoint, cfg.APIKey, cfg.Debug)
|
||||||
r := &Rainyun{client: c}
|
r := &Rainyun{client: c}
|
||||||
r.server = &ServerService{client: c}
|
r.server = &ServerService{client: c}
|
||||||
r.domain = &DomainService{client: c}
|
r.domain = &DomainService{client: c}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func TestServerList(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||||
svc := rain.Server()
|
svc := rain.Server()
|
||||||
servers, err := svc.List(context.Background())
|
servers, err := svc.List(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -66,7 +66,7 @@ func TestServerStart(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||||
err := rain.Server().Start(context.Background(), "12001")
|
err := rain.Server().Start(context.Background(), "12001")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Start() error: %v", err)
|
t.Fatalf("Start() error: %v", err)
|
||||||
@@ -83,7 +83,7 @@ func TestServerVNC(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
rain := &Rainyun{client: newClient(srv.URL, "sk-test")}
|
rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)}
|
||||||
url, err := rain.Server().GetVNC(context.Background(), "12001")
|
url, err := rain.Server().GetVNC(context.Background(), "12001")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetVNC() error: %v", err)
|
t.Fatalf("GetVNC() error: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user