Skip to content

Commit

Permalink
Merge pull request #16 from ScrapingAnt/feature/issue14-handle-423-st…
Browse files Browse the repository at this point in the history
…atus-code

feature/issue14-handle-423-status-code: done
  • Loading branch information
megabotan authored Aug 24, 2021
2 parents 04b9f20 + 3486300 commit dca85cd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Class defining response from API.
| ScrapingantInvalidInputException | Invalid value provided. Please, look into error message for more info |
| ScrapingantInternalException | Something went wrong with the server side code. Try again later or contact ScrapingAnt support |
| ScrapingantSiteNotReachableException | The requested URL is not reachable. Please, check it locally |
| ScrapingantDetectedException | The anti-bot detection system has detected the request. Please, retry or change the request settings. |

* * *

Expand Down
4 changes: 3 additions & 1 deletion scrapingant_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.4"
__version__ = "0.3.5"

from scrapingant_client.client import ScrapingAntClient
from scrapingant_client.cookie import Cookie
Expand All @@ -8,6 +8,7 @@
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
ScrapingantDetectedException,
)
from scrapingant_client.response import Response

Expand All @@ -19,5 +20,6 @@
'ScrapingantInvalidInputException',
'ScrapingantInternalException',
'ScrapingantSiteNotReachableException',
'ScrapingantDetectedException',
'Response',
]
7 changes: 5 additions & 2 deletions scrapingant_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
ScrapingantDetectedException,
)
from scrapingant_client.response import Response
from scrapingant_client.utils import base64_encode_string
Expand Down Expand Up @@ -53,10 +54,12 @@ def general_request(
)
if response.status_code == 403:
raise ScrapingantInvalidTokenException()
elif response.status_code == 422:
raise ScrapingantInvalidInputException(response.text)
elif response.status_code == 404:
raise ScrapingantSiteNotReachableException(url)
elif response.status_code == 422:
raise ScrapingantInvalidInputException(response.text)
elif response.status_code == 423:
raise ScrapingantDetectedException()
elif response.status_code == 500:
raise ScrapingantInternalException()
json_response = response.json()
Expand Down
7 changes: 7 additions & 0 deletions scrapingant_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def __init__(self, url):
super().__init__(message)


class ScrapingantDetectedException(ScrapingantClientException):
def __init__(self):
message = 'The anti-bot detection system has detected the request. ' \
'Please, retry or change the request settings.'
super().__init__(message)


class ScrapingantInternalException(ScrapingantClientException):
def __init__(self):
message = 'Something went wrong with the server side. Please try later or contact support'
Expand Down
11 changes: 11 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
ScrapingantDetectedException,
)
from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL

Expand Down Expand Up @@ -47,3 +48,13 @@ def test_not_reachable():
with pytest.raises(ScrapingantSiteNotReachableException) as e:
client.general_request('example.com')
assert 'The requested URL is not reachable (example.com)' in str(e)


@responses.activate
def test_detected():
responses.add(responses.POST, SCRAPINGANT_API_BASE_URL + '/general',
json={}, status=423)
client = ScrapingAntClient(token='some_token')
with pytest.raises(ScrapingantDetectedException) as e:
client.general_request('example.com')
assert 'The anti-bot detection system has detected the request' in str(e)

0 comments on commit dca85cd

Please sign in to comment.