Skip to content

Commit

Permalink
Extract get with timeout into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
4ernovm committed Dec 1, 2023
1 parent f7a8b51 commit 237621c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
8 changes: 6 additions & 2 deletions internal/client/deb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type IDebClient interface {
// Post makes a POST request to one of Debricked's API endpoints
Post(uri string, contentType string, body *bytes.Buffer, timeout int) (*http.Response, error)
// Get makes a GET request to one of Debricked's API endpoints
Get(uri string, format string) (*http.Response, error)
Get(uri string, format string, timeout int) (*http.Response, error)
SetAccessToken(accessToken *string)
ConfigureClientSettings(retry bool, timeout int)
}
Expand Down Expand Up @@ -51,7 +51,11 @@ func (debClient *DebClient) Post(uri string, contentType string, body *bytes.Buf
return post(uri, debClient, contentType, body, true)
}

func (debClient *DebClient) Get(uri string, format string) (*http.Response, error) {
func (debClient *DebClient) Get(uri string, format string, timeout int) (*http.Response, error) {
if timeout > 0 {
return getWithTimeout(uri, debClient, false, format, timeout)
}

return get(uri, debClient, debClient.retry, format)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/client/deb_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestClientUnauthorized(t *testing.T) {
})
client = NewDebClient(&tkn, clientMock)

res, err := client.Get("/api/1.0/open/user-profile/is-admin", "application/json")
res, err := client.Get("/api/1.0/open/user-profile/is-admin", "application/json", 0)
if err == nil {
t.Error("failed to assert client error")
defer res.Body.Close()
Expand All @@ -104,7 +104,7 @@ func TestGet(t *testing.T) {
})
client = NewDebClient(&tkn, clientMock)

res, err := client.Get("/api/1.0/open/user-profile/is-admin", "application/json")
res, err := client.Get("/api/1.0/open/user-profile/is-admin", "application/json", 0)
if err != nil {
t.Fatal("failed to assert that no client error occurred. Error:", err)
}
Expand Down
22 changes: 17 additions & 5 deletions internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ func get(uri string, debClient *DebClient, retry bool, format string) (*http.Res
return nil, err
}

if debClient.timeout > 0 {
timeoutDuration := time.Duration(debClient.timeout) * time.Second
ctx, cancel := context.WithTimeout(request.Context(), timeoutDuration)
defer cancel()
request = request.WithContext(ctx)
res, _ := debClient.httpClient.Do(request)
req := func() (*http.Response, error) {
return get(uri, debClient, false, format)
}

return interpret(res, req, debClient, retry)
}

func getWithTimeout(uri string, debClient *DebClient, retry bool, format string, timeout int) (*http.Response, error) {
request, err := newRequest("GET", *debClient.host+uri, debClient.jwtToken, format, nil)
if err != nil {
return nil, err
}

timeoutDuration := time.Duration(debClient.timeout) * time.Second
ctx, cancel := context.WithTimeout(request.Context(), timeoutDuration)
defer cancel()
request = request.WithContext(ctx)

res, _ := debClient.httpClient.Do(request)
req := func() (*http.Response, error) {
return get(uri, debClient, false, format)
Expand Down
4 changes: 2 additions & 2 deletions internal/client/testdata/deb_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func NewDebClientMock() *DebClientMock {
}
}

func (mock *DebClientMock) Get(uri string, format string) (*http.Response, error) {
func (mock *DebClientMock) Get(uri string, format string, timeout int) (*http.Response, error) {
response, err := mock.popResponse(mock.RemoveQueryParamsFromUri(uri))

if response != nil || !mock.serviceUp {
return response, err
}

return mock.realDebClient.Get(uri, format)
return mock.realDebClient.Get(uri, format, timeout)
}

func (mock *DebClientMock) Post(uri string, format string, body *bytes.Buffer, timeout int) (*http.Response, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func (finder *Finder) GetSupportedFormats() ([]*CompiledFormat, error) {
}

func (finder *Finder) GetSupportedFormatsJson() ([]byte, error) {
finder.debClient.ConfigureClientSettings(false, 3)
defer finder.debClient.ConfigureClientSettings(true, 15)
//finder.debClient.ConfigureClientSettings(false, 3)
//defer finder.debClient.ConfigureClientSettings(true, 15)

res, err := finder.debClient.Get(SupportedFormatsUri, "application/json")
res, err := finder.debClient.Get(SupportedFormatsUri, "application/json", 3)

if err != nil || res.StatusCode != http.StatusOK {
fmt.Printf("%s Unable to get supported formats from the server. Using cached data instead.\n", color.YellowString("⚠️"))
Expand Down
4 changes: 2 additions & 2 deletions internal/report/license/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r Reporter) Order(args report.IOrderArgs) error {
}

uri := fmt.Sprintf("/api/1.0/open/licenses/get-licenses?order=asc&sortColumn=name&generateExcel=1&commitId=%d&email=%s", commitId, orderArgs.Email)
res, err := r.DebClient.Get(uri, "application/json")
res, err := r.DebClient.Get(uri, "application/json", 0)
if err != nil {
return err
}
Expand All @@ -62,7 +62,7 @@ type commit struct {

func (r Reporter) getCommitId(hash string) (int, error) {
uri := fmt.Sprintf("/api/1.0/open/releases/by/name?name=%s", hash)
res, err := r.DebClient.Get(uri, "application/json")
res, err := r.DebClient.Get(uri, "application/json", 0)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/report/vulnerability/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r Reporter) Order(args report.IOrderArgs) error {
}

uri := fmt.Sprintf("/api/1.0/open/repositories/get-repositories?order=asc&generateExcel=1&email=%s", orderArgs.Email)
res, err := r.DebClient.Get(uri, "application/json")
res, err := r.DebClient.Get(uri, "application/json", 0)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/upload/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (uploadBatch *uploadBatch) wait() (*UploadResult, error) {
var resultStatus *UploadResult
uri := fmt.Sprintf("/api/1.0/open/ci/upload/status?ciUploadId=%s", strconv.Itoa(uploadBatch.ciUploadId))
for !bar.IsFinished() {
res, err := (*uploadBatch.client).Get(uri, "application/json")
res, err := (*uploadBatch.client).Get(uri, "application/json", 0)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 237621c

Please sign in to comment.