aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: d8303ab380f953bbd5277233e085e11b22bd245c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// The transmission package provides a Go-native interface for communicating
// with the Transmission RPC interface.
package transmission

import (
	"bytes"
	"encoding/json"
	"io"
	"log/slog"
	"math/rand"
	"net/http"
	"strconv"
)

type CallArguments interface {
	CallTorrentGetArguments | interface{}
}

// The base struct for a Transmission client to perform RPC calls with.
type Client struct {
	Hostname string
	Port     int
	Username string
	Password string

	httpClient http.Client
	sessionId  string
}

// Call a given method on the Transmission RPC interface, and pass it the given arguments in JSON.
func (this *Client) Call(method string, arguments CallArguments, tag uint, response interface{}) error {
	// Create HTTP client to work with
	httpClient := http.Client{}

	// Create the Request object
	req, _ := this.makeRequest(method, arguments, tag)

	slog.Debug("Sending request", "address", req.URL.String(), "session-id", this.sessionId, "tag", tag)
	res, err := httpClient.Do(req)
	if err != nil {
		return err
	}

	// Read the response
	body, err := io.ReadAll(res.Body)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	slog.Debug("Got response", "status", res.StatusCode, "body", body, "tag", tag)

	// Check if we're missing the session-id
	if res.StatusCode == 409 {
		slog.Debug("Got new session-id", "tag", tag)
		this.sessionId = res.Header["X-Transmission-Session-Id"][0]
		return this.Call(method, arguments, tag, response)
	}

	// Unmarshal it into the response
	json.Unmarshal(body, &response)

	// Return the response
	return nil
}

// Convenience function to generate the URL to post RPC calls to.
func (this *Client) connectionString() string {
	url := "http://" + this.Hostname

	if this.Port != 0 {
		url += ":" + strconv.Itoa(this.Port)
	} else {
		url += ":9091"
	}

	url += "/transmission/rpc"

	return url
}

// makeRequest provides a convenience function to make a new http.Request
// object. It sets the URL, headers, and body of the request.
func (this *Client) makeRequest(method string, arguments CallArguments, tag uint) (*http.Request, error) {
	body, _ := json.Marshal(map[string]interface{}{
		"method":    method,
		"arguments": arguments,
		"tag":       tag,
	})

	slog.Debug("Making request", "body", body, "tag", tag)

	req, err := http.NewRequest("POST", this.connectionString(), bytes.NewBuffer([]byte(body)))
	if err != nil {
		return nil, err
	}

	if this.Username != "" {
		slog.Debug("Adding Authorization header", "tag", tag)
		req.SetBasicAuth(this.Username, this.Password)
	}

	req.Header.Add("X-Transmission-Session-Id", this.sessionId)

	return req, nil
}

// Generate a tag ID. Since the maximum value is not documented, this allows
// for easy bug-fixing later in life.
func (this *Client) makeTag() uint {
	return uint(rand.Uint32())
}