Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
add methods to allow supplying your own custom http.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Robinson committed Apr 8, 2018
1 parent c871c51 commit 649d3ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rtorrent/rtorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package rtorrent

import (
"fmt"
"net/http"

"github.com/mrobinsn/go-rtorrent/xmlrpc"
"github.com/pkg/errors"
)

// RTorrent is used to communicate with a remote rTorrent instance
type RTorrent struct {
addr string
xmlrpcClient *xmlrpc.Client
}

Expand Down Expand Up @@ -59,10 +61,17 @@ func (f *File) Pretty() string {
// Pass in a true value for `insecure` to turn off certificate verification
func New(addr string, insecure bool) *RTorrent {
return &RTorrent{
addr: addr,
xmlrpcClient: xmlrpc.NewClient(addr, insecure),
}
}

// WithHTTPClient allows you to a provide a custom http.Client.
func (r *RTorrent) WithHTTPClient(client *http.Client) *RTorrent {
r.xmlrpcClient = xmlrpc.NewClientWithHTTPClient(r.addr, client)
return r
}

// Add adds a new torrent by URL
func (r *RTorrent) Add(url string) error {
_, err := r.xmlrpcClient.Call("load_start", url)
Expand Down
9 changes: 9 additions & 0 deletions xmlrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func NewClient(addr string, insecure bool) *Client {
}
}

// NewClientWithHTTPClient returns a new instance of Client.
// This allows you to use a custom http.Client setup for your needs.
func NewClientWithHTTPClient(addr string, client *http.Client) *Client {
return &Client{
addr: addr,
httpClient: client,
}
}

// Call calls the method with "name" with the given args
// Returns the result, and an error for communication errors
func (c *Client) Call(name string, args ...interface{}) (interface{}, error) {
Expand Down

0 comments on commit 649d3ba

Please sign in to comment.