Files
nostrlib/nipb0/blossom/client.go
fiatjaf 38a6ca92b9 b0: remove hardcoded timeouts in blossom client.
see nostr:nevent1qvzqqqqqqypzqmjxss3dld622uu8q25gywum9qtg4w4cv4064jmg20xsac2aam5nqyd8wumn8ghj7urewfsk66ty9enxjct5dfskvtnrdakj7qpqrfcfcq52gfv6znm8hkdpa3rlxad72zl6ah4haaneqzdxwmnamg3sk2sztg
2025-11-04 08:26:13 -03:00

57 lines
1.3 KiB
Go

package blossom
import (
"strings"
"time"
"fiatjaf.com/nostr"
"github.com/valyala/fasthttp"
)
// Client represents a Blossom client for interacting with a media server
type Client struct {
mediaserver string
httpClient *fasthttp.Client
signer nostr.Signer
}
// NewClient creates a new Blossom client
func NewClient(mediaserver string, signer nostr.Signer) *Client {
if !strings.HasPrefix(mediaserver, "http") {
mediaserver = "https://" + mediaserver
}
return &Client{
mediaserver: strings.TrimSuffix(mediaserver, "/") + "/",
httpClient: createHTTPClient(),
signer: signer,
}
}
// createHTTPClient creates a properly configured HTTP client
func createHTTPClient() *fasthttp.Client {
return &fasthttp.Client{
MaxIdleConnDuration: time.Hour,
DisableHeaderNamesNormalizing: true, // because our headers are properly constructed
DisablePathNormalizing: true,
Name: "nl-b", // user-agent
// increase DNS cache time to an hour instead of default minute
Dial: (&fasthttp.TCPDialer{
Concurrency: 4096,
DNSCacheDuration: time.Hour,
}).Dial,
}
}
// GetSigner returns the client's signer
func (c *Client) GetSigner() nostr.Signer {
return c.signer
}
// GetMediaServer returns the client's media server URL
func (c *Client) GetMediaServer() string {
return c.mediaserver
}