aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: 2dec117edc5b62b15d10205f21b5c57acc8d8edd (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
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
package inwx

import (
	"net"
	"net/http"
	"net/http/cookiejar"
	"sync/atomic"
	"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 {
	httpClient *http.Client
	rpcClient  jsonrpc.RPCClient
	endpoint   Endpoint

	closed  chan struct{}
	closing uint32
	err     error
}

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

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

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

	clt := &Client{
		httpClient: httpClient,
		rpcClient:  rpcClient,
		endpoint:   endpoint,
	}

	// TODO: login

	return clt, nil
}

// Closed returns a channel which is closed when the Client is closed.
func (c *Client) Closed() <-chan struct{} { return c.closed }

// WhyClosed returns the error that caused the Conn to be closed or nil
// if the Conn was closed using the Close method.
// WhyClosed returns nil if the Conn is not closed.
func (c *Client) WhyClosed() error {
	select {
	case <-c.Closed():
		return c.err
	default:
		return nil
	}
}

// Close closes the Client. Any blocked Calls will return net.ErrClosed.
func (c *Client) Close() error {
	return c.closeLogout(nil)
}

func (c *Client) closeLogout(err error) error {
	// TODO: logout

	return c.close(err)
}

func (c *Client) close(err error) error {
	if atomic.SwapUint32(&c.closing, 1) == 1 {
		return net.ErrClosed
	}

	c.err = err
	defer close(c.closed)

	return nil
}