A practical walkthrough of patterns for building HTTP API clients in Go, using an Infogram API client as a concrete example. Covers five key concepts: composing client options with a configurable struct (custom http.Client, endpoint, credentials), implementing custom JSON marshaling/unmarshaling for non-standard types like url.URL, HMAC-based HTTP request signing, creating small composable convenience methods backed by a shared Do() helper, and returning binary responses as io.Reader for PDF/PNG/HTML formats. The approach emphasizes testability via httptest.Server injection and keeping endpoint methods minimal by centralizing boilerplate.

10m read timeFrom blainsmith.com
Post cover image
Table of contents
Composing Client OptionsJSON Encoding/Decoding to Go TypesHTTP Request SigningConvenience MethodsBinary ReadersConclusion

Sort: