|
| 1 | +# CLAUDE.md - Postmark Go Library |
| 2 | + |
| 3 | +## 🎯 Quick Start |
| 4 | + |
| 5 | +**Postmark Go Library** - Unofficial Go client for Postmark email API with comprehensive coverage and minimal dependencies. |
| 6 | + |
| 7 | +**Key Info:** |
| 8 | +- Go 1.18+, single dependency: `testify` for testing |
| 9 | +- Context-aware API with Server/Account token auth |
| 10 | +- 38 Go files covering complete Postmark API surface |
| 11 | +- Test-driven with custom TestRouter for HTTP mocking |
| 12 | + |
| 13 | +## 🏗️ Architecture & Patterns |
| 14 | + |
| 15 | +### Client Structure |
| 16 | +```go |
| 17 | +type Client struct { |
| 18 | + HTTPClient *http.Client |
| 19 | + ServerToken string // Server-level operations |
| 20 | + AccountToken string // Account-level operations |
| 21 | + BaseURL string // API endpoint |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### API Implementation Pattern |
| 26 | +Every API endpoint follows this consistent pattern: |
| 27 | +```go |
| 28 | +func (client *Client) MethodName(ctx context.Context, payload PayloadType) (ResponseType, error) { |
| 29 | + res := ResponseType{} |
| 30 | + err := client.doRequest(ctx, parameters{ |
| 31 | + Method: "POST|GET|PUT|DELETE", |
| 32 | + Path: "api/endpoint", |
| 33 | + Payload: payload, // nil for GET |
| 34 | + TokenType: serverToken, // or accountToken |
| 35 | + }, &res) |
| 36 | + return res, err |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### File Organization |
| 41 | +- `postmark.go` - Core Client, doRequest, error handling |
| 42 | +- `email.go` - Send emails, batch operations |
| 43 | +- `templates.go` - Template management and templated emails |
| 44 | +- `data_removals.go` - GDPR data removal requests |
| 45 | +- `webhooks.go` - Webhook configuration |
| 46 | +- `bounce.go`, `domains.go`, `message_streams.go`, etc. - Specialized APIs |
| 47 | + |
| 48 | +## 🧪 Testing Patterns |
| 49 | + |
| 50 | +### Test Structure (using testify suite) |
| 51 | +```go |
| 52 | +func (s *PostmarkTestSuite) TestMethodName() { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + responseJSON string |
| 56 | + wantErr bool |
| 57 | + // expected fields |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "successful operation", |
| 61 | + responseJSON: `{"field": "value"}`, |
| 62 | + wantErr: false, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + s.Run(tt.name, func() { |
| 68 | + s.mux.Post("/endpoint", func(w http.ResponseWriter, _ *http.Request) { |
| 69 | + _, _ = w.Write([]byte(tt.responseJSON)) |
| 70 | + }) |
| 71 | + |
| 72 | + result, err := s.client.MethodName(context.Background(), payload) |
| 73 | + |
| 74 | + if tt.wantErr { |
| 75 | + s.Require().Error(err) |
| 76 | + } else { |
| 77 | + s.Require().NoError(err) |
| 78 | + // assertions |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### TestRouter Usage |
| 86 | +- `s.mux.Get()`, `s.mux.Post()`, `s.mux.Put()`, `s.mux.Delete()` |
| 87 | +- Supports path parameters: `/domains/:domainID` |
| 88 | +- Custom TestRouter in `test_router.go` |
| 89 | + |
| 90 | +## 🛠️ Common Tasks |
| 91 | + |
| 92 | +### Adding New API Endpoint |
| 93 | +1. **Create struct types** for request/response in appropriate file |
| 94 | +2. **Add method** following the standard pattern |
| 95 | +3. **Write tests** using TestRouter pattern |
| 96 | +4. **Update README.md** API coverage checklist if needed |
| 97 | + |
| 98 | +### JSON Struct Guidelines |
| 99 | +- Use `json:"fieldName,omitempty"` for optional fields |
| 100 | +- Match Postmark API field names exactly |
| 101 | +- Use proper Go naming (e.g., `HTMLBody` for `HtmlBody`) |
| 102 | +- Add struct tags for ID fields: `json:"TemplateID"` |
| 103 | + |
| 104 | +### Error Handling |
| 105 | +- API errors use `APIError` struct with ErrorCode and Message |
| 106 | +- Context cancellation supported throughout |
| 107 | +- Method-specific errors like `ErrEmailFailed` |
| 108 | + |
| 109 | +## 🔧 Build & Test Commands |
| 110 | + |
| 111 | +**Using MAGE-X:** |
| 112 | +```bash |
| 113 | +magex test # Run tests (fast) |
| 114 | +magex test:race # Run with race detector |
| 115 | +magex bench # Run benchmarks |
| 116 | +magex help # View all commands |
| 117 | +``` |
| 118 | + |
| 119 | +## 📁 Key Files |
| 120 | + |
| 121 | +| File | Purpose | |
| 122 | +|------|---------| |
| 123 | +| `postmark.go` | Core client, doRequest method, auth headers | |
| 124 | +| `email.go` | Email sending, batch operations | |
| 125 | +| `templates.go` | Template CRUD, templated email sending | |
| 126 | +| `data_removals.go` | GDPR data removal API | |
| 127 | +| `webhooks.go` | Webhook management | |
| 128 | +| `test_router.go` | Custom HTTP router for testing | |
| 129 | +| `examples/examples.go` | Usage examples for all major features | |
| 130 | + |
| 131 | +## ⚠️ Important Notes |
| 132 | + |
| 133 | +- **Token Types**: Use `serverToken` (default) or `accountToken` based on API requirements |
| 134 | +- **Context**: Always pass context.Context as first parameter |
| 135 | +- **Testing**: Use `PostmarkTestSuite` pattern, not standalone tests |
| 136 | +- **Dependencies**: Keep minimal - only add if absolutely necessary |
| 137 | +- **API Coverage**: This library has near-complete Postmark API coverage |
| 138 | +- **Conventions**: Follow existing patterns exactly for consistency |
| 139 | + |
| 140 | +## 🚀 Examples |
| 141 | + |
| 142 | +**Send Email:** |
| 143 | +```go |
| 144 | +client := postmark.NewClient("[SERVER-TOKEN]", "[ACCOUNT-TOKEN]") |
| 145 | +email := postmark.Email{ |
| 146 | + From: "no-reply@example.com", To: "user@example.com", |
| 147 | + Subject: "Test", HTMLBody: "<p>Hello</p>", TrackOpens: true, |
| 148 | +} |
| 149 | +res, err := client.SendEmail(context.Background(), email) |
| 150 | +``` |
| 151 | + |
| 152 | +**Create Template:** |
| 153 | +```go |
| 154 | +template := postmark.Template{ |
| 155 | + Name: "Welcome", Subject: "Welcome {{name}}!", |
| 156 | + HTMLBody: "<p>Hello {{name}}!</p>", Alias: "welcome-template", |
| 157 | +} |
| 158 | +res, err := client.CreateTemplate(context.Background(), template) |
| 159 | +``` |
0 commit comments