Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

add new action: answerCards #390

Merged
merged 2 commits into from
Jun 16, 2023
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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,44 @@ corresponding to when the API was available for use.
```
</details>

#### `answerCards`

* 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>
<summary><i>Sample request:</i></summary>

```json
{
"action": "answerCards",
"version": 6,
"params": {
"answers": [
{
"cardId": 1498938915662,
"ease": 2
},
{
"cardId": 1502098034048,
"ease": 4
}
]
}
}
```
</details>

<details>
<summary><i>Sample result:</i></summary>

```json
{
"result": [true, true],
"error": null
}
```
</details>

---

### Deck Actions
Expand Down
18 changes: 18 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,24 @@ def relearnCards(self, cards):
self.stopEditing()


@util.api()
def answerCards(self, answers):
scheduler = self.scheduler()
success = []
for answer in answers:
try:
cid = answer['cardId']
ease = answer['ease']
card = self.getCard(cid)
card.start_timer()
scheduler.answerCard(card, ease)
success.append(True)
except NotFoundError:
success.append(False)

return success


@util.api()
def cardReviews(self, deck, startID):
return self.database().all(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ def test_forgetCards(setup):

def test_relearnCards(setup):
ac.relearnCards(cards=setup.card_ids)


class TestAnswerCards:
def test_answerCards(self, setup):
ac.scheduler().reset()
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

def test_answerCards_with_invalid_card_id(self, setup):
ac.scheduler().reset()
result = ac.answerCards([{"cardId": 123, "ease": 2}])
assert result == [False]