Skip to content

Commit

Permalink
Merge pull request #4 from Recruitee/return-concurrency-error
Browse files Browse the repository at this point in the history
Return `too_many_requests` for `MS_MAX_CONCURRENT_REQ` error
  • Loading branch information
siepet authored Dec 18, 2023
2 parents 852e059 + 9f54a24 commit aee8fd4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 aee8fd4

Please sign in to comment.