File: client.go

package info (click to toggle)
golang-github-rluisr-mysqlrouter-go 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 264 kB
  • sloc: sh: 215; javascript: 15; makefile: 11
file content (50 lines) | stat: -rw-r--r-- 901 bytes parent folder | download
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
package mysqlrouter

import (
	"errors"
	"net/http"
)

const apiVer = "20190715"

// Client holds the configuration for 20190715 version API client.
type Client struct {
	URL      string
	Username string
	Password string
	Options  *Options
}

type Options struct {
	Transport *http.Transport
}

func newClient(url, user, pass string, Options *Options) *Client {
	return &Client{
		URL:      url + "/api/" + apiVer,
		Username: user,
		Password: pass,
		Options:  Options,
	}
}

// New creates a new API client.
func New(url, user, pass string, Options *Options) (*Client, error) {
	if url == "" {
		return nil, errors.New(errEmptyClientInformation)
	}

	client := newClient(url, user, pass, Options)

	err := client.verifyConnection()
	if err != nil {
		return nil, err
	}

	return client, nil
}

func (c *Client) verifyConnection() error {
	_, err := c.request(c.URL + "/swagger.json")
	return err
}