|
| 1 | +package voice |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "time" |
| 8 | + |
| 9 | + messagebird "github.com/messagebird/go-rest-api" |
| 10 | +) |
| 11 | + |
| 12 | +// A Webhook is an HTTP callback to your platform. They are sent when calls are |
| 13 | +// created and updated. |
| 14 | +type Webhook struct { |
| 15 | + ID string |
| 16 | + URL string |
| 17 | + Token string |
| 18 | + CreatedAt time.Time |
| 19 | + UpdatedAt time.Time |
| 20 | +} |
| 21 | + |
| 22 | +type jsonWebhook struct { |
| 23 | + ID string `json:"id"` |
| 24 | + URL string `json:"url"` |
| 25 | + Token string `json:"token"` |
| 26 | + CreatedAt string `json:"createdAt"` |
| 27 | + UpdatedAt string `json:"updatedAt"` |
| 28 | +} |
| 29 | + |
| 30 | +// MarshalJSON implements the json.Marshaler interface. |
| 31 | +func (wh Webhook) MarshalJSON() ([]byte, error) { |
| 32 | + data := jsonWebhook{ |
| 33 | + ID: wh.ID, |
| 34 | + URL: wh.URL, |
| 35 | + Token: wh.Token, |
| 36 | + CreatedAt: wh.CreatedAt.Format(time.RFC3339), |
| 37 | + UpdatedAt: wh.UpdatedAt.Format(time.RFC3339), |
| 38 | + } |
| 39 | + return json.Marshal(data) |
| 40 | +} |
| 41 | + |
| 42 | +// UnmarshalJSON implements the json.Unmarshaler interface. |
| 43 | +func (wh *Webhook) UnmarshalJSON(data []byte) error { |
| 44 | + var raw jsonWebhook |
| 45 | + if err := json.Unmarshal(data, &raw); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + createdAt, err := time.Parse(time.RFC3339, raw.CreatedAt) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("unable to parse Webhook CreatedAt: %v", err) |
| 51 | + } |
| 52 | + updatedAt, err := time.Parse(time.RFC3339, raw.UpdatedAt) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("unable to parse Webhook UpdatedAt: %v", err) |
| 55 | + } |
| 56 | + *wh = Webhook{ |
| 57 | + ID: raw.ID, |
| 58 | + URL: raw.URL, |
| 59 | + Token: raw.Token, |
| 60 | + CreatedAt: createdAt, |
| 61 | + UpdatedAt: updatedAt, |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// Webhooks returns a paginator over all webhooks. |
| 67 | +func Webhooks(client *messagebird.Client) *Paginator { |
| 68 | + return newPaginator(client, "webhooks/", reflect.TypeOf(Webhook{})) |
| 69 | +} |
| 70 | + |
| 71 | +// CreateWebHook creates a new webhook the specified url that will be called |
| 72 | +// and security token. |
| 73 | +func CreateWebHook(client *messagebird.Client, url, token string) (*Webhook, error) { |
| 74 | + data := struct { |
| 75 | + URL string `json:"url"` |
| 76 | + Token string `json:"token,omitempty"` |
| 77 | + }{ |
| 78 | + URL: url, |
| 79 | + Token: token, |
| 80 | + } |
| 81 | + wh := &Webhook{} |
| 82 | + if err := client.Request(wh, "POST", "webhooks/", data); err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return wh, nil |
| 86 | +} |
| 87 | + |
| 88 | +// Update syncs hte local state of a webhook to the MessageBird API. |
| 89 | +func (wh *Webhook) Update(client *messagebird.Client) error { |
| 90 | + var data struct { |
| 91 | + Data []Webhook `json:"data"` |
| 92 | + } |
| 93 | + if err := client.Request(&data, "PUT", "webhooks/"+wh.ID, wh); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + *wh = data.Data[0] |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// Delete deletes a webhook. |
| 101 | +func (wh *Webhook) Delete(client *messagebird.Client) error { |
| 102 | + return client.Request(nil, "DELETE", "webhooks/"+wh.ID, nil) |
| 103 | +} |
0 commit comments