Add relayinfo package and utility modules for NIP-11 support
Introduce the `relayinfo` package with `NIP-11` utilities, including `Fees`, `Limits`, and `NIPs` structures. Add utility modules for handling numbers, timestamps, and kinds. Integrate functionality for fetching and managing relay information.
This commit is contained in:
48
pkg/protocol/relayinfo/fetch.go
Normal file
48
pkg/protocol/relayinfo/fetch.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package relayinfo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"lol.mleku.dev/chk"
|
||||
"lol.mleku.dev/errorf"
|
||||
"next.orly.dev/pkg/utils/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
|
||||
}
|
||||
Reference in New Issue
Block a user