You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1020 B
48 lines
1020 B
package relayinfo |
|
|
|
import ( |
|
"context" |
|
"encoding/json" |
|
"io" |
|
"net/http" |
|
"time" |
|
|
|
"lol.mleku.dev/chk" |
|
"lol.mleku.dev/errorf" |
|
"utils.orly/normalize" |
|
) |
|
|
|
// Fetch fetches the NIP-11 Info. |
|
func Fetch(c context.Context, u []byte) (info *T, err error) { |
|
if _, ok := c.Deadline(); !ok { |
|
// if no timeout is set, force it to 7 seconds |
|
var cancel context.CancelFunc |
|
c, cancel = context.WithTimeout(c, 7*time.Second) |
|
defer cancel() |
|
} |
|
u = normalize.URL(u) |
|
var req *http.Request |
|
if req, err = http.NewRequestWithContext( |
|
c, http.MethodGet, string(u), nil, |
|
); chk.E(err) { |
|
return |
|
} |
|
// add the NIP-11 header |
|
req.Header.Add("Accept", "application/nostr+json") |
|
// send the response |
|
var resp *http.Response |
|
if resp, err = http.DefaultClient.Do(req); chk.E(err) { |
|
err = errorf.E("request failed: %w", err) |
|
return |
|
} |
|
defer chk.E(resp.Body.Close()) |
|
var b []byte |
|
if b, err = io.ReadAll(resp.Body); chk.E(err) { |
|
return |
|
} |
|
info = &T{} |
|
if err = json.Unmarshal(b, info); chk.E(err) { |
|
return |
|
} |
|
return |
|
}
|
|
|