client

package
v0.1.0-beta.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package client provides a wrapper around the AhaSend SDK with enhanced functionality.

This package implements the AhaSendClient interface and provides additional features beyond the base SDK:

  • Rate limiting (50 requests/second with 100 burst capacity)
  • Automatic retry logic with exponential backoff
  • HTTP request/response logging for debugging
  • Structured error handling and API error translation
  • Context-aware request handling
  • Idempotency key support for message sending
  • Configuration validation and connection testing

The Client struct wraps the official AhaSend SDK while maintaining full compatibility and adding CLI-specific enhancements for better user experience.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AhaSendClient

type AhaSendClient interface {
	// Authentication and account info
	GetAccountID() string
	GetAuthContext() context.Context
	GetAccount() (*responses.Account, error)
	Ping() error
	ValidateConfiguration() error

	// Message operations
	SendMessage(req requests.CreateMessageRequest) (*responses.CreateMessageResponse, error)
	SendMessageWithIdempotencyKey(req requests.CreateMessageRequest, idempotencyKey string) (*responses.CreateMessageResponse, error)
	CancelMessage(accountID, messageID string) (*common.SuccessResponse, error)
	GetMessages(params requests.GetMessagesParams) (*responses.PaginatedMessagesResponse, error)
	GetMessage(messageID string) (*responses.Message, error)

	// Domain operations
	ListDomains(limit *int32, cursor *string) (*responses.PaginatedDomainsResponse, error)
	CreateDomain(domain string) (*responses.Domain, error)
	GetDomain(domain string) (*responses.Domain, error)
	DeleteDomain(domain string) (*common.SuccessResponse, error)

	// Webhook operations
	CreateWebhookVerifier(secret string) (*webhooks.WebhookVerifier, error)
	ListWebhooks(limit *int32, cursor *string) (*responses.PaginatedWebhooksResponse, error)
	CreateWebhook(req requests.CreateWebhookRequest) (*responses.Webhook, error)
	GetWebhook(webhookID string) (*responses.Webhook, error)
	UpdateWebhook(webhookID string, req requests.UpdateWebhookRequest) (*responses.Webhook, error)
	DeleteWebhook(webhookID string) error

	// Webhook streaming operations (development only)
	InitiateWebhookStream(webhookID string) (*WebhookStreamResponse, error)
	ConnectWebSocket(wsURL, webhookID string, forceReconnect, skipVerify bool) (*WebSocketClient, error)
	TriggerWebhook(webhookID string, events []string) error

	// Route operations
	ListRoutes(limit *int32, cursor *string) (*responses.PaginatedRoutesResponse, error)
	CreateRoute(req requests.CreateRouteRequest) (*responses.Route, error)
	GetRoute(routeID string) (*responses.Route, error)
	UpdateRoute(routeID string, req requests.UpdateRouteRequest) (*responses.Route, error)
	DeleteRoute(routeID string) error

	// Route streaming operations (development only)
	InitiateRouteStream(routeID, recipient string) (*RouteStreamResponse, error)
	TriggerRoute(routeID string) error

	// Suppression operations
	ListSuppressions(params requests.GetSuppressionsParams) (*responses.PaginatedSuppressionsResponse, error)
	CreateSuppression(req requests.CreateSuppressionRequest) (*responses.CreateSuppressionResponse, error)
	DeleteSuppression(email string, domain *string) (*common.SuccessResponse, error)
	WipeSuppressions(domains *string) (*common.SuccessResponse, error)

	// SMTP operations
	ListSMTPCredentials(limit *int32, cursor *string) (*responses.PaginatedSMTPCredentialsResponse, error)
	GetSMTPCredential(credentialID string) (*responses.SMTPCredential, error)
	CreateSMTPCredential(req requests.CreateSMTPCredentialRequest) (*responses.SMTPCredential, error)
	DeleteSMTPCredential(credentialID string) error

	// Statistics operations
	GetDeliverabilityStatistics(params requests.GetDeliverabilityStatisticsParams) (*responses.DeliverabilityStatisticsResponse, error)
	GetBounceStatistics(params requests.GetBounceStatisticsParams) (*responses.BounceStatisticsResponse, error)
	GetDeliveryTimeStatistics(params requests.GetDeliveryTimeStatisticsParams) (*responses.DeliveryTimeStatisticsResponse, error)

	// API Key operations
	ListAPIKeys(limit *int32, cursor *string) (*responses.PaginatedAPIKeysResponse, error)
	GetAPIKey(keyID string) (*responses.APIKey, error)
	CreateAPIKey(req requests.CreateAPIKeyRequest) (*responses.APIKey, error)
	UpdateAPIKey(keyID string, req requests.UpdateAPIKeyRequest) (*responses.APIKey, error)
	DeleteAPIKey(keyID string) (*common.SuccessResponse, error)
}

