package basic_http

import (
	"context"
	"io"
	"net"
	"net/http"
	"strings"

	httputils "dnsupdater/http"
	"dnsupdater/ip/internal"
)

type Service struct {
	ServiceName string
	Url         string
	Headers     http.Header
}

func (s Service) Name() string {
	return s.ServiceName
}

func (s Service) Lookup(ctx context.Context) (net.IP, error) {
	resp, err := httputils.Get(ctx, s.Url, s.Headers)
	if err != nil {
		return nil, err
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	// Trim spaces and stuff.
	ip_str := strings.TrimSpace(string(body))

	return internal.ParseIP(ip_str)
}
