mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-12 12:32:32 +02:00
nip11: always return a struct from Fetch() with URL filled.
This commit is contained in:
@ -10,8 +10,12 @@ import (
|
|||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Fetch fetches the NIP-11 RelayInformationDocument.
|
// Fetch fetches the NIP-11 metadata for a relay.
|
||||||
func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err error) {
|
//
|
||||||
|
// It will always return `info` with at least `URL` filled -- even if we can't connect to the
|
||||||
|
// relay or if it doesn't have a NIP-11 handler -- although in that case it will also return
|
||||||
|
// an error.
|
||||||
|
func Fetch(ctx context.Context, u string) (info RelayInformationDocument, err error) {
|
||||||
if _, ok := ctx.Deadline(); !ok {
|
if _, ok := ctx.Deadline(); !ok {
|
||||||
// if no timeout is set, force it to 7 seconds
|
// if no timeout is set, force it to 7 seconds
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
@ -20,10 +24,14 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// normalize URL to start with http://, https:// or without protocol
|
// normalize URL to start with http://, https:// or without protocol
|
||||||
u = "http" + nostr.NormalizeURL(u)[2:]
|
u = nostr.NormalizeURL(u)
|
||||||
|
|
||||||
|
info = RelayInformationDocument{
|
||||||
|
URL: u,
|
||||||
|
}
|
||||||
|
|
||||||
// make request
|
// make request
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
req, err := http.NewRequestWithContext(ctx, "GET", "http"+u[2:], nil)
|
||||||
|
|
||||||
// add the NIP-11 header
|
// add the NIP-11 header
|
||||||
req.Header.Add("Accept", "application/nostr+json")
|
req.Header.Add("Accept", "application/nostr+json")
|
||||||
@ -31,13 +39,12 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
|
|||||||
// send the request
|
// send the request
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return info, fmt.Errorf("request failed: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
info = &RelayInformationDocument{}
|
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
|
||||||
if err := json.NewDecoder(resp.Body).Decode(info); err != nil {
|
return info, fmt.Errorf("invalid json: %w", err)
|
||||||
return nil, fmt.Errorf("invalid json: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
|
@ -3,6 +3,8 @@ package nip11
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAddSupportedNIP(t *testing.T) {
|
func TestAddSupportedNIP(t *testing.T) {
|
||||||
@ -34,15 +36,20 @@ func TestAddSupportedNIP(t *testing.T) {
|
|||||||
|
|
||||||
func TestFetch(t *testing.T) {
|
func TestFetch(t *testing.T) {
|
||||||
res, err := Fetch(context.Background(), "wss://relay.nostr.bg")
|
res, err := Fetch(context.Background(), "wss://relay.nostr.bg")
|
||||||
if err != nil || res.Name == "" {
|
assert.Equal(t, res.URL, "wss://relay.nostr.bg")
|
||||||
t.Errorf("failed to fetch from wss")
|
assert.Nil(t, err, "failed to fetch from wss")
|
||||||
}
|
assert.NotEmpty(t, res.Name)
|
||||||
|
|
||||||
res, err = Fetch(context.Background(), "https://relay.nostr.bg")
|
res, err = Fetch(context.Background(), "https://relay.nostr.bg")
|
||||||
if err != nil || res.Name == "" {
|
assert.Nil(t, err, "failed to fetch from https")
|
||||||
t.Errorf("failed to fetch from https")
|
assert.NotEmpty(t, res.Name)
|
||||||
}
|
|
||||||
res, err = Fetch(context.Background(), "relay.nostr.bg")
|
res, err = Fetch(context.Background(), "relay.nostr.bg")
|
||||||
if err != nil || res.Name == "" {
|
assert.Nil(t, err, "failed to fetch without protocol")
|
||||||
t.Errorf("failed to fetch without protocol")
|
assert.NotEmpty(t, res.Name)
|
||||||
}
|
|
||||||
|
res, err = Fetch(context.Background(), "wlenwqkeqwe.asjdaskd")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.NotNil(t, res)
|
||||||
|
assert.Equal(t, res.URL, "wss://wlenwqkeqwe.asjdaskd")
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package nip11
|
|||||||
import "slices"
|
import "slices"
|
||||||
|
|
||||||
type RelayInformationDocument struct {
|
type RelayInformationDocument struct {
|
||||||
|
URL string `json:"-"`
|
||||||
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
PubKey string `json:"pubkey"`
|
PubKey string `json:"pubkey"`
|
||||||
|
Reference in New Issue
Block a user