Skip to content

Commit 6c8a999

Browse files
committed
Ability to configure API base
1 parent 50d9ec6 commit 6c8a999

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,34 @@ func main() {
115115

116116
```
117117

118+
# Defining a custom API host
119+
120+
For testing purposes (eg. mocking validation responses in integration tests), you can configure the API base:
121+
122+
```go
123+
package main
124+
125+
import "github.com/teamwork/vat"
126+
127+
func main() {
128+
err := vat.Validate("NL123456789B01", ValidatorOpts{
129+
APIBase: "http://mocker:8080",
130+
})
131+
132+
var ukAccessToken *vat.UKAccessToken
133+
ukAccessToken, _ = vat.GenerateUKAccessToken(vat.ValidatorOpts{
134+
UKClientID: "yourClientID",
135+
UKClientSecret: "yourClientSecret",
136+
IsUKTest: true, // set this if you are testing this in their sandbox test API
137+
})
138+
139+
err = vat.Validate("GB123456789", vat.ValidatorOpts{
140+
UKAccessToken: ukAccessToken.Token,
141+
APIBase: "http://mocker:8080",
142+
})
143+
}
144+
```
145+
118146
## License
119147

120148
MIT licensed. See the LICENSE file for details.

uk_vat_service.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ func (s *ukVATService) Validate(vatNumber string, opts ValidatorOpts) error {
3434
return ErrInvalidCountryCode
3535
}
3636

37+
baseURL := ukVatServiceURL(opts.IsUKTest)
38+
if opts.APIBase != nil {
39+
baseURL = *opts.APIBase
40+
}
41+
3742
apiURL := fmt.Sprintf(
3843
"%s/organisations/vat/check-vat-number/lookup/%s",
39-
ukVatServiceURL(opts.IsUKTest),
44+
baseURL,
4045
vatNumber[2:],
4146
)
4247

validate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ type ValidatorOpts struct {
104104
UKClientSecret string
105105
UKAccessToken *UKAccessToken
106106
IsUKTest bool
107+
APIBase *string
107108
}

vies_service.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ type viesService struct{}
1919

2020
// Validate returns whether the given VAT number is valid or not
2121
// There currently are no VIES options, so the "opts" parameter is here for interface compliance but ignored
22-
func (s *viesService) Validate(vatNumber string, _ ValidatorOpts) error {
22+
func (s *viesService) Validate(vatNumber string, opts ValidatorOpts) error {
2323
if len(vatNumber) < 3 {
2424
return ErrInvalidVATNumberFormat
2525
}
2626

27-
res, err := s.lookup(s.getEnvelope(vatNumber))
27+
serviceURL := viesServiceURL
28+
if opts.APIBase != nil {
29+
serviceURL = *opts.APIBase
30+
}
31+
32+
res, err := s.lookup(serviceURL, s.getEnvelope(vatNumber))
2833
if err != nil {
2934
return ErrServiceUnavailable{Err: err}
3035
}
@@ -105,12 +110,12 @@ func (s *viesService) getEnvelope(n string) string {
105110
}
106111

107112
// lookup calls the VIES service to get info about the VAT number
108-
func (s *viesService) lookup(envelope string) (*http.Response, error) {
113+
func (s *viesService) lookup(serviceURL string, envelope string) (*http.Response, error) {
109114
envelopeBuffer := bytes.NewBufferString(envelope)
110115
client := http.Client{
111116
Timeout: serviceTimeout,
112117
}
113-
return client.Post(viesServiceURL, "text/xml;charset=UTF-8", envelopeBuffer)
118+
return client.Post(serviceURL, "text/xml;charset=UTF-8", envelopeBuffer)
114119
}
115120

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

0 commit comments

Comments
 (0)