diff options
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/client.go b/client.go new file mode 100644 index 0000000..ec9b44e --- /dev/null +++ b/client.go @@ -0,0 +1,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 +} |