MCPcopy Create free account
hub / github.com/apache/devlake / testConnection

Function testConnection

backend/plugins/taiga/api/connection_api.go:39–120  ·  view source on GitHub ↗

testConnection tests the Taiga connection

(ctx context.Context, connection models.TaigaConnection)

Source from the content-addressed store, hash-verified

37
38// testConnection tests the Taiga connection
39func testConnection(ctx context.Context, connection models.TaigaConnection) (*TaigaTestConnResponse, errors.Error) {
40 // If username and password are provided, authenticate to get a token
41 if connection.Username != "" && connection.Password != "" && connection.Token == "" {
42 // Create a temporary connection without token for authentication
43 tempConnection := connection
44 tempConnection.Token = ""
45
46 // Create a temporary API client to call the auth endpoint
47 tempApiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &tempConnection)
48 if err != nil {
49 return nil, errors.Default.Wrap(err, "error creating API client")
50 }
51
52 // Prepare auth request body
53 authBody := map[string]interface{}{
54 "type": "normal",
55 "username": connection.Username,
56 "password": connection.Password,
57 }
58
59 // Authenticate to get token
60 authResponse := struct {
61 AuthToken string `json:"auth_token"`
62 }{}
63
64 res, err := tempApiClient.Post("auth", nil, authBody, nil)
65 if err != nil {
66 return nil, errors.Default.Wrap(err, "error authenticating with Taiga")
67 }
68
69 if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusBadRequest {
70 return nil, errors.HttpStatus(http.StatusBadRequest).New("authentication failed - please check your username and password")
71 }
72
73 if res.StatusCode != http.StatusOK {
74 return nil, errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code during auth: %d", res.StatusCode))
75 }
76
77 // Parse the auth response
78 err = api.UnmarshalResponse(res, &authResponse)
79 if err != nil {
80 return nil, errors.Default.Wrap(err, "error parsing authentication response")
81 }
82
83 // Set the token for validation
84 connection.Token = authResponse.AuthToken
85 }
86
87 // validate - but make Token optional if we have username/password
88 if vld != nil {
89 if connection.Token == "" && (connection.Username == "" || connection.Password == "") {
90 return nil, errors.Default.New("either token or username/password must be provided")
91 }
92 }
93
94 apiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &connection)
95 if err != nil {
96 return nil, err

Callers 2

TestConnectionFunction · 0.70
TestExistingConnectionFunction · 0.70

Calls 5

WrapMethod · 0.80
PostMethod · 0.65
NewMethod · 0.65
GetMethod · 0.65
SanitizeMethod · 0.45

Tested by 2

TestConnectionFunction · 0.56
TestExistingConnectionFunction · 0.56