From c75771d60bf3dd4dd7e98b2212550354a3154a24 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 9 Jul 2026 18:03:14 -0230 Subject: remove context from methods Set timeout by passing HTTP client to constructor instead. --- xmrpayclnt.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'xmrpayclnt.go') diff --git a/xmrpayclnt.go b/xmrpayclnt.go index 2ecd183..fc2470c 100644 --- a/xmrpayclnt.go +++ b/xmrpayclnt.go @@ -5,7 +5,6 @@ package xmrpayclnt import ( "bytes" - "context" "encoding/json" "log" "net/http" @@ -30,16 +29,22 @@ type Client struct { endpoint *url.URL } -// New creates a client that will connect to the moneropay server at +// New creates a client that will connect to the MoneroPay server at // endpoint. func New(endpoint *url.URL) *Client { - return &Client{jttp.NewClient(&http.Client{}), endpoint} + return WithClient(&http.Client{}, endpoint) +} + +// WithClient creates a MoneroPay client that will use the given HTTP +// client to connect to the MoneroPay server at endpoint. +func WithClient(c *http.Client, endpoint *url.URL) *Client { + return &Client{jttp.NewClient(c), endpoint} } // Balance returns the entire wallet balance. -func (c *Client) Balance(ctx context.Context) (model.BalanceResponse, error) { +func (c *Client) Balance() (model.BalanceResponse, error) { url := c.endpoint.JoinPath(balancePath) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil) + req, err := http.NewRequest(http.MethodGet, url.String(), nil) if err != nil { return model.BalanceResponse{}, err } @@ -49,9 +54,9 @@ func (c *Client) Balance(ctx context.Context) (model.BalanceResponse, error) { } // Health returns true if all required services are up. -func (c *Client) Health(ctx context.Context) (bool, error) { +func (c *Client) Health() (bool, error) { url := c.endpoint.JoinPath(healthPath) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil) + req, err := http.NewRequest(http.MethodGet, url.String(), nil) if err != nil { return false, err } @@ -61,14 +66,14 @@ func (c *Client) Health(ctx context.Context) (bool, error) { } // NewTx creates a subaddress for incoming transfers. -func (c *Client) NewTx(ctx context.Context, amount uint64, description string) (model.ReceivePostResponse, error) { - return c.postReceive(ctx, amount, description, "") +func (c *Client) NewTx(amount uint64, description string) (model.ReceivePostResponse, error) { + return c.postReceive(amount, description, "") } // NewTxWithCallback creates a subaddress and a callback to monitor it. -func (c *Client) NewTxWithCallback(ctx context.Context, amount uint64, description string, ch *CallbackHandler) (model.ReceivePostResponse, Callback, error) { +func (c *Client) NewTxWithCallback(amount uint64, description string, ch *CallbackHandler) (model.ReceivePostResponse, Callback, error) { cb := ch.listen() - if resp, err := c.postReceive(ctx, amount, description, cb.url.String()); err == nil { + if resp, err := c.postReceive(amount, description, cb.url.String()); err == nil { return resp, cb, nil } else { cb.Close() @@ -77,10 +82,10 @@ func (c *Client) NewTxWithCallback(ctx context.Context, amount uint64, descripti } // GetTxStatus retrieves information about a transfer on a subaddress. -func (c *Client) GetTxStatus(ctx context.Context, address string) (model.ReceiveGetResponse, error) { +func (c *Client) GetTxStatus(address string) (model.ReceiveGetResponse, error) { var resp model.ReceiveGetResponse url := c.endpoint.JoinPath(receivePath, address) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil) + req, err := http.NewRequest(http.MethodGet, url.String(), nil) if err != nil { return resp, err } @@ -88,7 +93,7 @@ func (c *Client) GetTxStatus(ctx context.Context, address string) (model.Receive return resp, err } -func (c *Client) postReceive(ctx context.Context, amount uint64, description string, callbackUrl string) (model.ReceivePostResponse, error) { +func (c *Client) postReceive(amount uint64, description string, callbackUrl string) (model.ReceivePostResponse, error) { var resp model.ReceivePostResponse body := new(bytes.Buffer) @@ -99,7 +104,7 @@ func (c *Client) postReceive(ctx context.Context, amount uint64, description str } url := c.endpoint.JoinPath(receivePath) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), body) + req, err := http.NewRequest(http.MethodPost, url.String(), body) if err != nil { return resp, err } -- cgit v1.2.3