Skip to content

Commit

Permalink
Return too many requests error
Browse files Browse the repository at this point in the history
  • Loading branch information
siepet committed Dec 18, 2023
1 parent 852e059 commit be66748
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/lookup_requests_limit_reached.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"request": {
"body": "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">\n <soapenv:Header/>\n <soapenv:Body>\n <urn:checkVat>\n <urn:countryCode>NL</urn:countryCode>\n <urn:vatNumber>9999999</urn:vatNumber>\n </urn:checkVat>\n </soapenv:Body>\n </soapenv:Envelope>",
"headers": {
"SOAPAction": "",
"Content-Type": "text/xml;charset=UTF-8"
},
"method": "post",
"options": [],
"request_body": "",
"url": "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"
},
"response": {
"binary": false,
"body": "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Header/><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>MS_MAX_CONCURRENT_REQ</faultstring></env:Fault></env:Body></env:Envelope>",
"headers": {
"Date": "Fri, 11 Aug 2023 11:23:11 GMT",
"Content-Length": "221",
"Content-Type": "text/xml; charset=UTF-8",
"SOAPAction": "\"\"",
"Accept": "text/xml",
"Server": "Europa",
"Proxy-Connection": "Keep-Alive",
"Connection": "keep-alive"
},
"status_code": 200,
"type": "ok"
}
}
]
8 changes: 6 additions & 2 deletions lib/viex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ defmodule Viex do
defp handle_soap_response({:ok, %HTTPoison.Response{status_code: 500}}),
do: {:error, :internal_server_error}

defp handle_soap_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}),
do: {:ok, body}
defp handle_soap_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}) do
cond do
String.contains?(body, "MS_MAX_CONCURRENT_REQ") -> {:error, :too_many_requests}
true -> {:ok, body}
end
end

defp headers() do
[
Expand Down
8 changes: 8 additions & 0 deletions test/viex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ defmodule ViexTest do
end
end

test "lookup with requests limit reached" do
use_cassette "lookup_requests_limit_reached" do
response = Viex.lookup("NL9999999")

assert response == {:error, :too_many_requests}
end
end

test "valid?" do
use_cassette "valid" do
assert Viex.valid?("NL854265259B01") == true
Expand Down

0 comments on commit be66748

Please sign in to comment.