From 114e103fcedea65fb04f1fb842e5b8c34bbab051 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Fri, 11 Oct 2024 09:24:34 +0700 Subject: [PATCH] [mobile] add self-info response --- smsgateway/domain.go | 11 ------ smsgateway/domain_devices.go | 14 ++++++++ smsgateway/domain_webhooks.go | 49 +++++++++++++++++++++------ smsgateway/domain_webhooks_test.go | 34 +++++++++++++++++++ smsgateway/responses.go | 6 ++++ smsgateway/webhooks/webhook.go | 54 ++++++++++-------------------- 6 files changed, 111 insertions(+), 57 deletions(-) create mode 100644 smsgateway/domain_devices.go create mode 100644 smsgateway/domain_webhooks_test.go diff --git a/smsgateway/domain.go b/smsgateway/domain.go index 82faa45..598db34 100644 --- a/smsgateway/domain.go +++ b/smsgateway/domain.go @@ -21,17 +21,6 @@ var allProcessStates = map[ProcessingState]struct{}{ ProcessingStateFailed: {}, } -// Device -type Device struct { - ID string `json:"id" example:"PyDmBQZZXYmyxMwED8Fzy"` // ID - Name string `json:"name" example:"My Device"` // Name - CreatedAt time.Time `json:"createdAt" example:"2020-01-01T00:00:00Z"` // Created at (read only) - UpdatedAt time.Time `json:"updatedAt" example:"2020-01-01T00:00:00Z"` // Updated at (read only) - DeletedAt *time.Time `json:"deletedAt,omitempty" example:"2020-01-01T00:00:00Z"` // Deleted at (read only) - - LastSeen time.Time `json:"lastSeen" example:"2020-01-01T00:00:00Z"` // Last seen at (read only) -} - // Message type Message struct { ID string `json:"id,omitempty" validate:"omitempty,max=36" example:"PyDmBQZZXYmyxMwED8Fzy"` // ID (if not set - will be generated) diff --git a/smsgateway/domain_devices.go b/smsgateway/domain_devices.go new file mode 100644 index 0000000..e48fbb8 --- /dev/null +++ b/smsgateway/domain_devices.go @@ -0,0 +1,14 @@ +package smsgateway + +import "time" + +// Device +type Device struct { + ID string `json:"id" example:"PyDmBQZZXYmyxMwED8Fzy"` // ID + Name string `json:"name" example:"My Device"` // Name + CreatedAt time.Time `json:"createdAt" example:"2020-01-01T00:00:00Z"` // Created at (read only) + UpdatedAt time.Time `json:"updatedAt" example:"2020-01-01T00:00:00Z"` // Updated at (read only) + DeletedAt *time.Time `json:"deletedAt,omitempty" example:"2020-01-01T00:00:00Z"` // Deleted at (read only) + + LastSeen time.Time `json:"lastSeen" example:"2020-01-01T00:00:00Z"` // Last seen at (read only) +} diff --git a/smsgateway/domain_webhooks.go b/smsgateway/domain_webhooks.go index b55c188..a1ddda6 100644 --- a/smsgateway/domain_webhooks.go +++ b/smsgateway/domain_webhooks.go @@ -1,16 +1,45 @@ package smsgateway -import "github.com/android-sms-gateway/client-go/smsgateway/webhooks" - -// Deprecated: use webhooks package instead. -type WebhookEvent = webhooks.EventType +type WebhookEvent = string const ( - // Deprecated: use webhooks package instead. - WebhookEventSmsReceived WebhookEvent = webhooks.EventTypeSmsReceived - // Deprecated: use webhooks package instead. - WebhookEventSystemPing WebhookEvent = webhooks.EventTypeSystemPing + // Triggered when an SMS is received. + WebhookEventSmsReceived WebhookEvent = "sms:received" + // Triggered when an SMS is sent. + WebhookEventSmsSent WebhookEvent = "sms:sent" + // Triggered when an SMS is delivered. + WebhookEventSmsDelivered WebhookEvent = "sms:delivered" + // Triggered when an SMS processing fails. + WebhookEventSmsFailed WebhookEvent = "sms:failed" + // Triggered when the device pings the server. + WebhookEventSystemPing WebhookEvent = "system:ping" ) -// Deprecated: use webhook package instead. -type Webhook = webhooks.Webhook +var allEventTypes = map[WebhookEvent]struct{}{ + WebhookEventSmsReceived: {}, + WebhookEventSmsSent: {}, + WebhookEventSmsDelivered: {}, + WebhookEventSmsFailed: {}, + WebhookEventSystemPing: {}, +} + +// 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 IsValidWebhookEvent(e WebhookEvent) 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 WebhookEvent `json:"event" validate:"required" example:"sms:received"` +} diff --git a/smsgateway/domain_webhooks_test.go b/smsgateway/domain_webhooks_test.go new file mode 100644 index 0000000..78851cf --- /dev/null +++ b/smsgateway/domain_webhooks_test.go @@ -0,0 +1,34 @@ +package smsgateway_test + +import ( + "testing" + + "github.com/android-sms-gateway/client-go/smsgateway" +) + +// TestIsValidEventType tests the IsValidEventType function. +func TestIsValidEventType(t *testing.T) { + tests := []struct { + name string + e smsgateway.WebhookEvent + want bool + }{ + { + name: "Valid event type", + e: smsgateway.WebhookEventSmsDelivered, + 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 := smsgateway.IsValidWebhookEvent(tt.e); got != tt.want { + t.Errorf("IsValid() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/smsgateway/responses.go b/smsgateway/responses.go index 54ba143..0ce8266 100644 --- a/smsgateway/responses.go +++ b/smsgateway/responses.go @@ -1,5 +1,11 @@ package smsgateway +// Device self-information response +type MobileDeviceResponse struct { + Device *Device `json:"device,omitempty"` // Device information, empty if device is not registered on the server + ExternalIP string `json:"externalIp,omitempty"` // External IP +} + // Device registration response type MobileRegisterResponse struct { Id string `json:"id" example:"QslD_GefqiYV6RQXdkM6V"` // New device ID diff --git a/smsgateway/webhooks/webhook.go b/smsgateway/webhooks/webhook.go index 58beeec..0096c07 100644 --- a/smsgateway/webhooks/webhook.go +++ b/smsgateway/webhooks/webhook.go @@ -1,45 +1,27 @@ package webhooks -type EventType string +import "github.com/android-sms-gateway/client-go/smsgateway" + +// Deprecated: use smsgateway package instead. +type EventType = smsgateway.WebhookEvent const ( - // Triggered when an SMS is received. - EventTypeSmsReceived EventType = "sms:received" - // Triggered when an SMS is sent. - EventTypeSmsSent EventType = "sms:sent" - // Triggered when an SMS is delivered. - EventTypeSmsDelivered EventType = "sms:delivered" - // Triggered when an SMS processing fails. - EventTypeSmsFailed EventType = "sms:failed" - // Triggered when the device pings the server. - EventTypeSystemPing EventType = "system:ping" + // Deprecated: use smsgateway package instead. + EventTypeSmsReceived EventType = smsgateway.WebhookEventSmsReceived + // Deprecated: use smsgateway package instead. + EventTypeSmsSent EventType = smsgateway.WebhookEventSmsSent + // Deprecated: use smsgateway package instead. + EventTypeSmsDelivered EventType = smsgateway.WebhookEventSmsDelivered + // Deprecated: use smsgateway package instead. + EventTypeSmsFailed EventType = smsgateway.WebhookEventSmsFailed + // Deprecated: use smsgateway package instead. + EventTypeSystemPing EventType = smsgateway.WebhookEventSystemPing ) -var allEventTypes = map[EventType]struct{}{ - EventTypeSmsReceived: {}, - EventTypeSmsSent: {}, - EventTypeSmsDelivered: {}, - EventTypeSmsFailed: {}, - 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. +// Deprecated: use smsgateway package instead. func IsValidEventType(e EventType) bool { - _, ok := allEventTypes[e] - return ok + return smsgateway.IsValidWebhookEvent(e) } -// 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"` -} +// Deprecated: use smsgateway package instead. +type Webhook = smsgateway.Webhook