add new action: answerCards

This commit is contained in:
Antistic 2023-06-11 22:57:08 +01:00 committed by ant
parent 41ce156114
commit 0012245c57
3 changed files with 58 additions and 0 deletions

View File

@ -724,6 +724,36 @@ corresponding to when the API was available for use.
```
</details>
#### `answerCards`
* Answer cards. Answers are between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "answerCards",
"version": 6,
"params": {
"cards": [1498938915662, 1502098034048],
"answers": [2, 4],
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": [true, true],
"error": null
}
```
</details>
---
### Deck Actions

View File

@ -1530,6 +1530,22 @@ class AnkiConnect:
self.stopEditing()
@util.api()
def answerCards(self, cards, answers):
scheduler = self.scheduler()
success = []
for i, cid in enumerate(cards):
try:
card = self.getCard(cid)
card.start_timer()
scheduler.answerCard(card, answers[i])
success.append(True)
except NotFoundError:
success.append(False)
return success
@util.api()
def cardReviews(self, deck, startID):
return self.database().all(

View File

@ -76,3 +76,15 @@ def test_forgetCards(setup):
def test_relearnCards(setup):
ac.relearnCards(cards=setup.card_ids)
class TestAnswerCards:
def test_answerCards(self, setup):
ac.scheduler().reset()
result = ac.answerCards(cards=setup.card_ids, answers=[2, 1, 4, 3])
assert result == [True] * 4
def test_answerCards_with_invalid_card_id(self, setup):
ac.scheduler().reset()
result = ac.answerCards(cards=[123], answers=[2])
assert result == [False]