Skip to content

Commit

Permalink
feat: Add server delete request
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarey1590 committed Jan 12, 2025
1 parent f53db65 commit f36b361
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 6 deletions.
24 changes: 20 additions & 4 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ func (s Server) MarshalJSON() ([]byte, error) {
}

// GetServer fetches a specific server via serverID
func (client *Client) GetServer(ctx context.Context, serverID string) (Server, error) {
func (client *Client) GetServer(ctx context.Context, serverID int64) (Server, error) {
res := Server{}
err := client.doRequest(ctx, parameters{
Method: http.MethodGet,
Path: fmt.Sprintf("servers/%s", serverID),
Path: fmt.Sprintf("servers/%d", serverID),
TokenType: accountToken,
}, &res)
return res, err
Expand Down Expand Up @@ -199,11 +199,11 @@ func (client *Client) GetServers(ctx context.Context, count, offset int64, name
}

// EditServer updates details for a specific server with serverID
func (client *Client) EditServer(ctx context.Context, serverID string, request ServerEditRequest) (Server, error) {
func (client *Client) EditServer(ctx context.Context, serverID int64, request ServerEditRequest) (Server, error) {
res := Server{}
err := client.doRequest(ctx, parameters{
Method: http.MethodPut,
Path: fmt.Sprintf("servers/%s", serverID),
Path: fmt.Sprintf("servers/%d", serverID),
TokenType: accountToken,
Payload: request,
}, &res)
Expand All @@ -221,3 +221,19 @@ func (client *Client) CreateServer(ctx context.Context, request ServerCreateRequ
}, &res)
return res, err
}

// DeleteServer removes a server.
func (client *Client) DeleteServer(ctx context.Context, serverID int64) error {
res := APIError{}
err := client.doRequest(ctx, parameters{
Method: http.MethodDelete,
Path: fmt.Sprintf("servers/%d", serverID),
TokenType: accountToken,
}, &res)

if res.ErrorCode != 0 {
return res
}

return err
}
160 changes: 158 additions & 2 deletions servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,83 @@ import (
"goji.io/pat"
)

func TestGetServers(t *testing.T) {
responseJSON := `{
"TotalCount": 2,
"Servers": [
{
"ID": 1,
"Name": "Production01",
"ApiTokens": [
"server token"
],
"Color": "red",
"SmtpApiActivated": true,
"RawEmailEnabled": false,
"DeliveryType": "Live",
"ServerLink": "https://postmarkapp.com/servers/1/streams",
"InboundAddress": "[email protected]",
"InboundHookUrl": "http://inboundhook.example.com/inbound",
"BounceHookUrl": "http://bouncehook.example.com/bounce",
"OpenHookUrl": "http://openhook.example.com/open",
"DeliveryHookUrl": "http://hooks.example.com/delivery",
"PostFirstOpenOnly": true,
"InboundDomain": "",
"InboundHash": "yourhash",
"InboundSpamThreshold": 5,
"TrackOpens": false,
"TrackLinks": "None",
"IncludeBounceContentInHook": true,
"ClickHookUrl": "http://hooks.example.com/click",
"EnableSmtpApiErrorHooks": false
},
{
"ID": 2,
"Name": "Production02",
"ApiTokens": [
"server token"
],
"Color": "green",
"SmtpApiActivated": true,
"RawEmailEnabled": false,
"DeliveryType": "Sandbox",
"ServerLink": "https://postmarkapp.com/servers/2/streams",
"InboundAddress": "[email protected]",
"InboundHookUrl": "",
"BounceHookUrl": "",
"OpenHookUrl": "",
"DeliveryHookUrl": "http://hooks.example.com/delivery",
"PostFirstOpenOnly": false,
"InboundDomain": "",
"InboundHash": "yourhash",
"InboundSpamThreshold": 0,
"TrackOpens": true,
"TrackLinks": "HtmlAndText",
"IncludeBounceContentInHook": false,
"ClickHookUrl": "",
"EnableSmtpApiErrorHooks": false
}
]
}`

tMux.HandleFunc(pat.Get("/servers"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})

res, err := client.GetServers(context.Background(), 100, 10, "")
if err != nil {
t.Fatalf("GetServers: %s", err.Error())
}

if len(res.Servers) == 0 {
t.Fatalf("GetServers: unmarshaled to empty")
}

if res.TotalCount != 2 {
t.Fatalf("GetServers: unmarshaled to empty")
}
}

func TestGetServer(t *testing.T) {
responseJSON := `{
"ID": 1,
Expand All @@ -34,7 +111,7 @@ func TestGetServer(t *testing.T) {
_, _ = w.Write([]byte(responseJSON))
})

res, err := client.GetServer(context.Background(), "1")
res, err := client.GetServer(context.Background(), 1)
if err != nil {
t.Fatalf("GetServer: %s", err.Error())
}
Expand All @@ -44,6 +121,57 @@ func TestGetServer(t *testing.T) {
}
}

func TestCreateServer(t *testing.T) {
responseJSON := `{
"ID": 1,
"Name": "Staging Testing",
"ApiTokens": [
"server token"
],
"Color": "red",
"SmtpApiActivated": true,
"RawEmailEnabled": false,
"DeliveryType": "Live",
"ServerLink": "https://postmarkapp.com/servers/1/streams",
"InboundAddress": "[email protected]",
"InboundHookUrl": "http://hooks.example.com/inbound",
"PostFirstOpenOnly": false,
"InboundDomain": "",
"InboundHash": "yourhash",
"InboundSpamThreshold": 5,
"TrackOpens": false,
"TrackLinks": "None",
"IncludeBounceContentInHook": true,
"EnableSmtpApiErrorHooks": false
}`

tMux.HandleFunc(pat.Post("/servers"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})

res, err := client.CreateServer(context.Background(), ServerCreateRequest{
Name: "Staging Testing",
Color: "red",
SMTPAPIActivated: true,
RawEmailEnabled: false,
InboundHookURL: "http://hooks.example.com/inbound",
PostFirstOpenOnly: false,
InboundDomain: "",
InboundSpamThreshold: 5,
TrackOpens: false,
TrackLinks: "None",
IncludeBounceContentInHook: true,
EnableSMTPAPIErrorHooks: false,
})
if err != nil {
t.Fatalf("CreateServer: %s", err.Error())
}

if res.Name != "Staging Testing" {
t.Fatalf("CreateServer: wrong name!")
}
}

func TestEditServer(t *testing.T) {
responseJSON := `{
"ID": 1,
Expand All @@ -70,7 +198,7 @@ func TestEditServer(t *testing.T) {
_, _ = w.Write([]byte(responseJSON))
})

res, err := client.EditServer(context.Background(), "1234", ServerEditRequest{
res, err := client.EditServer(context.Background(), 1234, ServerEditRequest{
Name: "Production Testing",
})
if err != nil {
Expand All @@ -81,3 +209,31 @@ func TestEditServer(t *testing.T) {
t.Fatalf("EditServer: wrong name!: %s", res.Name)
}
}

func TestDeleteServer(t *testing.T) {
responseJSON := `{
"ErrorCode": 0,
"Message": "Server 1234 removed."
}`

tMux.HandleFunc(pat.Delete("/servers/:serverID"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})

// Success
err := client.DeleteServer(context.Background(), 1234)
if err != nil {
t.Fatalf("DeleteServer: %s", err.Error())
}

// Failure
responseJSON = `{
"ErrorCode": 402,
"Message": "Invalid JSON"
}`

err = client.DeleteServer(context.Background(), 1234)
if err == nil {
t.Fatalf("DeleteServer: should have failed")
}
}

0 comments on commit f36b361

Please sign in to comment.