This is an automated email from the ASF dual-hosted git repository.
warren pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new 551ea1759 refactor: simplify azure api client creation (#4308)
551ea1759 is described below
commit 551ea17594a4bcd165dd4a45abd61e81f31bb292
Author: Klesh Wong <[email protected]>
AuthorDate: Fri Feb 3 12:45:48 2023 +0800
refactor: simplify azure api client creation (#4308)
* refactor: simplify azure api client creation
* docs: add endpoint example to swag doc
---
backend/plugins/azure/api/connection.go | 97 ++++++++++++++++--------------
backend/plugins/azure/models/connection.go | 20 ++----
backend/plugins/azure/tasks/api_client.go | 7 +--
3 files changed, 58 insertions(+), 66 deletions(-)
diff --git a/backend/plugins/azure/api/connection.go
b/backend/plugins/azure/api/connection.go
index c645197c2..5648c4825 100644
--- a/backend/plugins/azure/api/connection.go
+++ b/backend/plugins/azure/api/connection.go
@@ -19,38 +19,35 @@ package api
import (
"context"
- "fmt"
+ "net/http"
+
"github.com/apache/incubator-devlake/core/errors"
plugin "github.com/apache/incubator-devlake/core/plugin"
- "github.com/apache/incubator-devlake/core/utils"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/azure/models"
- "net/http"
- "time"
)
+// @Summary test azure connection
+// @Description Test azure Connection. endpoint:
"https://dev.azure.com/{organization}/
+// @Tags pluginsazure/
+// @Param body body models.AzureConn true "json body"
+// @Success 200 {object} shared.ApiBody "Success"
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/test [POST]
func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
// decode
- var connection models.TestConnectionRequest
+ var connection models.AzureConn
if err := api.Decode(input.Body, &connection, vld); err != nil {
return nil, errors.BadInput.Wrap(err, "could not decode request
parameters")
}
// test connection
- encodedToken := utils.GetEncodedToken(connection.Username,
connection.Password)
- apiClient, err := api.NewApiClient(
- context.TODO(),
- connection.Endpoint,
- map[string]string{
- "Authorization": fmt.Sprintf("Basic %v", encodedToken),
- },
- 3*time.Second,
- connection.Proxy,
- basicRes,
- )
+ apiClient, err := api.NewApiClientFromConnection(context.TODO(),
basicRes, connection)
if err != nil {
return nil, err
}
- res, err := apiClient.Get("", nil, nil)
+
+ res, err := apiClient.Get("_apis/projects", nil, nil)
if err != nil {
return nil, err
}
@@ -61,16 +58,14 @@ func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
return nil, nil
}
-/*
-POST /plugins/zaure/connections
-
- {
- "name": "zaure data connection name",
- "endpoint": "zaure api endpoint, i.e. https://ci.zaure.io/",
- "username": "username, usually should be email address",
- "password": "zaure api access token"
- }
-*/
+// @Summary create azure connection
+// @Description Create azure connection
+// @Tags pluginsazure/
+// @Param body body models.AzureConnection true "json body"
+// @Success 200 {object} models.AzureConnection
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/connections [POST]
func PostConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
// create a new connection
connection := &models.AzureConnection{}
@@ -83,16 +78,14 @@ func PostConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
return &plugin.ApiResourceOutput{Body: connection, Status:
http.StatusOK}, nil
}
-/*
-PATCH /plugins/zaure/connections/connectionId
-{
- "name": "zaure data connection name",
- "endpoint": "zaure api endpoint, i.e. https://ci.zaure.io/",
- "username": "username, usually should be email address",
- "password": "zaure api access token"
-}
-*/
-
+// @Summary patch azure connection
+// @Description Patch azure connection
+// @Tags pluginsazure/
+// @Param body body models.AzureConnection true "json body"
+// @Success 200 {object} models.AzureConnection
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/connections/{connectionId} [PATCH]
func PatchConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
connection := &models.AzureConnection{}
err := connectionHelper.Patch(connection, input)
@@ -103,9 +96,13 @@ func PatchConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
return &plugin.ApiResourceOutput{Body: connection}, nil
}
-/*
-DELETE /plugins/zaure/connections/connectionId
-*/
+// @Summary delete a azure connection
+// @Description Delete a azure connection
+// @Tags pluginsazure/
+// @Success 200 {object} models.AzureConnection
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/connections/{connectionId} [DELETE]
func DeleteConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
connection := &models.AzureConnection{}
err := connectionHelper.First(connection, input.Params)
@@ -116,9 +113,13 @@ func DeleteConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput
return &plugin.ApiResourceOutput{Body: connection}, err
}
-/*
-GET /plugins/zaure/connections
-*/
+// @Summary get all azure connections
+// @Description Get all azure connections
+// @Tags pluginsazure/
+// @Success 200 {object} []models.AzureConnection
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/connections [GET]
func ListConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
var connections []models.AzureConnection
err := connectionHelper.List(&connections)
@@ -129,9 +130,13 @@ func ListConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
return &plugin.ApiResourceOutput{Body: connections, Status:
http.StatusOK}, nil
}
-/*
-GET /plugins/zaure/connections/connectionId
-*/
+// @Summary get azure connection detail
+// @Description Get azure connection detail
+// @Tags pluginsazure/
+// @Success 200 {object} models.AzureConnection
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/azure/connections/{connectionId} [GET]
func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
errors.Error) {
connection := &models.AzureConnection{}
err := connectionHelper.First(connection, input.Params)
diff --git a/backend/plugins/azure/models/connection.go
b/backend/plugins/azure/models/connection.go
index 115d6fd4e..4d58ec368 100644
--- a/backend/plugins/azure/models/connection.go
+++ b/backend/plugins/azure/models/connection.go
@@ -21,24 +21,16 @@ import (
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
)
-// This object conforms to what the frontend currently sends.
-type AzureConnection struct {
- helper.BaseConnection `mapstructure:",squash"`
+// AzureConn holds the essential information to connect to the Azure API
+type AzureConn struct {
helper.RestConnection `mapstructure:",squash"`
helper.BasicAuth `mapstructure:",squash"`
}
-type AzureResponse struct {
- ID int `json:"id"`
- Name string `json:"name"`
- AzureConnection
-}
-
-type TestConnectionRequest struct {
- Endpoint string `json:"endpoint" validate:"required"`
- Username string `json:"username" validate:"required"`
- Password string `json:"password" validate:"required"`
- Proxy string `json:"proxy"`
+// AzureConnection holds AzureConn plus ID/Name for database storage
+type AzureConnection struct {
+ helper.BaseConnection `mapstructure:",squash"`
+ AzureConn `mapstructure:",squash"`
}
func (AzureConnection) TableName() string {
diff --git a/backend/plugins/azure/tasks/api_client.go
b/backend/plugins/azure/tasks/api_client.go
index 1d02eaad7..cb0853156 100644
--- a/backend/plugins/azure/tasks/api_client.go
+++ b/backend/plugins/azure/tasks/api_client.go
@@ -18,7 +18,6 @@ limitations under the License.
package tasks
import (
- "fmt"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -27,11 +26,7 @@ import (
func CreateApiClient(taskCtx plugin.TaskContext, connection
*models.AzureConnection) (*api.ApiAsyncClient, errors.Error) {
// create synchronize api client so we can calculate api rate limit
dynamically
- headers := map[string]string{
- "Authorization": fmt.Sprintf("Basic %v",
connection.GetEncodedToken()),
- }
-
- apiClient, err := api.NewApiClient(taskCtx.GetContext(),
connection.Endpoint, headers, 0, connection.Proxy, taskCtx)
+ apiClient, err := api.NewApiClientFromConnection(taskCtx.GetContext(),
taskCtx, connection)
if err != nil {
return nil, err
}