Skip to content

Commit

Permalink
Merge pull request #66 from sanchitrk/master
Browse files Browse the repository at this point in the history
feat: added support for create server API and updated Server data model.
  • Loading branch information
mrz1836 authored Dec 16, 2024
2 parents 5bedea8 + 9ccb6da commit ad34971
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ package postmark

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// Server represents a server registered in your Postmark account
type Server struct {
// ID of server
ID int64
ID int64 `json:"ID"`
// Name of server
Name string
Name string `json:"Name"`
// APITokens associated with server.
APITokens []string `json:"ApiTokens"`
// ServerLink to your server overview page in Postmark.
ServerLink string
ServerLink string `json:"ServerLink"`
// Color of the server in the rack screen. Purple Blue Turquoise Green Red Yellow Grey
Color string
Color string `json:"Color"`
// SMTPAPIActivated specifies whether SMTP is enabled on this server.
SMTPAPIActivated bool `json:"SmtpApiActivated"`
// RawEmailEnabled allows raw email to be sent with inbound.
RawEmailEnabled bool
RawEmailEnabled bool `json:"RawEmailEnabled"`
// DeliveryType specifies the type of environment for your server: Live or Sandbox, defaults to Live
DeliveryType string `json:"DeliveryType"`
// InboundAddress is the inbound email address
InboundAddress string
InboundAddress string `json:"InboundAddress"`
// InboundHookURL to POST to every time an inbound event occurs.
InboundHookURL string `json:"InboundHookUrl"`
// BounceHookURL to POST to every time a bounce event occurs.
Expand All @@ -32,15 +35,48 @@ type Server struct {
OpenHookURL string `json:"OpenHookUrl"`
// PostFirstOpenOnly - If set to true, only the first open by a particular recipient will initiate the open webhook. Any
// subsequent opens of the same email by the same recipient will not initiate the webhook.
PostFirstOpenOnly bool
PostFirstOpenOnly bool `json:"PostFirstOpenOnly"`
// TrackOpens indicates if all emails being sent through this server have open tracking enabled.
TrackOpens bool
TrackOpens bool `json:"TrackOpens"`
// TrackLinks specifies link tracking in emails: None, HtmlAndText, HtmlOnly, TextOnly, defaults to "None"
TrackLinks string `json:"TrackLinks"`
// IncludeBounceContentInHook determines if bounce content is included in webhook.
IncludeBounceContentInHook bool `json:"IncludeBounceContentInHook"`
// InboundDomain is the inbound domain for MX setup
InboundDomain string
InboundDomain string `json:"InboundDomain"`
// InboundHash is the inbound hash of your inbound email address.
InboundHash string
InboundHash string `json:"InboundHash"`
// InboundSpamThreshold is the maximum spam score for an inbound message before it's blocked.
InboundSpamThreshold int64
InboundSpamThreshold int64 `json:"InboundSpamThreshold"`
// EnableSMTPAPIErrorHooks specifies whether SMTP API Errors will be included with bounce webhooks.
EnableSMTPAPIErrorHooks bool `json:"EnableSmtpApiErrorHooks"`
}

// MarshalJSON customizes the JSON representation of the Server struct by setting default values for specific fields.
func (s Server) MarshalJSON() ([]byte, error) {
type Aux Server

// If TrackLinks is empty, set it to "None"
trackLinks := s.TrackLinks
if trackLinks == "" {
trackLinks = "None"
}

// If DeliveryType is empty, set it to default value "Live"
deliveryType := s.DeliveryType
if deliveryType == "" {
deliveryType = "Live"
}

return json.Marshal(&struct {
Aux
TrackLinks string `json:"TrackLinks"`
DeliveryType string `json:"DeliveryType"`
}{
Aux: Aux(s),
TrackLinks: trackLinks,
DeliveryType: deliveryType,
})
}

// GetServer fetches a specific server via serverID
Expand All @@ -64,3 +100,15 @@ func (client *Client) EditServer(ctx context.Context, serverID string, server Se
}, &server)
return server, err
}

// CreateServer creates a server
func (client *Client) CreateServer(ctx context.Context, server Server) (Server, error) {
res := Server{}
err := client.doRequest(ctx, parameters{
Method: http.MethodPost,
Path: "servers",
TokenType: accountToken,
Payload: server,
}, &res)
return res, err
}

0 comments on commit ad34971

Please sign in to comment.