diff --git a/servers.go b/servers.go index fd26aef..0c42c67 100644 --- a/servers.go +++ b/servers.go @@ -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 @@ -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) @@ -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 +} diff --git a/servers_test.go b/servers_test.go index 359a6b2..a73670f 100644 --- a/servers_test.go +++ b/servers_test.go @@ -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": "yourhash@inbound.postmarkapp.com", + "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": "yourhash@inbound.postmarkapp.com", + "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, @@ -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()) } @@ -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": "yourhash@inbound.postmarkapp.com", + "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, @@ -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 { @@ -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") + } +}