Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Apr 29, 2024
1 parent 04b0071 commit 45bd046
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 163 deletions.
19 changes: 14 additions & 5 deletions internal/oonirun/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/ooni/probe-cli/v3/internal/httpclientx"
"github.com/ooni/probe-cli/v3/internal/httpx"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
Expand Down Expand Up @@ -64,12 +64,21 @@ type V2Nettest struct {
// a static URL (e.g., from a GitHub repo or from a Gist).
func getV2DescriptorFromHTTPSURL(ctx context.Context, client model.HTTPClient,
logger model.Logger, URL string) (*V2Descriptor, error) {
return httpclientx.GetJSON[*V2Descriptor](ctx, URL, &httpclientx.Config{
Authorization: "", // not needed
Client: client,
template := httpx.APIClientTemplate{
Accept: "",
Authorization: "",
BaseURL: URL,
HTTPClient: client,
Host: "",
LogBody: true,
Logger: logger,
UserAgent: model.HTTPHeaderUserAgent,
})
}
var desc V2Descriptor
if err := template.Build().GetJSON(ctx, "", &desc); err != nil {
return nil, err
}
return &desc, nil
}

// v2DescriptorCache contains all the known v2Descriptor entries.
Expand Down
4 changes: 2 additions & 2 deletions internal/oonirun/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/ooni/probe-cli/v3/internal/httpclientx"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/mocks"
"github.com/ooni/probe-cli/v3/internal/model"
Expand Down Expand Up @@ -201,7 +200,8 @@ func TestOONIRunV2LinkNilDescriptor(t *testing.T) {
r := NewLinkRunner(config, server.URL)

// make sure we correctly handled an invalid "null" descriptor
if err := r.Run(ctx); !errors.Is(err, httpclientx.ErrIsNil) {
if err := r.Run(ctx); err != nil {
t.Fatal(err)
t.Fatal("unexpected error", err)
}
}
Expand Down
25 changes: 5 additions & 20 deletions internal/probeservices/bouncer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
package probeservices

//
// bouncer.go - GET /api/v1/test-helpers
//

import (
"context"

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

// GetTestHelpers queries the /api/v1/test-helpers API.
func (c *Client) GetTestHelpers(ctx context.Context) (map[string][]model.OOAPIService, error) {
// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/test-helpers", "")
if err != nil {
return nil, err
}

// get the response
return httpclientx.GetJSON[map[string][]model.OOAPIService](ctx, URL, &httpclientx.Config{
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
})
// GetTestHelpers is like GetCollectors but for test helpers.
func (c Client) GetTestHelpers(
ctx context.Context) (output map[string][]model.OOAPIService, err error) {
err = c.APIClientTemplate.WithBodyLogging().Build().GetJSON(ctx, "/api/v1/test-helpers", &output)
return
}
45 changes: 8 additions & 37 deletions internal/probeservices/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"reflect"
"sync"

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

var (
Expand Down Expand Up @@ -60,24 +58,10 @@ func (c Client) OpenReport(ctx context.Context, rt model.OOAPIReportTemplate) (R
if rt.Format != model.OOAPIReportDefaultFormat {
return nil, ErrUnsupportedFormat
}

URL, err := urlx.ResolveReference(c.BaseURL, "/report", "")
if err != nil {
var cor model.OOAPICollectorOpenResponse
if err := c.APIClientTemplate.WithBodyLogging().Build().PostJSON(ctx, "/report", rt, &cor); err != nil {
return nil, err
}

cor, err := httpclientx.PostJSON[model.OOAPIReportTemplate, model.OOAPICollectorOpenResponse](
ctx, URL, rt, &httpclientx.Config{
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
},
)

if err != nil {
return nil, err
}

for _, format := range cor.SupportedFormats {
if format == "json" {
return &reportChan{ID: cor.ReportID, client: c, tmpl: rt}, nil
Expand All @@ -99,27 +83,14 @@ func (r reportChan) CanSubmit(m *model.Measurement) bool {
// submitted. Otherwise, we'll set the report ID to the empty
// string, so that you know which measurements weren't submitted.
func (r reportChan) SubmitMeasurement(ctx context.Context, m *model.Measurement) error {
var updateResponse model.OOAPICollectorUpdateResponse
m.ReportID = r.ID

URL, err := urlx.ResolveReference(r.client.BaseURL, fmt.Sprintf("/report/%s", r.ID), "")
if err != nil {
return err
}

apiReq := model.OOAPICollectorUpdateRequest{
Format: "json",
Content: m,
}

updateResponse, err := httpclientx.PostJSON[
model.OOAPICollectorUpdateRequest, model.OOAPICollectorUpdateResponse](
ctx, URL, apiReq, &httpclientx.Config{
Client: r.client.HTTPClient,
Logger: r.client.Logger,
UserAgent: r.client.UserAgent,
},
err := r.client.APIClientTemplate.WithBodyLogging().Build().PostJSON(
ctx, fmt.Sprintf("/report/%s", r.ID), model.OOAPICollectorUpdateRequest{
Format: "json",
Content: m,
}, &updateResponse,
)

if err != nil {
m.ReportID = ""
return err
Expand Down
21 changes: 3 additions & 18 deletions internal/probeservices/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package probeservices
import (
"context"

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

// MaybeLogin performs login if necessary
Expand All @@ -19,24 +17,11 @@ func (c Client) MaybeLogin(ctx context.Context) error {
return ErrNotRegistered
}
c.LoginCalls.Add(1)

URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/login", "")
if err != nil {
return err
}

auth, err := httpclientx.PostJSON[*model.OOAPILoginCredentials, model.OOAPILoginAuth](
ctx, URL, creds, &httpclientx.Config{
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
},
)

if err != nil {
var auth model.OOAPILoginAuth
if err := c.APIClientTemplate.Build().PostJSON(
ctx, "/api/v1/login", *creds, &auth); err != nil {
return err
}

state.Expire = auth.Expire
state.Token = auth.Token
return c.StateFile.Set(state)
Expand Down
26 changes: 9 additions & 17 deletions internal/probeservices/measurementmeta.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package probeservices

//
// measurementmeta.go - GET /api/v1/measurement_meta
//

import (
"context"
"net/url"

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

// GetMeasurementMeta returns meta information about a measurement.
func (c Client) GetMeasurementMeta(
ctx context.Context, config model.OOAPIMeasurementMetaConfig) (*model.OOAPIMeasurementMeta, error) {
// construct the query to use
query := url.Values{}
query.Add("report_id", config.ReportID)
if config.Input != "" {
Expand All @@ -25,17 +19,15 @@ func (c Client) GetMeasurementMeta(
if config.Full {
query.Add("full", "true")
}

// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/measurement_meta", query.Encode())
var response model.OOAPIMeasurementMeta
err := (&httpx.APIClientTemplate{
BaseURL: c.BaseURL,
HTTPClient: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
}).WithBodyLogging().Build().GetJSONWithQuery(ctx, "/api/v1/measurement_meta", query, &response)
if err != nil {
return nil, err
}

// get the response
return httpclientx.GetJSON[*model.OOAPIMeasurementMeta](ctx, URL, &httpclientx.Config{
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
})
return &response, nil
}
22 changes: 2 additions & 20 deletions internal/probeservices/psiphon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,15 @@ package probeservices
import (
"context"
"fmt"

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

// FetchPsiphonConfig fetches psiphon config from authenticated OONI orchestra.
func (c Client) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
// get credentials and authentication token
_, auth, err := c.GetCredsAndAuth()
if err != nil {
return nil, err
}

// format Authorization header value
s := fmt.Sprintf("Bearer %s", auth.Token)

// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/test-list/psiphon-config", "")
if err != nil {
return nil, err
}

// get response
return httpclientx.GetRaw(ctx, URL, &httpclientx.Config{
Authorization: s,
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
})
client := c.APIClientTemplate.BuildWithAuthorization(s)
return client.FetchResource(ctx, "/api/v1/test-list/psiphon-config")
}
25 changes: 3 additions & 22 deletions internal/probeservices/register.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package probeservices

//
// register.go - POST /api/v1/register
//

import (
"context"

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

// MaybeRegister registers this client if not already registered
Expand All @@ -30,24 +24,11 @@ func (c Client) MaybeRegister(ctx context.Context, metadata model.OOAPIProbeMeta
OOAPIProbeMetadata: metadata,
Password: pwd,
}

// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/register", "")
if err != nil {
return err
}

resp, err := httpclientx.PostJSON[*model.OOAPIRegisterRequest, *model.OOAPIRegisterResponse](
ctx, URL, req, &httpclientx.Config{
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
},
)
if err != nil {
var resp model.OOAPIRegisterResponse
if err := c.APIClientTemplate.Build().PostJSON(
ctx, "/api/v1/register", req, &resp); err != nil {
return err
}

state.ClientID = resp.ClientID
state.Password = pwd
return c.StateFile.Set(state)
Expand Down
27 changes: 5 additions & 22 deletions internal/probeservices/tor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,20 @@ import (
"fmt"
"net/url"

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

// FetchTorTargets returns the targets for the tor experiment.
func (c Client) FetchTorTargets(ctx context.Context, cc string) (map[string]model.OOAPITorTarget, error) {
// get credentials and authentication token
func (c Client) FetchTorTargets(ctx context.Context, cc string) (result map[string]model.OOAPITorTarget, err error) {
_, auth, err := c.GetCredsAndAuth()
if err != nil {
return nil, err
}

// format Authorization header value
s := fmt.Sprintf("Bearer %s", auth.Token)

// create query string
client := c.APIClientTemplate.BuildWithAuthorization(s)
query := url.Values{}
query.Add("country_code", cc)

// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/test-list/tor-targets", query.Encode())
if err != nil {
return nil, err
}

// get response
return httpclientx.GetJSON[map[string]model.OOAPITorTarget](ctx, URL, &httpclientx.Config{
Authorization: s,
Client: c.HTTPClient,
Logger: c.Logger,
UserAgent: c.UserAgent,
})
err = client.GetJSONWithQuery(
ctx, "/api/v1/test-list/tor-targets", query, &result)
return
}

0 comments on commit 45bd046

Please sign in to comment.