1
0
Fork 0
mirror of https://gitlab.com/pnx-tools/dns-updater.git synced 2026-06-16 05:54:56 +02:00

ip/resolver: use ip.ParseIP() helper function

This commit is contained in:
Henrik Hautakoski 2023-12-02 14:19:13 +01:00
parent 2204be7777
commit acf8976f53
3 changed files with 8 additions and 13 deletions

View file

@ -7,6 +7,8 @@ import (
"net"
"net/http"
"strings"
"dnsupdater/ip"
)
type Service struct {
@ -44,9 +46,5 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
// Trim spaces and stuff.
ip_str := strings.TrimSpace(string(body))
ip := net.ParseIP(ip_str)
if ip == nil {
err = fmt.Errorf("Failed to parse ip: %s", ip_str)
}
return ip, err
return ip.ParseIP(ip_str)
}

View file

@ -80,6 +80,6 @@ func TestService_Lookup_ParseError(t *testing.T) {
}
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "Failed to parse ip: random_string")
assert.EqualError(t, err, "invalid IP address: random_string")
assert.Nil(t, ip)
}

View file

@ -5,6 +5,8 @@ import (
"encoding/json"
"net"
"net/http"
"dnsupdater/ip"
)
type Service struct {
@ -22,12 +24,7 @@ func (s Service) Name() string {
}
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
req, err := http.NewRequestWithContext(ctx, "GET", s.url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
resp, err := httphelper.Get(ctx, s.url, nil)
if err != nil {
return nil, err
}
@ -41,5 +38,5 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
return nil, err
}
return net.ParseIP(v.Ip), err
return ip.ParseIP(v.Ip)
}