Skip to content

Commit

Permalink
[mobile] add self-info response
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Oct 13, 2024
1 parent ec3f83a commit 114e103
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 57 deletions.
11 changes: 0 additions & 11 deletions smsgateway/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions smsgateway/domain_devices.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 39 additions & 10 deletions smsgateway/domain_webhooks.go
Original file line number Diff line number Diff line change
@@ -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"`
}
34 changes: 34 additions & 0 deletions smsgateway/domain_webhooks_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
6 changes: 6 additions & 0 deletions smsgateway/responses.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
54 changes: 18 additions & 36 deletions smsgateway/webhooks/webhook.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 114e103

Please sign in to comment.