fix: add getAfterResponse to overwrite by helper (#2755)
* fix: fix afterResponse callback will overwrite by helper
* fix: fix for ci
Co-authored-by: linyh <yanghui@meri.co>
diff --git a/plugins/feishu/tasks/api_client.go b/plugins/feishu/tasks/api_client.go
index f726dc7..b762a60 100644
--- a/plugins/feishu/tasks/api_client.go
+++ b/plugins/feishu/tasks/api_client.go
@@ -19,8 +19,6 @@
import (
"fmt"
- "net/http"
-
"github.com/apache/incubator-devlake/plugins/feishu/apimodels"
"github.com/apache/incubator-devlake/plugins/feishu/models"
@@ -65,13 +63,6 @@
"Authorization": fmt.Sprintf("Bearer %v", tokenResBody.TenantAccessToken),
})
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("feishu authentication failed, please check your AccessToken")
- }
- return nil
- })
-
// create async api client
asyncApiCLient, err := helper.CreateAsyncApiClient(taskCtx, apiClient, &helper.ApiRateLimitCalculator{
UserRateLimitPerHour: connection.RateLimitPerHour,
diff --git a/plugins/gitee/tasks/api_client.go b/plugins/gitee/tasks/api_client.go
index 230f5d6..8449437 100644
--- a/plugins/gitee/tasks/api_client.go
+++ b/plugins/gitee/tasks/api_client.go
@@ -42,13 +42,6 @@
return nil
})
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your Basic Auth Token")
- }
- return nil
- })
-
rateLimiter := &helper.ApiRateLimitCalculator{
UserRateLimitPerHour: connection.RateLimitPerHour,
DynamicRateLimit: func(res *http.Response) (int, time.Duration, error) {
diff --git a/plugins/github/tasks/api_client.go b/plugins/github/tasks/api_client.go
index 326d52c..131467c 100644
--- a/plugins/github/tasks/api_client.go
+++ b/plugins/github/tasks/api_client.go
@@ -46,12 +46,6 @@
tokenIndex = (tokenIndex + 1) % len(tokens)
return nil
})
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your AccessToken configuration")
- }
- return nil
- })
// create rate limit calculator
rateLimiter := &helper.ApiRateLimitCalculator{
diff --git a/plugins/gitlab/tasks/api_client.go b/plugins/gitlab/tasks/api_client.go
index fe40a4a..804f2ba 100644
--- a/plugins/gitlab/tasks/api_client.go
+++ b/plugins/gitlab/tasks/api_client.go
@@ -38,12 +38,6 @@
if err != nil {
return nil, err
}
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your AccessToken")
- }
- return nil
- })
// create rate limit calculator
rateLimiter := &helper.ApiRateLimitCalculator{
diff --git a/plugins/helper/api_async_client.go b/plugins/helper/api_async_client.go
index d86ac0b..fe60a82 100644
--- a/plugins/helper/api_async_client.go
+++ b/plugins/helper/api_async_client.go
@@ -239,6 +239,7 @@
HasError() bool
NextTick(task func() error)
GetNumOfWorkers() int
+ GetAfterFunction() common.ApiClientAfterResponse
SetAfterFunction(callback common.ApiClientAfterResponse)
Release()
}
diff --git a/plugins/helper/api_client.go b/plugins/helper/api_client.go
index 6bebdb7..2baf078 100644
--- a/plugins/helper/api_client.go
+++ b/plugins/helper/api_client.go
@@ -47,7 +47,7 @@
endpoint string
headers map[string]string
beforeRequest common.ApiClientBeforeRequest
- afterReponse common.ApiClientAfterResponse
+ afterResponse common.ApiClientAfterResponse
ctx context.Context
logger core.Logger
}
@@ -146,14 +146,24 @@
return apiClient.headers
}
-// SetBeforeFunction FIXME ...
+// GetBeforeFunction return beforeResponseFunction
+func (apiClient *ApiClient) GetBeforeFunction() common.ApiClientBeforeRequest {
+ return apiClient.beforeRequest
+}
+
+// SetBeforeFunction will set beforeResponseFunction
func (apiClient *ApiClient) SetBeforeFunction(callback common.ApiClientBeforeRequest) {
apiClient.beforeRequest = callback
}
-// SetAfterFunction FIXME ...
+// GetAfterFunction return afterResponseFunction
+func (apiClient *ApiClient) GetAfterFunction() common.ApiClientAfterResponse {
+ return apiClient.afterResponse
+}
+
+// SetAfterFunction will set afterResponseFunction
func (apiClient *ApiClient) SetAfterFunction(callback common.ApiClientAfterResponse) {
- apiClient.afterReponse = callback
+ apiClient.afterResponse = callback
}
// SetContext FIXME ...
@@ -250,8 +260,8 @@
}
// after receive
- if apiClient.afterReponse != nil {
- err = apiClient.afterReponse(res)
+ if apiClient.afterResponse != nil {
+ err = apiClient.afterResponse(res)
if err == ErrIgnoreAndContinue {
res.Body.Close()
return res, err
diff --git a/plugins/helper/api_collector.go b/plugins/helper/api_collector.go
index 60be876..6a4aaae 100644
--- a/plugins/helper/api_collector.go
+++ b/plugins/helper/api_collector.go
@@ -109,22 +109,22 @@
if args.ResponseParser == nil {
return nil, fmt.Errorf("ResponseParser is required")
}
- apicllector := &ApiCollector{
+ apiCollector := &ApiCollector{
RawDataSubTask: rawDataSubTask,
args: &args,
urlTemplate: tpl,
}
if args.AfterResponse != nil {
- apicllector.SetAfterResponse(args.AfterResponse)
- } else {
- apicllector.SetAfterResponse(func(res *http.Response) error {
+ apiCollector.SetAfterResponse(args.AfterResponse)
+ } else if apiCollector.GetAfterResponse() == nil {
+ apiCollector.SetAfterResponse(func(res *http.Response) error {
if res.StatusCode == http.StatusUnauthorized {
return fmt.Errorf("authentication failed, please check your AccessToken")
}
return nil
})
}
- return apicllector, nil
+ return apiCollector, nil
}
// Execute will start collection
@@ -303,7 +303,12 @@
return buf.String(), nil
}
-// SetAfterResponse FIXME ...
+// GetAfterResponse return apiClient's afterResponseFunction
+func (collector *ApiCollector) GetAfterResponse() common.ApiClientAfterResponse {
+ return collector.args.ApiClient.GetAfterFunction()
+}
+
+// SetAfterResponse set apiClient's afterResponseFunction
func (collector *ApiCollector) SetAfterResponse(f common.ApiClientAfterResponse) {
collector.args.ApiClient.SetAfterFunction(f)
}
diff --git a/plugins/helper/api_collector_test.go b/plugins/helper/api_collector_test.go
index ee9cb96..73055f8 100644
--- a/plugins/helper/api_collector_test.go
+++ b/plugins/helper/api_collector_test.go
@@ -72,6 +72,7 @@
}).Twice()
mockApi.On("HasError").Return(false)
mockApi.On("WaitAsync").Return(nil)
+ mockApi.On("GetAfterFunction", mock.Anything).Return(nil)
mockApi.On("SetAfterFunction", mock.Anything).Return()
params := struct {
Name string
diff --git a/plugins/icla/tasks/api_client.go b/plugins/icla/tasks/api_client.go
index 6e695e2..664b6a1 100644
--- a/plugins/icla/tasks/api_client.go
+++ b/plugins/icla/tasks/api_client.go
@@ -19,8 +19,6 @@
import (
"fmt"
- "net/http"
-
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/apache/incubator-devlake/utils"
@@ -50,14 +48,6 @@
"Authorization": fmt.Sprintf("Bearer %v", token),
})
- // TODO add some check after request if necessary
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your Bearer Auth Token")
- }
- return nil
- })
-
// create async api client
asyncApiClient, err := helper.CreateAsyncApiClient(taskCtx, apiClient, &helper.ApiRateLimitCalculator{
UserRateLimitPerHour: userRateLimit,
diff --git a/plugins/jenkins/tasks/client.go b/plugins/jenkins/tasks/client.go
index b226b32..b0a9840 100644
--- a/plugins/jenkins/tasks/client.go
+++ b/plugins/jenkins/tasks/client.go
@@ -19,8 +19,6 @@
import (
"fmt"
- "net/http"
-
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/apache/incubator-devlake/plugins/jenkins/models"
@@ -37,13 +35,6 @@
return nil, err
}
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your Username/Password")
- }
- return nil
- })
-
// create rate limit calculator
rateLimiter := &helper.ApiRateLimitCalculator{
UserRateLimitPerHour: connection.RateLimitPerHour,
diff --git a/plugins/jira/tasks/api_client.go b/plugins/jira/tasks/api_client.go
index 13f94ae..85312af 100644
--- a/plugins/jira/tasks/api_client.go
+++ b/plugins/jira/tasks/api_client.go
@@ -36,12 +36,6 @@
if err != nil {
return nil, err
}
- apiClient.SetAfterFunction(func(res *http.Response) error {
- if res.StatusCode == http.StatusUnauthorized {
- return fmt.Errorf("authentication failed, please check your AccessToken")
- }
- return nil
- })
// create rate limit calculator
rateLimiter := &helper.ApiRateLimitCalculator{