Deduplicate connector driver request helpers
The Crisp and Railway connectors each repeated their HTTP plumbing across the driver, the name resolver, and (for Crisp) the subscription settings fetcher: the same JoinPath/headers/Do for Crisp GETs and the same marshal/POST/headers for Railway GraphQL. The Crisp and Scaleway drivers also carried byte-identical owner/member role mapping and admin checks. Extract crispGet and railwayPost as package-private request helpers so each call site owns only status handling, and lift the owner/member role mapping into shared ownerMemberRoles/isOwnerRole helpers beside activeFromStatus in driver.go. Name the Crisp base URL and tier header as consts in probe.go's const block rather than inlining the literals, matching the file's existing convention. Behavior is unchanged. Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -1546,28 +1546,9 @@ func NewRailwayNameResolver(httpClient *http.Client) NameResolver {
|
||||
}
|
||||
|
||||
func (r *railwayNameResolver) ResolveInstanceName(ctx context.Context) (string, error) {
|
||||
body := struct {
|
||||
Query string `json:"query"`
|
||||
}{
|
||||
Query: `query { me { name workspaces { id name } } }`,
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(body)
|
||||
httpResp, err := railwayPost(ctx, r.httpClient, "account", `query { me { name workspaces { id name } } }`)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot marshal railway account query: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, railwayGraphQLEndpoint, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot create railway account request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
httpResp, err := r.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot execute railway account request: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer func() { _ = httpResp.Body.Close() }()
|
||||
@@ -1628,22 +1609,9 @@ func (r *crispNameResolver) ResolveInstanceName(ctx context.Context) (string, er
|
||||
return "", nil
|
||||
}
|
||||
|
||||
endpoint, err := url.JoinPath(crispAPIBaseURL, "website", url.PathEscape(r.websiteID))
|
||||
httpResp, err := crispGet(ctx, r.httpClient, "website", "website", url.PathEscape(r.websiteID))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot build crisp website URL: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot create crisp website request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set(crispTierHeader, crispTierValue)
|
||||
|
||||
httpResp, err := r.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot execute crisp website request: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer func() { _ = httpResp.Body.Close() }()
|
||||
|
||||
Reference in New Issue
Block a user