From 1c5bd4adaad54b66739da1a130338559b9222810 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sat, 23 Nov 2024 17:09:32 +0100 Subject: [PATCH] Default HTTP client should have timeout (#47) --- resend.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/resend.go b/resend.go index 835f847..a0d03d9 100644 --- a/resend.go +++ b/resend.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "strings" + "time" ) const ( @@ -19,6 +20,10 @@ const ( var defaultBaseURL = getEnv("RESEND_BASE_URL", "https://api.resend.com/") +var defaultHTTPClient = &http.Client{ + Timeout: time.Minute, +} + // Client handles communication with Resend API. type Client struct { // HTTP client @@ -48,13 +53,13 @@ type Client struct { // NewClient is the default client constructor func NewClient(apiKey string) *Client { key := strings.Trim(strings.TrimSpace(apiKey), "'") - return NewCustomClient(http.DefaultClient, key) + return NewCustomClient(defaultHTTPClient, key) } // NewCustomClient builds a new Resend API client, using a provided Http client. func NewCustomClient(httpClient *http.Client, apiKey string) *Client { if httpClient == nil { - httpClient = http.DefaultClient + httpClient = defaultHTTPClient } baseURL, _ := url.Parse(defaultBaseURL)