refactor(httpx): improve and modernize (1/n) (#647)

This PR starts to implement the refactoring described at https://github.com/ooni/probe/issues/1951. I originally wrote more patches than the ones in this PR, but overall they were not readable. Since I want to squash and merge, here's a reasonable subset of the original patches that will still be readable and understandable in the future.
This commit is contained in:
Simone Basso
2022-01-05 12:48:32 +01:00
committed by GitHub
parent 0a630c1716
commit 7b7df2c6af
25 changed files with 173 additions and 294 deletions
+1 -1
View File
@@ -9,6 +9,6 @@ import (
// GetTestHelpers is like GetCollectors but for test helpers.
func (c Client) GetTestHelpers(
ctx context.Context) (output map[string][]model.OOAPIService, err error) {
err = c.Client.GetJSON(ctx, "/api/v1/test-helpers", &output)
err = c.APIClient.GetJSON(ctx, "/api/v1/test-helpers", &output)
return
}
+1 -1
View File
@@ -16,7 +16,7 @@ type checkInResult struct {
// Returns the list of tests to run and the URLs, on success, or an explanatory error, in case of failure.
func (c Client) CheckIn(ctx context.Context, config model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
var response checkInResult
if err := c.Client.PostJSON(ctx, "/api/v1/check-in", config, &response); err != nil {
if err := c.APIClient.PostJSON(ctx, "/api/v1/check-in", config, &response); err != nil {
return nil, err
}
return &response.Tests, nil
@@ -16,7 +16,7 @@ func (c Client) CheckReportID(ctx context.Context, reportID string) (bool, error
query := url.Values{}
query.Add("report_id", reportID)
var response checkReportIDResponse
err := (httpx.Client{
err := (&httpx.APIClient{
BaseURL: c.BaseURL,
HTTPClient: c.HTTPClient,
Logger: c.Logger,
@@ -15,7 +15,7 @@ import (
func TestCheckReportIDWorkingAsIntended(t *testing.T) {
client := probeservices.Client{
Client: httpx.Client{
APIClient: httpx.APIClient{
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Logger: log.Log,
@@ -38,7 +38,7 @@ func TestCheckReportIDWorkingAsIntended(t *testing.T) {
func TestCheckReportIDWorkingWithCancelledContext(t *testing.T) {
client := probeservices.Client{
Client: httpx.Client{
APIClient: httpx.APIClient{
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Logger: log.Log,
+2 -2
View File
@@ -105,7 +105,7 @@ func (c Client) OpenReport(ctx context.Context, rt ReportTemplate) (ReportChanne
return nil, ErrUnsupportedFormat
}
var cor collectorOpenResponse
if err := c.Client.PostJSON(ctx, "/report", rt, &cor); err != nil {
if err := c.APIClient.PostJSON(ctx, "/report", rt, &cor); err != nil {
return nil, err
}
for _, format := range cor.SupportedFormats {
@@ -144,7 +144,7 @@ func (r reportChan) CanSubmit(m *model.Measurement) bool {
func (r reportChan) SubmitMeasurement(ctx context.Context, m *model.Measurement) error {
var updateResponse collectorUpdateResponse
m.ReportID = r.ID
err := r.client.Client.PostJSON(
err := r.client.APIClient.PostJSON(
ctx, fmt.Sprintf("/report/%s", r.ID), collectorUpdateRequest{
Format: "json",
Content: m,
+1 -1
View File
@@ -29,7 +29,7 @@ func (c Client) MaybeLogin(ctx context.Context) error {
}
c.LoginCalls.Add(1)
var auth LoginAuth
if err := c.Client.PostJSON(ctx, "/api/v1/login", *creds, &auth); err != nil {
if err := c.APIClient.PostJSON(ctx, "/api/v1/login", *creds, &auth); err != nil {
return err
}
state.Expire = auth.Expire
@@ -54,7 +54,7 @@ func (c Client) GetMeasurementMeta(
query.Add("full", "true")
}
var response MeasurementMeta
err := (httpx.Client{
err := (&httpx.APIClient{
BaseURL: c.BaseURL,
HTTPClient: c.HTTPClient,
Logger: c.Logger,
@@ -16,7 +16,7 @@ import (
func TestGetMeasurementMetaWorkingAsIntended(t *testing.T) {
client := probeservices.Client{
Client: httpx.Client{
APIClient: httpx.APIClient{
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Logger: log.Log,
@@ -84,7 +84,7 @@ func TestGetMeasurementMetaWorkingAsIntended(t *testing.T) {
func TestGetMeasurementMetaWorkingWithCancelledContext(t *testing.T) {
client := probeservices.Client{
Client: httpx.Client{
APIClient: httpx.APIClient{
BaseURL: "https://ams-pg.ooni.org/",
HTTPClient: http.DefaultClient,
Logger: log.Log,
@@ -65,7 +65,7 @@ type Session interface {
// Client is a client for the OONI probe services API.
type Client struct {
httpx.Client
httpx.APIClient
LoginCalls *atomicx.Int64
RegisterCalls *atomicx.Int64
StateFile StateFile
@@ -91,7 +91,7 @@ func (c Client) GetCredsAndAuth() (*LoginCredentials, *LoginAuth, error) {
// function fails, e.g., we don't support the specified endpoint.
func NewClient(sess Session, endpoint model.OOAPIService) (*Client, error) {
client := &Client{
Client: httpx.Client{
APIClient: httpx.APIClient{
BaseURL: endpoint.Address,
HTTPClient: sess.DefaultHTTPClient(),
Logger: sess.Logger(),
@@ -115,7 +115,7 @@ func NewClient(sess Session, endpoint model.OOAPIService) (*Client, error) {
if URL.Scheme != "https" || URL.Host != URL.Hostname() {
return nil, ErrUnsupportedCloudFrontAddress
}
client.Client.Host = URL.Hostname()
client.APIClient.Host = URL.Hostname()
URL.Host = endpoint.Front
client.BaseURL = URL.String()
if _, err := url.Parse(client.BaseURL); err != nil {
+3 -1
View File
@@ -11,7 +11,9 @@ func (c Client) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
if err != nil {
return nil, err
}
client := c.Client
// Note: the following code is very bad: it copies the original
// API client and then overrides one of its fields. Bleah...
client := c.APIClient
client.Authorization = fmt.Sprintf("Bearer %s", auth.Token)
return client.FetchResource(ctx, "/api/v1/test-list/psiphon-config")
}
+1 -1
View File
@@ -33,7 +33,7 @@ func (c Client) MaybeRegister(ctx context.Context, metadata Metadata) error {
Password: pwd,
}
var resp registerResult
if err := c.Client.PostJSON(ctx, "/api/v1/register", req, &resp); err != nil {
if err := c.APIClient.PostJSON(ctx, "/api/v1/register", req, &resp); err != nil {
return err
}
state.ClientID = resp.ClientID
+3 -1
View File
@@ -14,7 +14,9 @@ func (c Client) FetchTorTargets(ctx context.Context, cc string) (result map[stri
if err != nil {
return nil, err
}
client := c.Client
// Note: the following code is very bad: it copies the original
// API client and then overrides one of its fields. Bleah...
client := c.APIClient
client.Authorization = fmt.Sprintf("Bearer %s", auth.Token)
query := url.Values{}
query.Add("country_code", cc)
+1 -1
View File
@@ -61,7 +61,7 @@ func (clnt *FetchTorTargetsHTTPTransport) RoundTrip(req *http.Request) (*http.Re
func TestFetchTorTargetsSetsQueryString(t *testing.T) {
clnt := newclient()
txp := new(FetchTorTargetsHTTPTransport)
clnt.HTTPClient.Transport = txp
clnt.HTTPClient = &http.Client{Transport: txp}
if err := clnt.MaybeRegister(context.Background(), testorchestra.MetadataFixture()); err != nil {
t.Fatal(err)
}
+1 -1
View File
@@ -28,7 +28,7 @@ func (c Client) FetchURLList(ctx context.Context, config model.OOAPIURLListConfi
query.Set("category_codes", strings.Join(config.Categories, ","))
}
var response urlListResult
err := c.Client.GetJSONWithQuery(ctx, "/api/v1/test-list/urls", query, &response)
err := c.APIClient.GetJSONWithQuery(ctx, "/api/v1/test-list/urls", query, &response)
if err != nil {
return nil, err
}