Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(probeservices): httpx -> httpclientx (#1576)
This issue replaces httpx with httpclientx for probeservices. Part of ooni/probe#2723. --- Here are some checks to make sure we' not changing API semantics. ## GetTestHelpers Before: ```Go err = c.APIClientTemplate.WithBodyLogging().Build().GetJSON(ctx, "/api/v1/test-helpers", &output) ``` After: ```Go // 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, }) ``` In both cases: uses the same client and user agent, uses the same URL path, uses logging. ## OpenReport Before: ```Go var cor model.OOAPICollectorOpenResponse if err := c.APIClientTemplate.WithBodyLogging().Build().PostJSON(ctx, "/report", rt, &cor); err != nil { ``` After: ```Go URL, err := urlx.ResolveReference(c.BaseURL, "/report", "") if 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, }, ) ``` In both cases: uses the same client and user agent, uses the same URL path, uses logging. ## SubmitMeasurement Before: ```Go err := r.client.APIClientTemplate.WithBodyLogging().Build().PostJSON( ctx, fmt.Sprintf("/report/%s", r.ID), model.OOAPICollectorUpdateRequest{ Format: "json", Content: m, }, &updateResponse, ``` After: ```Go 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, }, ) ``` In both cases: uses the same client and user agent, uses the same URL path, uses logging. ## Login Before: ```Go var auth model.OOAPILoginAuth if err := c.APIClientTemplate.Build().PostJSON( ctx, "/api/v1/login", *creds, &auth); err != nil { ``` After: ```Go 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: model.DiscardLogger, UserAgent: c.UserAgent, }, ) ``` In both cases: uses the same client and user agent, uses the same URL path, and we're not using logging. ## MeasurementMeta Before: ```Go 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) ``` After: ```Go // construct the URL to use URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/measurement_meta", query.Encode()) if err != nil { return nil, err } return &response, nil // get the response return httpclientx.GetJSON[*model.OOAPIMeasurementMeta](ctx, URL, &httpclientx.Config{ Client: c.HTTPClient, Logger: c.Logger, UserAgent: c.UserAgent, }) ``` In both cases: uses the same client and user agent, uses the same URL path, and logging. ## Psiphon Before: ```Go client := c.APIClientTemplate.BuildWithAuthorization(s) return client.FetchResource(ctx, "/api/v1/test-list/psiphon-config") ``` After: ```Go // 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 // // use a model.DiscardLogger to avoid logging config return httpclientx.GetRaw(ctx, URL, &httpclientx.Config{ Authorization: s, Client: c.HTTPClient, Logger: model.DiscardLogger, UserAgent: c.UserAgent, }) ``` In both cases: uses the same client and user agent, uses the same URL path, and we're not logging. ## Register Before: ```Go var resp model.OOAPIRegisterResponse if err := c.APIClientTemplate.Build().PostJSON( ctx, "/api/v1/register", req, &resp); err != nil { ``` After: ```Go // 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: model.DiscardLogger, UserAgent: c.UserAgent, }, ) ``` In both cases: uses the same client and user agent, uses the same URL path, and we're not logging. ## Tor Before: ```Go client := c.APIClientTemplate.BuildWithAuthorization(s) err = client.GetJSONWithQuery( ctx, "/api/v1/test-list/tor-targets", query, &result) ``` After: ```Go // 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 // // use a model.DiscardLogger to avoid logging bridges return httpclientx.GetJSON[map[string]model.OOAPITorTarget](ctx, URL, &httpclientx.Config{ Authorization: s, Client: c.HTTPClient, Logger: model.DiscardLogger, UserAgent: c.UserAgent, }) ``` In both cases: uses the same client and user agent, uses the same URL path, and we're not logging.
- Loading branch information