-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
package smsgateway | ||
|
||
// The type of event a webhook can be triggered for. | ||
type WebhookEvent string | ||
import "github.com/android-sms-gateway/client-go/smsgateway/webhooks" | ||
|
||
// Deprecated: use webhooks package instead. | ||
type WebhookEvent = webhooks.EventType | ||
|
||
const ( | ||
// Triggered when an SMS is received. | ||
WebhookEventSmsReceived WebhookEvent = "sms:received" | ||
// Triggered when the device pings the server. | ||
WebhookEventSystemPing WebhookEvent = "system:ping" | ||
// Deprecated: use webhooks package instead. | ||
WebhookEventSmsReceived WebhookEvent = webhooks.EventTypeSmsReceived | ||
// Deprecated: use webhooks package instead. | ||
WebhookEventSystemPing WebhookEvent = webhooks.EventTypeSystemPing | ||
) | ||
|
||
// A webhook configuration. | ||
type Webhook struct { | ||
// The unique identifier of the webhook. | ||
ID string `json:"id" validate:"max=36" example:"123e4567-e89b-12d3-a456-426614174000"` | ||
|
||
// The URL the webhook will be sent to. | ||
URL string `json:"url" validate:"required,http_url" example:"https://example.com/webhook"` | ||
|
||
// The type of event the webhook is triggered for. | ||
Event WebhookEvent `json:"event" validate:"required" example:"sms:received"` | ||
} | ||
// Deprecated: use webhook package instead. | ||
type Webhook = webhooks.Webhook |
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,39 @@ | ||
package webhooks | ||
|
||
type EventType string | ||
|
||
const ( | ||
// Triggered when an SMS is received. | ||
EventTypeSmsReceived EventType = "sms:received" | ||
// Triggered when an SMS is sent. | ||
EventTypeSmsSent EventType = "sms:sent" | ||
// Triggered when the device pings the server. | ||
EventTypeSystemPing EventType = "system:ping" | ||
) | ||
|
||
var allEventTypes = map[EventType]struct{}{ | ||
EventTypeSmsReceived: {}, | ||
EventTypeSmsSent: {}, | ||
EventTypeSystemPing: {}, | ||
} | ||
|
||
// IsValid checks if the given event type is valid. | ||
// | ||
// e is the event type to be checked. | ||
// Returns true if the event type is valid, false otherwise. | ||
func IsValidEventType(e EventType) bool { | ||
_, ok := allEventTypes[e] | ||
return ok | ||
} | ||
|
||
// A webhook configuration. | ||
type Webhook struct { | ||
// The unique identifier of the webhook. | ||
ID string `json:"id" validate:"max=36" example:"123e4567-e89b-12d3-a456-426614174000"` | ||
|
||
// The URL the webhook will be sent to. | ||
URL string `json:"url" validate:"required,http_url" example:"https://example.com/webhook"` | ||
|
||
// The type of event the webhook is triggered for. | ||
Event EventType `json:"event" validate:"required" example:"sms:received"` | ||
} |
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,34 @@ | ||
package webhooks_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/android-sms-gateway/client-go/smsgateway/webhooks" | ||
) | ||
|
||
// TestIsValidEventType tests the IsValidEventType function. | ||
func TestIsValidEventType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
e webhooks.EventType | ||
want bool | ||
}{ | ||
{ | ||
name: "Valid event type", | ||
e: webhooks.EventTypeSmsReceived, | ||
want: true, | ||
}, | ||
{ | ||
name: "Invalid event type", | ||
e: "invalid:event", | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := webhooks.IsValidEventType(tt.e); got != tt.want { | ||
t.Errorf("IsValid() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |