Skip to content

Refactor and modernize API - #5

Merged
gmazoyer merged 11 commits into
mainfrom
refactor/modernize-api
Feb 9, 2026
Merged

gmazoyer merged 11 commits into
mainfrom
refactor/modernize-api

Conversation

@gmazoyer

@gmazoyer gmazoyer commented Feb 8, 2026 •

Copy link
Copy Markdown
Owner

Breaking change: all public API methods now require a context.Context as the first parameter and use url.Values instead of map[string]interface{} for search. The constructor is now NewAPI(opts ...Option).

  • Fix resource leak: response bodies are now always closed via defer
  • Fix typos and remove dead code (NetworkContact.Network field tag, unused ErrReadingBody)
  • Add missing CarrierCount field to Facility
  • Extract SocialMedia as a named type instead of an anonymous struct repeated across entities
  • Replace multiple constructors (NewAPI, NewAPIWithAPIKey, NewAPIFromURL, NewAPIFromURLWithAPIKey) with a single NewAPI(opts ...Option) using functional options (WithURL, WithAPIKey, WithHTTPClient)
  • Reuse a single http.Client across requests for connection pooling instead of creating one per call
  • Eliminate per-entity boilerplate with generic fetch[T] and fetchByID[T] helpers
  • Add context.Context to all API methods for cancellation and timeout support
  • Replace map[string]interface{} search parameters with url.Values for type safety — also fixes GET of multiple objets at the same time - PeeringDB API rate limiting #4 (bulk queries like id__in=1,2,3 now work correctly)
  • Return []T instead of *[]T
  • Wrap errors with %w for proper errors.Is/errors.As support
  • Rewrite tests using net/http/httptest with no live API dependency

Close response body on non-200 error paths to prevent resource leaks.
Wrap errors with the original error using %w for better debugging.
Fix constant name networkInternetExchangeLANNamepsace typo, fix
NewAPIWithAPIKey doc comment, remove unreachable ErrBuildingURL check.
The carrier_count field exists in the API schema but was missing from
the struct.
Replace the anonymous struct repeated in every entity with a shared
SocialMedia type defined in api.go.
Replace four constructors with a single NewAPI(opts...) using functional
options (WithURL, WithAPIKey, WithHTTPClient). Store http.Client on the
API struct for connection pooling and configurable timeouts.
Replace per-entity Resource structs, getResource methods, and repetitive
Get/GetAll/GetByID functions with generic fetch[T] and fetchByID[T]
helpers. All public methods now accept context.Context for cancellation
and url.Values for type-safe query parameters, and return []T instead
of *[]T. Tests rewritten with httptest to avoid live API calls.
@gmazoyer
gmazoyer merged commit 19fbae8 into main Feb 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET of multiple objets at the same time - PeeringDB API rate limiting

1 participant