Skip to content

Commit

Permalink
feat: export routing/http/client.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 4, 2023
1 parent 5ae64f3 commit 7d7e902
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ The following emojis are used to highlight certain changes:
* 🛠 The `IPFSBackend` interface was updated to make the responses of the
`Head` method more explicit. It now returns a `HeadResponse` instead of a
`files.Node`.
* `boxo/routing/http/client.Client` is now exported. This means you can now pass
it around functions, or add it to a struct if you want.

### Removed

Expand Down
34 changes: 17 additions & 17 deletions routing/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
_ contentrouter.Client = &client{}
_ contentrouter.Client = &Client{}
logger = logging.Logger("routing/http/client")
defaultHTTPClient = &http.Client{
Transport: &ResponseBodyLimitedTransport{
Expand All @@ -45,7 +45,7 @@ const (
mediaTypeIPNSRecord = "application/vnd.ipfs.ipns-record"
)

type client struct {
type Client struct {
baseURL string
httpClient httpClient
clock clock.Clock
Expand All @@ -65,28 +65,28 @@ type client struct {
// version sent a request
var defaultUserAgent = moduleVersion()

var _ contentrouter.Client = &client{}
var _ contentrouter.Client = &Client{}

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

type Option func(*client)
type Option func(*Client)

func WithIdentity(identity crypto.PrivKey) Option {
return func(c *client) {
return func(c *Client) {
c.identity = identity
}
}

func WithHTTPClient(h httpClient) Option {
return func(c *client) {
return func(c *Client) {
c.httpClient = h
}
}

func WithUserAgent(ua string) Option {
return func(c *client) {
return func(c *Client) {
if ua == "" {
return
}
Expand All @@ -103,7 +103,7 @@ func WithUserAgent(ua string) Option {
}

func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr) Option {
return func(c *client) {
return func(c *Client) {
c.peerID = peerID
for _, a := range addrs {
c.addrs = append(c.addrs, types.Multiaddr{Multiaddr: a})
Expand All @@ -112,15 +112,15 @@ func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr) Option {
}

func WithStreamResultsRequired() Option {
return func(c *client) {
return func(c *Client) {
c.accepts = mediaTypeNDJSON
}
}

// New creates a content routing API client.
// The Provider and identity parameters are option. If they are nil, the [client.ProvideBitswap] method will not function.
func New(baseURL string, opts ...Option) (*client, error) {
client := &client{
func New(baseURL string, opts ...Option) (*Client, error) {
client := &Client{
baseURL: baseURL,
httpClient: defaultHTTPClient,
clock: clock.New(),
Expand Down Expand Up @@ -160,7 +160,7 @@ func (c *measuringIter[T]) Close() error {
return c.Iter.Close()
}

func (c *client) FindProviders(ctx context.Context, key cid.Cid) (providers iter.ResultIter[types.Record], err error) {
func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter.ResultIter[types.Record], err error) {
// TODO test measurements
m := newMeasurement("FindProviders")

Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
// Deprecated: protocol-agnostic provide is being worked on in [IPIP-378]:
//
// [IPIP-378]: https://github.com/ipfs/specs/pull/378
func (c *client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error) {
func (c *Client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error) {
if c.identity == nil {
return 0, errors.New("cannot provide Bitswap records without an identity")
}
Expand Down Expand Up @@ -283,7 +283,7 @@ func (c *client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Du
// ProvideAsync makes a provide request to a delegated router
//
//lint:ignore SA1019 // ignore staticcheck
func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *types.WriteBitswapRecord) (time.Duration, error) {
func (c *Client) provideSignedBitswapRecord(ctx context.Context, bswp *types.WriteBitswapRecord) (time.Duration, error) {
//lint:ignore SA1019 // ignore staticcheck
req := jsontypes.WriteProvidersRequest{Providers: []types.Record{bswp}}

Expand Down Expand Up @@ -332,7 +332,7 @@ func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *types.Wri
return 0, nil
}

func (c *client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultIter[types.Record], err error) {
func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultIter[types.Record], err error) {
m := newMeasurement("FindPeers")

url := c.baseURL + "/routing/v1/peers/" + peer.ToCid(pid).String()
Expand Down Expand Up @@ -405,7 +405,7 @@ func (c *client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
return &measuringIter[iter.Result[types.Record]]{Iter: it, ctx: ctx, m: m}, nil
}

func (c *client) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
func (c *Client) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
url := c.baseURL + "/routing/v1/ipns/" + name.String()

httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
Expand Down Expand Up @@ -443,7 +443,7 @@ func (c *client) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, err
return record, nil
}

func (c *client) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error {
func (c *Client) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error {
url := c.baseURL + "/routing/v1/ipns/" + name.String()

rawRecord, err := ipns.MarshalRecord(record)
Expand Down
2 changes: 1 addition & 1 deletion routing/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type testDeps struct {
server *httptest.Server
peerID peer.ID
addrs []multiaddr.Multiaddr
client *client
client *Client
}

type recordingHandler struct {
Expand Down

0 comments on commit 7d7e902

Please sign in to comment.