AhaSendClient defines the interface for AhaSend API operations This interface allows for better testability and mocking

type Client

type Client struct {
	*api.APIClient
	// contains filtered or unexported fields
}

Client wraps the AhaSend SDK client with additional functionality

func NewClient

func NewClient(apiKey, accountID string, apiURL ...string) (*Client, error)

NewClient creates a new AhaSend client with rate limiting

func (*Client) CancelMessage

func (c *Client) CancelMessage(accountID, messageID string) (*common.SuccessResponse, error)

CancelMessage cancels a scheduled message

func (*Client) ConnectWebSocket

func (c *Client) ConnectWebSocket(wsURL, webhookID string, forceReconnect, skipVerify bool) (*WebSocketClient, error)

func (*Client) CreateAPIKey

func (c *Client) CreateAPIKey(req requests.CreateAPIKeyRequest) (*responses.APIKey, error)

CreateAPIKey creates a new API key

func (*Client) CreateDomain

func (c *Client) CreateDomain(domain string) (*responses.Domain, error)

CreateDomain creates a new domain

func (*Client) CreateRoute

func (c *Client) CreateRoute(req requests.CreateRouteRequest) (*responses.Route, error)

CreateRoute creates a new route

func (*Client) CreateSMTPCredential

func (c *Client) CreateSMTPCredential(req requests.CreateSMTPCredentialRequest) (*responses.SMTPCredential, error)

CreateSMTPCredential creates a new SMTP credential

func (*Client) CreateSuppression

CreateSuppression creates a new suppression

func (*Client) CreateWebhook

func (c *Client) CreateWebhook(req requests.CreateWebhookRequest) (*responses.Webhook, error)

CreateWebhook creates a new webhook

func (*Client) CreateWebhookVerifier

func (c *Client) CreateWebhookVerifier(secret string) (*webhooks.WebhookVerifier, error)

CreateWebhookVerifier creates a webhook verifier for the given secret

func (*Client) DeleteAPIKey

func (c *Client) DeleteAPIKey(keyID string) (*common.SuccessResponse, error)

DeleteAPIKey deletes an API key

func (*Client) DeleteDomain

func (c *Client) DeleteDomain(domain string) (*common.SuccessResponse, error)

DeleteDomain deletes a domain

func (*Client) DeleteRoute

func (c *Client) DeleteRoute(routeID string) error

DeleteRoute deletes a route

func (*Client) DeleteSMTPCredential

func (c *Client) DeleteSMTPCredential(credentialID string) error

DeleteSMTPCredential deletes an SMTP credential by ID

func (*Client) DeleteSuppression

func (c *Client) DeleteSuppression(email string, domain *string) (*common.SuccessResponse, error)

DeleteSuppression deletes a suppression by email and optional domain

func (*Client) DeleteWebhook

func (c *Client) DeleteWebhook(webhookID string) error

DeleteWebhook deletes a webhook

func (*Client) GetAPIKey

func (c *Client) GetAPIKey(keyID string) (*responses.APIKey, error)

GetAPIKey retrieves a specific API key by ID

func (*Client) GetAccount

func (c *Client) GetAccount() (*responses.Account, error)

GetAccount retrieves account information

func (*Client) GetAccountID

func (c *Client) GetAccountID() string

GetAccountID returns the configured account ID

func (*Client) GetAuthContext

func (c *Client) GetAuthContext() context.Context

GetAuthContext returns the authenticated context

func (*Client) GetBounceStatistics

GetBounceStatistics retrieves bounce statistics

func (*Client) GetDeliverabilityStatistics

GetDeliverabilityStatistics retrieves deliverability statistics

func (*Client) GetDeliveryTimeStatistics

GetDeliveryTimeStatistics retrieves delivery time statistics

func (*Client) GetDomain

func (c *Client) GetDomain(domain string) (*responses.Domain, error)

GetDomain gets a specific domain

func (*Client) GetMessage

func (c *Client) GetMessage(messageID string) (*responses.Message, error)

GetMessage retrieves a single message by ID

func (*Client) GetMessages

GetMessages retrieves messages with filtering and pagination

func (*Client) GetRoute

func (c *Client) GetRoute(routeID string) (*responses.Route, error)

GetRoute retrieves a single route by ID

func (*Client) GetSMTPCredential

func (c *Client) GetSMTPCredential(credentialID string) (*responses.SMTPCredential, error)

GetSMTPCredential gets a specific SMTP credential by ID

func (*Client) GetWebhook

func (c *Client) GetWebhook(webhookID string) (*responses.Webhook, error)

GetWebhook retrieves a single webhook by ID

func (*Client) InitiateRouteStream

func (c *Client) InitiateRouteStream(routeID, recipient string) (*RouteStreamResponse, error)

func (*Client) InitiateWebhookStream

func (c *Client) InitiateWebhookStream(webhookID string) (*WebhookStreamResponse, error)

