Skip to content

Commit

Permalink
refactor(enginelocate): switch to httpclientx (#1570)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone authored Apr 29, 2024
1 parent 3a4652e commit 419ab2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
15 changes: 7 additions & 8 deletions internal/enginelocate/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"strings"

"github.com/ooni/probe-cli/v3/internal/httpx"
"github.com/ooni/probe-cli/v3/internal/httpclientx"
"github.com/ooni/probe-cli/v3/internal/model"
)

Expand All @@ -18,12 +18,12 @@ func cloudflareIPLookup(
resolver model.Resolver,
) (string, error) {
// get the raw response body
data, err := (&httpx.APIClientTemplate{
BaseURL: "https://www.cloudflare.com",
HTTPClient: httpClient,
Logger: logger,
UserAgent: model.HTTPHeaderUserAgent,
}).WithBodyLogging().Build().FetchResource(ctx, "/cdn-cgi/trace")
data, err := httpclientx.GetRaw(ctx, "https://www.cloudflare.com/cdn-cgi/trace", &httpclientx.Config{
Authorization: "", // not needed
Client: httpClient,
Logger: logger,
UserAgent: userAgent,
})

// handle the error case
if err != nil {
Expand All @@ -33,7 +33,6 @@ func cloudflareIPLookup(
// find the IP addr
r := regexp.MustCompile("(?:ip)=(.*)")
ip := strings.Trim(string(r.Find(data)), "ip=")
logger.Debugf("cloudflare: body: %s", ip)

// make sure the IP addr is valid
if net.ParseIP(ip) == nil {
Expand Down
26 changes: 8 additions & 18 deletions internal/enginelocate/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/xml"
"net"

"github.com/ooni/probe-cli/v3/internal/httpx"
"github.com/ooni/probe-cli/v3/internal/httpclientx"
"github.com/ooni/probe-cli/v3/internal/model"
)

Expand All @@ -21,23 +21,13 @@ func ubuntuIPLookup(
userAgent string,
resolver model.Resolver,
) (string, error) {
// read the HTTP response body
data, err := (&httpx.APIClientTemplate{
BaseURL: "https://geoip.ubuntu.com/",
HTTPClient: httpClient,
Logger: logger,
UserAgent: userAgent,
}).WithBodyLogging().Build().FetchResource(ctx, "/lookup")

// handle the error case
if err != nil {
return model.DefaultProbeIP, err
}

// parse the XML
logger.Debugf("ubuntu: body: %s", string(data))
var v ubuntuResponse
err = xml.Unmarshal(data, &v)
// read the HTTP response and parse as XML
v, err := httpclientx.GetXML[*ubuntuResponse](ctx, "https://geoip.ubuntu.com/lookup", &httpclientx.Config{
Authorization: "", // not needed
Client: httpClient,
Logger: logger,
UserAgent: userAgent,
})

// handle the error case
if err != nil {
Expand Down

0 comments on commit 419ab2a

Please sign in to comment.