summary refs log tree commit diff stats
path: root/hibp/req.go
blob: a3085dadf360b258e94423f19ceae55215d7666b (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
package hibp

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

func reqHIBP(reqApi string) (string, error) {
	req, err := http.NewRequest(http.MethodGet, reqApi, nil)
	if err != nil {
		return "", fmt.Errorf("request init failed\n%s",
			err.Error())
	}

	c := http.Client{}
	res, err := c.Do(req)
	if err != nil {
		return "", fmt.Errorf("request failed\n%s",
			err.Error())
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return "", fmt.Errorf("Unexpected response status code received: %d %s",
			res.StatusCode, http.StatusText(res.StatusCode))
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", fmt.Errorf("reading res.Body failed\n%s",
			err.Error())
	}
	return string(body), err
}