-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
75 lines (60 loc) · 1.5 KB
/
get.go
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
package nodeinfo
import (
"io"
"net/http"
gourl "net/url"
"github.com/reiver/go-erorr"
)
func GetHost(host string) (WellKnown, error) {
var url gourl.URL
url.Scheme = "https"
url.Host = host
url.Path = DefaultPath
return getURL(&url)
}
func GetURL(url string) (WellKnown, error) {
urloc, err := gourl.Parse(url)
if nil != err {
var nada WellKnown
return nada, erorr.Errorf("nodeinfo: problem parsing URL %q: %w", url, err)
}
return getURL(urloc)
}
func getURL(url *gourl.URL) (WellKnown, error) {
var httprequest http.Request
{
httprequest.Method = http.MethodGet
httprequest.URL = url
if nil == httprequest.Header {
httprequest.Header = http.Header{}
}
httprequest.Header.Add("User-Agent", UserAgent)
}
var httpresponse *http.Response
{
var err error
httpresponse, err = http.DefaultClient.Do(&httprequest)
if nil != err {
var nada WellKnown
return nada, erorr.Errorf("nodeinfo: problem making HTTP-request to %q: %w", url.String(), err)
}
}
var bytes []byte
{
var err error
bytes, err = io.ReadAll(httpresponse.Body)
if nil != err {
var nada WellKnown
return nada, erorr.Errorf("nodeinfo: problem get data from body of HTTP-response from %q: %w", url.String(), err)
}
}
var wellknown WellKnown
{
err := wellknown.UnmarshalJSON(bytes)
if nil != err {
var nada WellKnown
return nada, erorr.Errorf("nodeinfo: problem json-unmarshaling the data from the body of the HTTP-response from %q: %w", url.String(), err)
}
}
return wellknown, nil
}