func (*Client) ListAPIKeys

func (c *Client) ListAPIKeys(limit *int32, cursor *string) (*responses.PaginatedAPIKeysResponse, error)

ListAPIKeys retrieves a list of API keys

func (*Client) ListDomains

func (c *Client) ListDomains(limit *int32, cursor *string) (*responses.PaginatedDomainsResponse, error)

ListDomains lists domains with pagination @TODO: Add DNSValid parameter

func (*Client) ListRoutes

func (c *Client) ListRoutes(limit *int32, cursor *string) (*responses.PaginatedRoutesResponse, error)

ListRoutes retrieves a paginated list of routes

func (*Client) ListSMTPCredentials

func (c *Client) ListSMTPCredentials(limit *int32, cursor *string) (*responses.PaginatedSMTPCredentialsResponse, error)

ListSMTPCredentials lists all SMTP credentials with pagination

func (*Client) ListSuppressions

ListSuppressions retrieves a paginated list of suppressions

func (*Client) ListWebhooks

func (c *Client) ListWebhooks(limit *int32, cursor *string) (*responses.PaginatedWebhooksResponse, error)

ListWebhooks retrieves all webhooks with pagination support

func (*Client) Ping

func (c *Client) Ping() error

Ping tests the connection and validates the API key

func (*Client) SendMessage

SendMessage sends a message with retry and rate limiting

func (*Client) SendMessageWithIdempotencyKey

func (c *Client) SendMessageWithIdempotencyKey(req requests.CreateMessageRequest, idempotencyKey string) (*responses.CreateMessageResponse, error)

SendMessageWithIdempotencyKey sends a message with idempotency key support

func (*Client) TriggerRoute

func (c *Client) TriggerRoute(routeID string) error

TriggerRoute triggers route events for development testing

func (*Client) TriggerWebhook

func (c *Client) TriggerWebhook(webhookID string, events []string) error

TriggerWebhook triggers webhook events for development testing

func (*Client) UpdateAPIKey

func (c *Client) UpdateAPIKey(keyID string, req requests.UpdateAPIKeyRequest) (*responses.APIKey, error)

UpdateAPIKey updates an existing API key

func (*Client) UpdateRoute

func (c *Client) UpdateRoute(routeID string, req requests.UpdateRouteRequest) (*responses.Route, error)

UpdateRoute updates an existing route

func (*Client) UpdateWebhook

func (c *Client) UpdateWebhook(webhookID string, req requests.UpdateWebhookRequest) (*responses.Webhook, error)

UpdateWebhook updates an existing webhook

func (*Client) ValidateConfiguration

func (c *Client) ValidateConfiguration() error

ValidateConfiguration validates the client configuration

func (*Client) WipeSuppressions

func (c *Client) WipeSuppressions(domain *string) (*common.SuccessResponse, error)

WipeSuppressions deletes all suppressions in the account

type Event

type Event struct {
	Type      string            `json:"type"`
	StreamID  string            `json:"stream_id"`
	AccountID string            `json:"account_id"`
	Data      interface{}       `json:"data"`
	Metadata  map[string]string `json:"metadata"`
	Timestamp int64             `json:"timestamp"`
}

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter wraps the golang rate limiter for API requests

func NewRateLimiter

func NewRateLimiter(rps int, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter rps: requests per second burst: burst capacity

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow() bool

Allow reports whether an event may happen now

func (*RateLimiter) GetWaitTime

func (rl *RateLimiter) GetWaitTime() time.Duration

GetWaitTime returns how long the caller must wait before the next action is permitted

func (*RateLimiter) Reserve

func (rl *RateLimiter) Reserve() *rate.Reservation

Reserve returns a Reservation that indicates how long the caller must wait before the action is permitted

func (*RateLimiter) Wait

func (rl *RateLimiter) Wait(ctx context.Context) error

Wait blocks until the rate limiter permits another request

type RouteStreamResponse

type RouteStreamResponse struct {
	RouteID string `json:"route_id"`
	WsURL   string `json:"ws_url"`
}

type WebSocketClient

type WebSocketClient struct {
	// contains filtered or unexported fields
}

func (*WebSocketClient) Close

func (wsc *WebSocketClient) Close() error

func (*WebSocketClient) ReadMessage

func (wsc *WebSocketClient) ReadMessage(ctx context.Context) (*WebSocketMessage, error)

type WebSocketMessage

type WebSocketMessage struct {
	Type      string `json:"type"`
	Event     *Event `json:"event,omitempty"`
	SessionID string `json:"session_id,omitempty"`
	Stream    string `json:"stream,omitempty"`
	Timestamp int64  `json:"timestamp"`
}

type WebhookStreamResponse

type WebhookStreamResponse struct {
	WebhookID string `json:"webhook_id"`
	WsURL     string `json:"ws_url"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL