-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new webhooks API to client (#13)
* Add new webhooks API to client * remove custom actions * restore custom actions * IntegrationFilter *[]string --------- Co-authored-by: Vadim Stepanov <[email protected]>
- Loading branch information
Showing
4 changed files
with
317 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package aapi | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// WebhookService handles requests to outgoing webhook endpoint | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/ | ||
type WebhookService struct { | ||
client *Client | ||
url string | ||
} | ||
|
||
// NewWebhookService creates WebhookService with defined url | ||
func NewWebhookService(client *Client) *WebhookService { | ||
WebhookService := WebhookService{} | ||
WebhookService.client = client | ||
WebhookService.url = "webhooks" | ||
return &WebhookService | ||
} | ||
|
||
type PaginatedWebhooksResponse struct { | ||
PaginatedResponse | ||
Webhooks []*Webhook `json:"results"` | ||
} | ||
|
||
type Webhook struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Team string `json:"team"` | ||
Url string `json:"url"` | ||
TriggerType string `json:"trigger_type"` | ||
HttpMethod string `json:"http_method"` | ||
Data *string `json:"data"` | ||
Username *string `json:"username"` | ||
Password *string `json:"password"` | ||
AuthorizationHeader *string `json:"authorization_header"` | ||
TriggerTemplate *string `json:"trigger_template"` | ||
Headers *string `json:"headers"` | ||
ForwardAll bool `json:"forward_all"` | ||
IntegrationFilter *[]string `json:"integration_filter"` | ||
IsWebhookEnabled bool `json:"is_webhook_enabled"` | ||
} | ||
|
||
type ListWebhookOptions struct { | ||
ListOptions | ||
Name string `url:"name,omitempty" json:"name,omitempty"` | ||
} | ||
|
||
// ListWebhooks fetches all Webhooks for authorized organization | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/#list-actions | ||
func (service *WebhookService) ListWebhooks(opt *ListWebhookOptions) (*PaginatedWebhooksResponse, *http.Response, error) { | ||
u := fmt.Sprintf("%s", service.url) | ||
|
||
req, err := service.client.NewRequest("GET", u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var Webhooks *PaginatedWebhooksResponse | ||
resp, err := service.client.Do(req, &Webhooks) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return Webhooks, resp, err | ||
} | ||
|
||
type GetWebhookOptions struct { | ||
} | ||
|
||
// GetWebhook fetches webhook by given id. | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/ | ||
func (service *WebhookService) GetWebhook(id string, opt *GetWebhookOptions) (*Webhook, *http.Response, error) { | ||
u := fmt.Sprintf("%s/%s/", service.url, id) | ||
|
||
req, err := service.client.NewRequest("GET", u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
Webhook := new(Webhook) | ||
resp, err := service.client.Do(req, Webhook) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return Webhook, resp, err | ||
} | ||
|
||
type CreateWebhookOptions struct { | ||
Name string `json:"name"` | ||
Team string `json:"team"` | ||
Url string `json:"url"` | ||
TriggerType string `json:"trigger_type"` | ||
HttpMethod string `json:"http_method"` | ||
Data *string `json:"data"` | ||
Username *string `json:"username"` | ||
Password *string `json:"password"` | ||
AuthorizationHeader *string `json:"authorization_header"` | ||
TriggerTemplate *string `json:"trigger_template"` | ||
Headers *string `json:"headers"` | ||
ForwardAll bool `json:"forward_all"` | ||
IntegrationFilter *[]string `json:"integration_filter"` | ||
IsWebhookEnabled bool `json:"is_webhook_enabled"` | ||
} | ||
|
||
// CreateWebhook creates webhook | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/ | ||
func (service *WebhookService) CreateWebhook(opt *CreateWebhookOptions) (*Webhook, *http.Response, error) { | ||
u := fmt.Sprintf("%s/", service.url) | ||
|
||
req, err := service.client.NewRequest("POST", u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
Webhook := new(Webhook) | ||
|
||
resp, err := service.client.Do(req, Webhook) | ||
|
||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return Webhook, resp, err | ||
} | ||
|
||
type UpdateWebhookOptions struct { | ||
Name string `json:"name"` | ||
Team string `json:"team"` | ||
Url string `json:"url"` | ||
TriggerType string `json:"trigger_type"` | ||
HttpMethod string `json:"http_method"` | ||
Data *string `json:"data"` | ||
Username *string `json:"username"` | ||
Password *string `json:"password"` | ||
AuthorizationHeader *string `json:"authorization_header"` | ||
TriggerTemplate *string `json:"trigger_template"` | ||
Headers *string `json:"headers"` | ||
ForwardAll bool `json:"forward_all"` | ||
IntegrationFilter *[]string `json:"integration_filter"` | ||
IsWebhookEnabled bool `json:"is_webhook_enabled"` | ||
} | ||
|
||
// UpdateWebhook updates webhook | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/ | ||
func (service *WebhookService) UpdateWebhook(id string, opt *UpdateWebhookOptions) (*Webhook, *http.Response, error) { | ||
u := fmt.Sprintf("%s/%s/", service.url, id) | ||
|
||
req, err := service.client.NewRequest("PUT", u, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
Webhook := new(Webhook) | ||
resp, err := service.client.Do(req, Webhook) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return Webhook, resp, err | ||
} | ||
|
||
type DeleteWebhookOptions struct { | ||
} | ||
|
||
// DeleteWebhook deletes webhook. | ||
// | ||
// https://grafana.com/docs/grafana-cloud/oncall/oncall-api-reference/outgoing_webhooks/ | ||
func (service *WebhookService) DeleteWebhook(id string, opt *DeleteWebhookOptions) (*http.Response, error) { | ||
|
||
u := fmt.Sprintf("%s/%s/", service.url, id) | ||
|
||
req, err := service.client.NewRequest("DELETE", u, opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := service.client.Do(req, nil) | ||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package aapi | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testWebhook = &Webhook{ | ||
ID: "KGEFG74LU1D8L", | ||
Name: "Test action", | ||
Team: "T3HRAP3K3IKOP", | ||
HttpMethod: "POST", | ||
TriggerType: "escalation step", | ||
Url: "http://test.com", | ||
} | ||
|
||
var testWebhookBody = `{ | ||
"id": "KGEFG74LU1D8L", | ||
"name": "Test action", | ||
"team": "T3HRAP3K3IKOP", | ||
"http_method": "POST", | ||
"trigger_type": "escalation step", | ||
"url":"http://test.com" | ||
}` | ||
|
||
func TestListWebhooks(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v1/webhooks/", func(w http.ResponseWriter, r *http.Request) { | ||
testRequestMethod(t, r, "GET") | ||
fmt.Fprint(w, fmt.Sprintf(`{"count": 1, "next": null, "previous": null, "results": [%s]}`, testWebhookBody)) | ||
}) | ||
|
||
options := &ListWebhookOptions{ | ||
Name: "Test action", | ||
} | ||
|
||
Webhooks, _, err := client.Webhooks.ListWebhooks(options) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := &PaginatedWebhooksResponse{ | ||
PaginatedResponse: PaginatedResponse{ | ||
Count: 1, | ||
Next: nil, | ||
Previous: nil, | ||
}, | ||
Webhooks: []*Webhook{ | ||
testWebhook, | ||
}, | ||
} | ||
if !reflect.DeepEqual(want, Webhooks) { | ||
t.Errorf("returned\n %+v, \nwant\n %+v", Webhooks, want) | ||
} | ||
} | ||
|
||
func TestCreateWebhook(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v1/webhooks/", func(w http.ResponseWriter, r *http.Request) { | ||
testRequestMethod(t, r, "POST") | ||
fmt.Fprint(w, testWebhookBody) | ||
}) | ||
|
||
createOptions := &CreateWebhookOptions{ | ||
Name: "Test Webhook", | ||
Url: "https://example.com", | ||
} | ||
Webhook, _, err := client.Webhooks.CreateWebhook(createOptions) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := testWebhook | ||
|
||
if !reflect.DeepEqual(want, Webhook) { | ||
t.Errorf("returned\n %+v\n want\n %+v\n", Webhook, want) | ||
} | ||
} | ||
|
||
func TestDeleteWebhook(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v1/webhooks/KGEFG74LU1D8L/", func(w http.ResponseWriter, r *http.Request) { | ||
testRequestMethod(t, r, "DELETE") | ||
}) | ||
|
||
options := &DeleteWebhookOptions{} | ||
|
||
_, err := client.Webhooks.DeleteWebhook("KGEFG74LU1D8L", options) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestGetWebhook(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v1/webhooks/KGEFG74LU1D8L/", func(w http.ResponseWriter, r *http.Request) { | ||
testRequestMethod(t, r, "GET") | ||
fmt.Fprint(w, testWebhookBody) | ||
}) | ||
|
||
options := &GetWebhookOptions{} | ||
|
||
Webhook, _, err := client.Webhooks.GetWebhook("KGEFG74LU1D8L", options) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := testWebhook | ||
|
||
if !reflect.DeepEqual(want, Webhook) { | ||
t.Errorf("returned\n %+v\n want\n %+v\n", Webhook, want) | ||
} | ||
} |