Skip to content

Commit

Permalink
Allow VIES Service timeout to be editable (#7)
Browse files Browse the repository at this point in the history
* Allow VIES Service timeout to be editable

* Set timeout via a function rather than editing an exported global

* Accept a duration rather than a number of seconds for service timeout
  • Loading branch information
chrisrollins65 authored Jun 13, 2024
1 parent 3963391 commit 0225b8a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetRates() ([]CountryRates, error) {
// FetchRates fetches the latest VAT rates from ibericode/vat-rates and updates the in-memory rates
func FetchRates() ([]CountryRates, error) {
client := http.Client{
Timeout: time.Duration(ViesServiceTimeout) * time.Second,
Timeout: serviceTimeout,
}
r, err := client.Get("https://raw.githubusercontent.com/ibericode/vat-rates/master/vat-rates.json")
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion uk_vat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func (s *ukVATService) Validate(vatNumber string) error {
return ErrInvalidCountryCode
}

response, err := http.Get(fmt.Sprintf(ukVATServiceURL, vatNumber[2:]))
client := http.Client{
Timeout: serviceTimeout,
}
response, err := client.Get(fmt.Sprintf(ukVATServiceURL, vatNumber[2:]))
if err != nil {
return ErrServiceUnavailable{Err: err}
}
Expand Down
9 changes: 9 additions & 0 deletions vat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ Get VAT rate that is currently in effect for a given country
*/
package vat

import "time"

// ViesLookupService is the interface for the VIES VAT number validation service
var ViesLookupService LookupServiceInterface = &viesService{}

// UKVATLookupService is the interface for the UK VAT number validation service
var UKVATLookupService LookupServiceInterface = &ukVATService{}

var serviceTimeout = time.Second * 60

// SetServiceTimeout sets the timeout for the external VAT lookup services.
func SetServiceTimeout(timeout time.Duration) {
serviceTimeout = timeout
}
6 changes: 1 addition & 5 deletions vies_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/http"
"strings"
"time"
)

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

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

// ViesServiceTimeout is the timeout for the VIES service
const ViesServiceTimeout = 10

// viesResponse holds the response data from the Vies call
type viesResponse struct {
CountryCode string
Expand Down

0 comments on commit 0225b8a

Please sign in to comment.