46 lines
1000 B
Go
46 lines
1000 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ================== Util ==================
|
|
|
|
func mustMkdirAll(dir string) error { return os.MkdirAll(dir, 0o755) }
|
|
|
|
func sanitize(s string) string {
|
|
return strings.ReplaceAll(strings.ReplaceAll(s, "\n", " "), "\r", " ")
|
|
}
|
|
|
|
func httpGet(ctx context.Context, url string) ([]byte, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("User-Agent", "eth-btc-daemon/1.0")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode/100 != 2 {
|
|
b, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("status %d dal server; body: %s", resp.StatusCode, string(b))
|
|
}
|
|
return io.ReadAll(resp.Body)
|
|
}
|
|
|
|
func httpGetJSON(ctx context.Context, url string, dst any) error {
|
|
b, err := httpGet(ctx, url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.Unmarshal(b, dst)
|
|
}
|