likyh commented on code in PR #2641:
URL: https://github.com/apache/incubator-devlake/pull/2641#discussion_r934103247
##########
generator/template/plugin/tasks/api_client.go-template:
##########
@@ -26,47 +26,59 @@ import (
)
// TODO add what host would want to requist
-const ENDPOINT = "{{ .Endpoint }}"
-func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext)
(*helper.ApiAsyncClient, error) {
- // load and process configuration
- token := taskCtx.GetConfig("{{ .PLUGIN_NAME }}_TOKEN")
- if token == "" {
- println("invalid {{ .PLUGIN_NAME }}_TOKEN, but ignore this
error now")
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "time"
+
+ "github.com/apache/incubator-devlake/plugins/{{ .pluginName }}/models"
Review Comment:
plugin_name
##########
generator/template/plugin/tasks/api_client.go-template:
##########
@@ -26,47 +26,59 @@ import (
)
// TODO add what host would want to requist
-const ENDPOINT = "{{ .Endpoint }}"
-func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext)
(*helper.ApiAsyncClient, error) {
- // load and process configuration
- token := taskCtx.GetConfig("{{ .PLUGIN_NAME }}_TOKEN")
- if token == "" {
- println("invalid {{ .PLUGIN_NAME }}_TOKEN, but ignore this
error now")
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "time"
+
+ "github.com/apache/incubator-devlake/plugins/{{ .pluginName }}/models"
+
+ "github.com/apache/incubator-devlake/plugins/core"
+ "github.com/apache/incubator-devlake/plugins/helper"
+)
+
+func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext, connection
*models.{{ .PluginName }}Connection) (*helper.ApiAsyncClient, error) {
Review Comment:
Must the new plugin use the connection? Is there any simple way to deal with
it?
##########
generator/template/plugin/tasks/api_client.go-template:
##########
@@ -26,47 +26,59 @@ import (
)
// TODO add what host would want to requist
-const ENDPOINT = "{{ .Endpoint }}"
-func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext)
(*helper.ApiAsyncClient, error) {
- // load and process configuration
- token := taskCtx.GetConfig("{{ .PLUGIN_NAME }}_TOKEN")
- if token == "" {
- println("invalid {{ .PLUGIN_NAME }}_TOKEN, but ignore this
error now")
+import (
Review Comment:
import duplicate with Line 20~25
##########
generator/template/plugin/impl/impl.go-template:
##########
@@ -0,0 +1,123 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package impl
+
+import (
+ "github.com/apache/incubator-devlake/migration"
+ "github.com/apache/incubator-devlake/plugins/core"
+ "github.com/apache/incubator-devlake/plugins/helper"
+ "github.com/apache/incubator-devlake/plugins/{{ .plugin_name }}/api"
Review Comment:
there is no `api`/`models`/`migrationscripts` package
##########
generator/cmd/create_plugin.go:
##########
@@ -131,6 +131,7 @@ Type in what the name of plugin is, then generator will
create a new plugin in p
// read template
templates = map[string]string{
`plugin_main.go`:
util.ReadTemplate("generator/template/plugin/plugin_main_with_api_client.go-template"),
+ `impl/impl.go`:
util.ReadTemplate("generator/template/plugin/impl/impl.go-template"),
Review Comment:
impl_with_api_client.go-template ?
##########
generator/template/plugin/tasks/api_client.go-template:
##########
@@ -26,47 +26,59 @@ import (
)
// TODO add what host would want to requist
-const ENDPOINT = "{{ .Endpoint }}"
-func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext)
(*helper.ApiAsyncClient, error) {
- // load and process configuration
- token := taskCtx.GetConfig("{{ .PLUGIN_NAME }}_TOKEN")
- if token == "" {
- println("invalid {{ .PLUGIN_NAME }}_TOKEN, but ignore this
error now")
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+ "time"
+
+ "github.com/apache/incubator-devlake/plugins/{{ .pluginName }}/models"
+
+ "github.com/apache/incubator-devlake/plugins/core"
+ "github.com/apache/incubator-devlake/plugins/helper"
+)
+
+func New{{ .PluginName }}ApiClient(taskCtx core.TaskContext, connection
*models.{{ .PluginName }}Connection) (*helper.ApiAsyncClient, error) {
+ // create synchronize api client so we can calculate api rate limit
dynamically
+ headers := map[string]string{
+ "Authorization": fmt.Sprintf("Bearer %v", connection.Token),
}
- userRateLimit, err := utils.StrToIntOr(taskCtx.GetConfig("{{
.PLUGIN_NAME }}_API_REQUESTS_PER_HOUR"), 18000)
+ apiClient, err := helper.NewApiClient(taskCtx.GetContext(),
connection.Endpoint, headers, 0, connection.Proxy, taskCtx)
if err != nil {
return nil, err
}
- proxy := taskCtx.GetConfig("{{ .PLUGIN_NAME }}_PROXY")
+ apiClient.SetAfterFunction(func(res *http.Response) error {
+ if res.StatusCode == http.StatusUnauthorized {
+ return fmt.Errorf("authentication failed, please check
your AccessToken")
+ }
+ return nil
+ })
- // real request apiClient
- apiClient, err := helper.NewApiClient(ENDPOINT, nil, 0, proxy,
taskCtx.GetContext())
- if err != nil {
- return nil, err
- }
- // set token
- if token != "" {
- apiClient.SetHeaders(map[string]string{
- "Authorization": fmt.Sprintf("Bearer %v", token),
- })
+ // create rate limit calculator
+ rateLimiter := &helper.ApiRateLimitCalculator{
+ UserRateLimitPerHour: connection.RateLimitPerHour,
+ DynamicRateLimit: func(res *http.Response) (int, time.Duration,
error) {
+ rateLimitHeader := res.Header.Get("RateLimit-Limit")
Review Comment:
Not all API has the header `RateLimit-Limit`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]