Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api domain)!: improve status handling in API Domain; do not return an error on 404 #870

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/pkg/domains/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

type APIResponse struct {
Status int
Raw json.RawMessage
Response any
StatusCode int
Status string
Raw json.RawMessage
Response any
}

func (a ApiDomain) makeRequests(ctx context.Context) (types.DomainResources, error) {
Expand Down Expand Up @@ -63,11 +64,13 @@ func (a ApiDomain) makeRequests(ctx context.Context) (types.DomainResources, err
errs = errors.Join(errs, err)
}
if response != nil {
collection[request.Name] = types.DomainResources{
"status": response.Status,
"raw": response.Raw,
"response": response.Response,
dr := types.DomainResources{
"status": response.Status,
"statuscode": response.StatusCode,
"raw": response.Raw,
"response": response.Response,
}
collection[request.Name] = dr
} else {
// If the entire response is empty, return a validly empty resource
collection[request.Name] = types.DomainResources{"status": 0}
Expand Down
23 changes: 15 additions & 8 deletions src/pkg/domains/api/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,28 @@ func doHTTPReq(ctx context.Context, client http.Client, method string, url url.U
return nil, fmt.Errorf("error: %s returned empty response", url.Redacted())
}
defer res.Body.Close()

var respObj APIResponse
respObj.StatusCode = res.StatusCode
if res.Status == "" {
respObj.Status = http.StatusText(res.StatusCode)
} else {
respObj.Status = res.Status
}
responseData, err := io.ReadAll(res.Body)
if err != nil {
message.Debugf("error reading response body: %s", err)
return nil, err
}

var respObj APIResponse
respObj.Raw = responseData
respObj.Status = res.StatusCode
err = json.Unmarshal(responseData, &respObj.Response)
if err != nil {
message.Debugf("error unmarshalling response: %s", err)
if respObj.StatusCode >= http.StatusOK && respObj.StatusCode < http.StatusMultiStatus {
respObj.Raw = responseData
err = json.Unmarshal(responseData, &respObj.Response)
if err != nil {
message.Debugf("error unmarshalling response: %s", err)
return nil, err
}
}
return &respObj, err
return &respObj, nil
}

func clientFromOpts(opts *ApiOpts) http.Client {
Expand Down
19 changes: 11 additions & 8 deletions src/pkg/domains/api/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func TestGetResources(t *testing.T) {
"response": map[string]interface{}{
"healthcheck": "ok",
},
"status": 200,
"status": "200 OK",
"statuscode": 200,
}}
require.Equal(t, want, drs)
})
Expand All @@ -135,13 +136,15 @@ func TestGetResources(t *testing.T) {

require.NoError(t, err) // the spec is correct
drs, err := api.GetResources(context.Background())
require.Error(t, err)
require.Equal(t, drs, types.DomainResources{
require.NoError(t, err)
require.Equal(t, types.DomainResources{
apiReqName: types.DomainResources{
"status": 400,
"raw": json.RawMessage{},
"response": nil,
},
})
"statuscode": 400,
"status": "400 Bad Request",
"response": nil,
"raw": json.RawMessage(nil),
}},
drs,
)
})
}
Loading