Skip to content

Commit 54bb0d1

Browse files
Set timeout via a function rather than editing an exported global
1 parent 65149b4 commit 54bb0d1

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

rates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func GetRates() ([]CountryRates, error) {
8383
// FetchRates fetches the latest VAT rates from ibericode/vat-rates and updates the in-memory rates
8484
func FetchRates() ([]CountryRates, error) {
8585
client := http.Client{
86-
Timeout: time.Duration(ViesServiceTimeout) * time.Second,
86+
Timeout: serviceTimeout,
8787
}
8888
r, err := client.Get("https://raw.githubusercontent.com/ibericode/vat-rates/master/vat-rates.json")
8989
if err != nil {

uk_vat_service.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ func (s *ukVATService) Validate(vatNumber string) error {
1919
return ErrInvalidCountryCode
2020
}
2121

22-
response, err := http.Get(fmt.Sprintf(ukVATServiceURL, vatNumber[2:]))
22+
client := http.Client{
23+
Timeout: serviceTimeout,
24+
}
25+
response, err := client.Get(fmt.Sprintf(ukVATServiceURL, vatNumber[2:]))
2326
if err != nil {
2427
return ErrServiceUnavailable{Err: err}
2528
}

vat.go

+9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ Get VAT rate that is currently in effect for a given country
1515
*/
1616
package vat
1717

18+
import "time"
19+
1820
// ViesLookupService is the interface for the VIES VAT number validation service
1921
var ViesLookupService LookupServiceInterface = &viesService{}
2022

2123
// UKVATLookupService is the interface for the UK VAT number validation service
2224
var UKVATLookupService LookupServiceInterface = &ukVATService{}
25+
26+
var serviceTimeout = time.Duration(60) * time.Second
27+
28+
// SetServiceTimeout sets the timeout in seconds for the external VAT lookup services.
29+
func SetServiceTimeout(seconds int) {
30+
serviceTimeout = time.Duration(seconds) * time.Second
31+
}

vies_service.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net/http"
88
"strings"
9-
"time"
109
)
1110

1211
// LookupServiceInterface is an interface for the service that calls external services to validate VATs.
@@ -106,16 +105,13 @@ func (s *viesService) getEnvelope(n string) string {
106105
func (s *viesService) lookup(envelope string) (*http.Response, error) {
107106
envelopeBuffer := bytes.NewBufferString(envelope)
108107
client := http.Client{
109-
Timeout: time.Duration(ViesServiceTimeout) * time.Second,
108+
Timeout: serviceTimeout,
110109
}
111110
return client.Post(viesServiceURL, "text/xml;charset=UTF-8", envelopeBuffer)
112111
}
113112

114113
const viesServiceURL = "https://ec.europa.eu/taxation_customs/vies/services/checkVatService"
115114

116-
// ViesServiceTimeout is the timeout for the VIES service
117-
var ViesServiceTimeout = 10
118-
119115
// viesResponse holds the response data from the Vies call
120116
type viesResponse struct {
121117
CountryCode string

0 commit comments

Comments
 (0)