File: request.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 (39 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
package mysqlrouter

import (
	"fmt"
	"io"
	"net/http"
)

func (c *Client) request(url string) ([]byte, error) {
	client := &http.Client{}

	if c.Options != nil {
		if c.Options.Transport != nil {
			client.Transport = c.Options.Transport
		}
	}

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	if c.Username != "" && c.Password != "" {
		req.SetBasicAuth(c.Username, c.Password)
	}

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// We accept InternalServerError because MySQL Router REST API return 500 when route status does not alive. see: https://github.com/rluisr/mysqlrouter_exporter/issues/30#issue-1703518829
	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusInternalServerError {
		return nil, fmt.Errorf("%s got %d", errStatusCode, resp.StatusCode)
	}

	return io.ReadAll(resp.Body)
}