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

Update pkg/osv to allow overriding the http client / transport. #357

Merged
merged 1 commit into from
May 1, 2023
Merged
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
24 changes: 21 additions & 3 deletions pkg/osv/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ func checkResponseError(resp *http.Response) error {

// MakeRequest sends a batched query to osv.dev
func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {
return MakeRequestWithClient(request, http.DefaultClient)
}

// MakeRequestWithClient sends a batched query to osv.dev with the provided
// http client.
func MakeRequestWithClient(request BatchedQuery, client *http.Client) (*BatchedResponse, error) {
// API has a limit of 1000 bulk query per request
queryChunks := chunkBy(request.Queries, maxQueriesPerRequest)
var totalOsvResp BatchedResponse
Expand All @@ -140,7 +146,7 @@ func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {
resp, err := makeRetryRequest(func() (*http.Response, error) {
// We do not need a specific context
//nolint:noctx
return http.Post(QueryEndpoint, "application/json", requestBuf)
return client.Post(QueryEndpoint, "application/json", requestBuf)
})
if err != nil {
return nil, err
Expand All @@ -166,9 +172,15 @@ func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {

// Get a Vulnerability for the given ID.
func Get(id string) (*models.Vulnerability, error) {
return GetWithClient(id, http.DefaultClient)
}

// GetWithClient gets a Vulnerability for the given ID with the provided http
// client.
func GetWithClient(id string, client *http.Client) (*models.Vulnerability, error) {
resp, err := makeRetryRequest(func() (*http.Response, error) {
//nolint:noctx
return http.Get(GetEndpoint + "/" + id)
return client.Get(GetEndpoint + "/" + id)
})
if err != nil {
return nil, err
Expand All @@ -192,6 +204,12 @@ func Get(id string) (*models.Vulnerability, error) {
// Hydrate fills the results of the batched response with the full
// Vulnerability details.
func Hydrate(resp *BatchedResponse) (*HydratedBatchedResponse, error) {
return HydrateWithClient(resp, http.DefaultClient)
}

// HydrateWithClient fills the results of the batched response with the full
// Vulnerability details using the provided http client.
func HydrateWithClient(resp *BatchedResponse, client *http.Client) (*HydratedBatchedResponse, error) {
hydrated := HydratedBatchedResponse{}
ctx := context.TODO()
// Preallocate the array to avoid slice reallocations when inserting later
Expand All @@ -211,7 +229,7 @@ func Hydrate(resp *BatchedResponse) (*HydratedBatchedResponse, error) {
}

go func(id string, batchIdx int, resultIdx int) {
vuln, err := Get(id)
vuln, err := GetWithClient(id, client)
if err != nil {
errChan <- err
} else {
Expand Down