Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions capmonstercloud_client/CapMonsterCloudClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ async def _solve(self, request: Union[
elif getResultResponse.get('status') == 'ready':
timer.stop()
result.solution = getResultResponse.get('solution')
result.cost = getResultResponse.get('cost')
if result.cost is not None and result.solution is not None:
return {**result.solution, 'cost': result.cost}
return result.solution

raise TimeoutError('Failed to get a solution within the maximum ' \
Expand Down
1 change: 1 addition & 0 deletions capmonstercloud_client/captchaResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class CaptchaResult(BaseModel):
solution: Optional[Dict[str, str]] = None
cost: Optional[float] = None

def __eq__(self, other: Any) -> bool:
return self.solution == other
Expand Down
2 changes: 1 addition & 1 deletion capmonstercloud_client/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.4.0
4.4.1
43 changes: 43 additions & 0 deletions test/cost_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import asyncio
import unittest
from unittest.mock import AsyncMock, patch

from capmonstercloudclient.requests import TurnstileRequest
from capmonstercloudclient import CapMonsterClient, ClientOptions
from capmonstercloudclient.captchaResult import CaptchaResult


class CostTest(unittest.TestCase):
def _solve(self, result_response):
client = CapMonsterClient(options=ClientOptions(api_key="YOUR_API_KEY"))
request = TurnstileRequest(
websiteURL="https://example.com/", websiteKey="key123"
)
with patch.object(
CapMonsterClient, "_createTask",
new=AsyncMock(return_value={"errorId": 0, "taskId": 1}),
), patch.object(
CapMonsterClient, "_getTaskResult",
new=AsyncMock(return_value=result_response),
):
return asyncio.run(client.solve_captcha(request))

def test_captcha_result_cost(self):
self.assertEqual(CaptchaResult(solution={"token": "t"}, cost=0.0003).cost, 0.0003)
self.assertIsNone(CaptchaResult(solution={"token": "t"}).cost)

def test_cost_returned_with_solution(self):
response = self._solve(
{"errorId": 0, "status": "ready", "cost": 0.0003, "solution": {"token": "t"}}
)
self.assertEqual(response, {"token": "t", "cost": 0.0003})

def test_no_cost_in_response(self):
response = self._solve(
{"errorId": 0, "status": "ready", "solution": {"token": "t"}}
)
self.assertEqual(response, {"token": "t"})


if __name__ == "__main__":
unittest.main()
Loading