PostConnections @Summary create webhook connection @Description Create webhook connection, example: {"name":"Webhook data connection name"} @Tags plugins/webhook @Param body body WebhookConnectionResponse true "json body" @Success 200 {object} WebhookConnectionResponse @Failure 400 {string} errcod
(input *plugin.ApiResourceInput)
| 40 | // @Failure 500 {string} errcode.Error "Internal Error" |
| 41 | // @Router /plugins/webhook/connections [POST] |
| 42 | func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 43 | // update from request and save to database |
| 44 | connection := &models.WebhookConnection{} |
| 45 | tx := basicRes.GetDal().Begin() |
| 46 | err := connectionHelper.CreateWithTx(tx, connection, input) |
| 47 | if err != nil { |
| 48 | if err := tx.Rollback(); err != nil { |
| 49 | logger.Error(err, "transaction Rollback") |
| 50 | } |
| 51 | if strings.Contains(err.Error(), "the connection name already exists (400)") { |
| 52 | return nil, errors.BadInput.New(fmt.Sprintf("A webhook with name %s already exists.", connection.Name)) |
| 53 | } |
| 54 | return nil, err |
| 55 | } |
| 56 | logger.Info("connection: %+v", connection) |
| 57 | name := apiKeyHelper.GenApiKeyNameForPlugin(pluginName, connection.ID) |
| 58 | allowedPath := fmt.Sprintf("/plugins/%s/connections/%d/.*", pluginName, connection.ID) |
| 59 | extra := fmt.Sprintf("connectionId:%d", connection.ID) |
| 60 | apiKeyRecord, err := apiKeyHelper.CreateForPlugin(tx, input.User, name, pluginName, allowedPath, extra) |
| 61 | if err != nil { |
| 62 | if err := tx.Rollback(); err != nil { |
| 63 | logger.Error(err, "transaction Rollback") |
| 64 | } |
| 65 | logger.Error(err, "CreateForPlugin") |
| 66 | return nil, err |
| 67 | } |
| 68 | if err := tx.Commit(); err != nil { |
| 69 | logger.Info("transaction commit: %s", err) |
| 70 | } |
| 71 | |
| 72 | webhookConnectionResponse, err := formatConnection(connection, false) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | webhookConnectionResponse.ApiKey = apiKeyRecord |
| 77 | logger.Info("api output connection: %+v", webhookConnectionResponse) |
| 78 | |
| 79 | return &plugin.ApiResourceOutput{Body: webhookConnectionResponse, Status: http.StatusOK}, nil |
| 80 | } |
| 81 | |
| 82 | // PatchConnection |
| 83 | // @Summary patch webhook connection |
nothing calls this directly
no test coverage detected