diff --git a/config/config.exs b/config/config.exs index d3e0fa4..3c2e23e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,6 +1,6 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. -use Mix.Config +import Config # This configuration is loaded before any dependency and is restricted # to this project. If another project depends on this project, this diff --git a/fixture/vcr_cassettes/lookup_requests_limit_reached.json b/fixture/vcr_cassettes/lookup_requests_limit_reached.json new file mode 100644 index 0000000..81ef3d3 --- /dev/null +++ b/fixture/vcr_cassettes/lookup_requests_limit_reached.json @@ -0,0 +1,31 @@ +[ + { + "request": { + "body": "\n \n \n \n NL\n 9999999\n \n \n ", + "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:ServerMS_MAX_CONCURRENT_REQ", + "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" + } + } +] \ No newline at end of file diff --git a/lib/viex.ex b/lib/viex.ex index 85201ca..ac9447c 100644 --- a/lib/viex.ex +++ b/lib/viex.ex @@ -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 [ diff --git a/mix.exs b/mix.exs index 076be60..32d9a60 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Viex.Mixfile do def project do [ app: :viex, - version: "0.3.1", + version: "0.3.2", elixir: "~> 1.4", description: "Elixir package to validate European VAT numbers with the VIES service.", build_embedded: Mix.env() == :prod, diff --git a/test/viex_test.exs b/test/viex_test.exs index 8dbcd18..ad9ec73 100644 --- a/test/viex_test.exs +++ b/test/viex_test.exs @@ -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