aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: ec9b44e2e4c53802eeb09dfe43712b44a1f98825 (plain) (blame)
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
package inwx

import (
	"net/http"
	"net/http/cookiejar"
	"time"

	"github.com/ybbus/jsonrpc/v3"
)

// An Endpoint is the server to communicate with.
type Endpoint string

const (
	Production Endpoint = "https://api.domrobot.com/jsonrpc/"
	Sandbox             = "https://api.ote.domrobot.com/jsonrpc/"
)

const requestTimeout = 30 * time.Second

// A Client is a session handle for the API.
type Client struct {
	rpcClient jsonrpc.RPCClient
	endpoint  Endpoint
}

func NewClient(endpoint Endpoint) (*Client, error) {
	jar, err := cookiejar.New(&cookiejar.Options{})
	if err != nil {
		return nil, err
	}

	httpClient := &http.Client{
		Jar:     jar,
		Timeout: requestTimeout,
	}

	clt := &Client{
		rpcClient: jsonrpc.NewClientWithOpts(string(endpoint), &jsonrpc.RPCClientOpts{
			HTTPClient: httpClient,
		}),
		endpoint: endpoint,
	}

	return clt, nil
}