package earn
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
func Call(method, path string, body map[string]any) ([]byte, error) {
const baseURL = "https://api.base.org/earn/v1"
var buf io.Reader
if method == http.MethodPost {
const builderCode = "your_registered_builder_code"
payload := make(map[string]any, len(body)+1)
for k, v := range body { payload[k] = v }
payload["builderCode"] = builderCode
body = payload
}
if body != nil {
b, err := json.Marshal(body)
if err != nil { return nil, err }
buf = bytes.NewReader(b)
}
req, err := http.NewRequest(method, baseURL+path, buf)
if err != nil { return nil, err }
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 15*time.Second}
res, err := client.Do(req)
if err != nil { return nil, err }
defer res.Body.Close()
out, err := io.ReadAll(res.Body)
if err != nil { return nil, err }
if res.StatusCode >= 300 { return nil, fmt.Errorf("%d %s", res.StatusCode, out) }
return out, nil
}