Commit e942f18
[Feat] Refactor: Centralize API Request Logic with Helper Methods (#97)
* Refactor: Centralize API Request Logic with Helper Methods
This commit refactors the Postmark API client to centralize and simplify how API requests are made. The previous implementation involved repetitive boilerplate code for constructing requests, handling authentication, and building URLs with query parameters.
The key changes include:
- A new `helpers.go` file with `buildURL` and `buildURLWithQuery` functions to eliminate duplicate URL construction logic.
- New helper methods on the `Client` struct (`get`, `post`, `put`, `patch`, `delete`) that wrap the underlying `doRequest` function. This provides a cleaner, more expressive, and consistent interface for making API calls. Variants for both server and account tokens have been included.
- The `doRequest` function signature has been simplified, removing the need for the `parameters` struct.
- All resource files (`email.go`, `templates.go`, `bounce.go`, etc.) and test files have been updated to use these new helper methods, significantly reducing code duplication and improving readability.
- Corrected error handling for `DELETE`, `PUT`, and `POST` requests that may return an `ErrorCode` in the body even with a successful HTTP status.
* docs: fix alignment in Key Files table in CLAUDE.md
Adjust the markdown table in the CLAUDE.md file to improve column
alignment and readability. This change ensures consistent spacing and
a cleaner presentation of file purposes in the documentation.
* fix(postmark): improve error wrapping and simplify query check
Use error wrapping (%w) when unmarshalling API errors to preserve original
error context, aiding in better error handling and debugging.
Simplify buildURLWithQuery function by removing redundant nil check for
query parameters, improving code clarity without changing behavior.
* chore: remove unused email-related source files
Remove helpers.go, postmark.go, email.go, sender_signatures.go,
inbound_rules_triggers.go, and data_removals.go as they are no longer
needed. This cleanup reduces code clutter and improves maintainability.
* test: add HTTP server benchmarks for bounce and data removal APIs
Add comprehensive benchmarks for Bounce and Data Removal client
methods using httptest servers. The benchmarks simulate realistic
HTTP responses for endpoints like GetDeliveryStats, GetBounces,
GetBounce, GetBounceDump, ActivateBounce, GetBouncedTags,
CreateDataRemoval, and GetDataRemovalStatus.
This change improves benchmark accuracy by exercising the full
HTTP client logic and request handling rather than isolated code,
enabling more reliable performance assessments and future
optimizations.
* test: enhance benchmarks with HTTP test server mocks
Refactor benchmarks in inbound_rules_triggers_test.go and domains_test.go
to use httptest.NewServer and test routers for mocking API responses.
Add HTTP handlers returning predefined JSON responses for all benchmarked
endpoints, enabling more realistic simulations of client-server interaction.
This improves benchmark accuracy by verifying request handling and response
processing, replacing previous no-op variable usage and manual context setup.
* test: add HTTP mock servers to benchmarks for stable results
Refactor benchmarks to use httptest servers and mock HTTP handlers
for all client calls in stats, messages_outbound, and
sender_signatures packages. This ensures benchmarks run against
controlled, consistent responses rather than live or nil backends.
The change improves benchmark reliability, removes dependencies on
external services, and enables more accurate performance measurement
of client methods by simulating realistic API responses.
* docs: add detailed API client performance results to README
Include comprehensive benchmark results measuring real API client
performance on Apple M1 Max, covering latency, throughput, memory
usage, and allocations for all major API operations. Add expandable
sections with detailed per-operation metrics to provide transparency
and help users understand the efficiency and speed of the client.
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>1 parent 6a31d3a commit e942f18
File tree
30 files changed
+1419
-897
lines changed- .github
30 files changed
+1419
-897
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
407 | 407 | | |
408 | 408 | | |
409 | 409 | | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
410 | 555 | | |
411 | 556 | | |
412 | 557 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | | - | |
8 | | - | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
| |||
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 31 | + | |
40 | 32 | | |
41 | 33 | | |
42 | 34 | | |
| |||
89 | 81 | | |
90 | 82 | | |
91 | 83 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 84 | + | |
| 85 | + | |
98 | 86 | | |
99 | 87 | | |
100 | | - | |
| 88 | + | |
| 89 | + | |
101 | 90 | | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 91 | + | |
107 | 92 | | |
108 | 93 | | |
109 | 94 | | |
110 | 95 | | |
111 | 96 | | |
112 | 97 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 98 | + | |
119 | 99 | | |
120 | 100 | | |
121 | 101 | | |
| |||
126 | 106 | | |
127 | 107 | | |
128 | 108 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 109 | + | |
135 | 110 | | |
136 | 111 | | |
137 | 112 | | |
| |||
145 | 120 | | |
146 | 121 | | |
147 | 122 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
| 123 | + | |
154 | 124 | | |
155 | 125 | | |
156 | 126 | | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | 127 | | |
162 | 128 | | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
181 | 132 | | |
0 commit comments