Files
Olares/cli/pkg/wizard/http_sender.go
Eliott van Nuffel df38148149
Some checks failed
Native Verification / Build Docs (pull_request) Successful in 1m12s
Native Verification / Build App-Service Native (pull_request) Successful in 1m27s
Native Verification / Build Daemon Native (pull_request) Successful in 42s
Lint and Test Charts / lint-test (pull_request_target) Failing after 19s
Lint and Test Charts / test-version (pull_request_target) Successful in 0s
Lint and Test Charts / push-image (pull_request_target) Failing after 15s
Lint and Test Charts / upload-cli (pull_request_target) Failing after 1m15s
Lint and Test Charts / upload-daemon (pull_request_target) Failing after 1m12s
Lint and Test Charts / push-deps (pull_request_target) Has been skipped
Lint and Test Charts / push-deps-arm64 (pull_request_target) Has been skipped
Lint and Test Charts / push-image-arm64 (pull_request_target) Has been cancelled
Lint and Test Charts / upload-package (pull_request_target) Has been cancelled
Lint and Test Charts / install-test (pull_request_target) Has been cancelled
continue beOS rebrand and add native verification
2026-03-10 13:48:45 +01:00

77 lines
1.8 KiB
Go

package wizard
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// HTTPSender implements HTTP-based Sender interface
type HTTPSender struct {
BaseURL string
Client *http.Client
}
// NewHTTPSender creates new HTTP Sender
func NewHTTPSender(baseURL string) *HTTPSender {
return &HTTPSender{
BaseURL: baseURL,
Client: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// Send implements Sender interface, sends HTTP request
func (h *HTTPSender) Send(req *Request) (*Response, error) {
// Build request URL
url := fmt.Sprintf("%s/api/rpc", h.BaseURL)
// Serialize request
reqBody, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Create HTTP request
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
// Set request headers
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Accept", "application/json")
// Authentication info already included in JSON request body's auth field, no extra HTTP headers needed
// Send request
httpResp, err := h.Client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("HTTP request failed: %w", err)
}
defer httpResp.Body.Close()
// Read response
respBody, err := io.ReadAll(httpResp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Check HTTP status code
if httpResp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error %d: %s", httpResp.StatusCode, string(respBody))
}
// Parse response
var response Response
if err := json.Unmarshal(respBody, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &response, nil
}