change param format for answerCards

This commit is contained in:
ant 2023-06-15 12:49:02 +01:00
parent 0012245c57
commit 3892481cea
3 changed files with 21 additions and 8 deletions

View File

@ -726,7 +726,7 @@ corresponding to when the API was available for use.
#### `answerCards` #### `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. * Answer cards. Ease is between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
<details> <details>
<summary><i>Sample request:</i></summary> <summary><i>Sample request:</i></summary>
@ -736,8 +736,16 @@ corresponding to when the API was available for use.
"action": "answerCards", "action": "answerCards",
"version": 6, "version": 6,
"params": { "params": {
"cards": [1498938915662, 1502098034048], "answers": [
"answers": [2, 4], {
"cardId": 1498938915662,
"ease": 2
},
{
"cardId": 1502098034048,
"ease": 4
}
]
} }
} }
``` ```

View File

@ -1531,14 +1531,16 @@ class AnkiConnect:
@util.api() @util.api()
def answerCards(self, cards, answers): def answerCards(self, answers):
scheduler = self.scheduler() scheduler = self.scheduler()
success = [] success = []
for i, cid in enumerate(cards): for answer in answers:
try: try:
cid = answer['cardId']
ease = answer['ease']
card = self.getCard(cid) card = self.getCard(cid)
card.start_timer() card.start_timer()
scheduler.answerCard(card, answers[i]) scheduler.answerCard(card, ease)
success.append(True) success.append(True)
except NotFoundError: except NotFoundError:
success.append(False) success.append(False)

View File

@ -81,10 +81,13 @@ def test_relearnCards(setup):
class TestAnswerCards: class TestAnswerCards:
def test_answerCards(self, setup): def test_answerCards(self, setup):
ac.scheduler().reset() ac.scheduler().reset()
result = ac.answerCards(cards=setup.card_ids, answers=[2, 1, 4, 3]) answers = [
{"cardId": a, "ease": b} for a, b in zip(setup.card_ids, [2, 1, 4, 3])
]
result = ac.answerCards(answers)
assert result == [True] * 4 assert result == [True] * 4
def test_answerCards_with_invalid_card_id(self, setup): def test_answerCards_with_invalid_card_id(self, setup):
ac.scheduler().reset() ac.scheduler().reset()
result = ac.answerCards(cards=[123], answers=[2]) result = ac.answerCards([{"cardId": 123, "ease": 2}])
assert result == [False] assert result == [False]