Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timeout to Client #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions namecheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"net/url"
"strconv"
"strings"
"time"
)

const defaultBaseURL = "https://api.namecheap.com/xml.response"
const defaultTimeout = 0

// Client represents a client used to make calls to the Namecheap API.
type Client struct {
Expand All @@ -23,6 +25,10 @@ type Client struct {
UserName string
HttpClient *http.Client

// Timeout specifies a time limit for requests made by this
// Client. Default value is zero, which means no timeout.
Timeout time.Duration

// Base URL for API requests.
// Defaults to the public Namecheap API,
// but can be set to a different endpoint (e.g. the sandbox).
Expand Down Expand Up @@ -90,6 +96,7 @@ func NewClient(apiUser, apiToken, userName string) *Client {
UserName: userName,
HttpClient: http.DefaultClient,
BaseURL: defaultBaseURL,
Timeout: defaultTimeout,
}
}

Expand Down Expand Up @@ -161,6 +168,8 @@ func (client *Client) sendRequest(request *ApiRequest) ([]byte, int, error) {
return nil, 0, err
}

client.HttpClient.Timeout = client.Timeout

resp, err := client.HttpClient.Do(req)
if err != nil {
return nil, 0, err
Expand Down