Return error message in failure scenarios, rename checkState to guiCheckState, remove success+message response format in favour of True/False + rename ord field to fieldOrder (see readme) + updated readme.

This commit is contained in:
Charles Henry 2017-06-26 23:57:41 +01:00
parent 842dae9830
commit 54ab68feb4
2 changed files with 50 additions and 42 deletions

View File

@ -439,6 +439,8 @@ class AnkiBridge:
self.window().moveToState('review') self.window().moveToState('review')
card = self.window().reviewer.card card = self.window().reviewer.card
if card:
return { return {
'success': True, 'success': True,
'id': card.id, 'id': card.id,
@ -446,47 +448,48 @@ class AnkiBridge:
'answer': card._getQA()['a'], 'answer': card._getQA()['a'],
'answerButtons': self.window().reviewer._answerButtonList(), 'answerButtons': self.window().reviewer._answerButtonList(),
'modelName': card.note(reload)._model['name'], 'modelName': card.note(reload)._model['name'],
'ord': card.ord, 'fieldOrder': card.ord,
'fields': card.note(reload).fields, 'fields': card.note(reload).fields,
'fieldMap': card.note(reload)._fmap 'fieldMap': card.note(reload)._fmap
} }
else:
return 'There are no cards left to review.'
def guiShowQuestion(self): def guiShowQuestion(self):
if self.window().reviewer.card is None or self.window().state != 'review': if self.window().reviewer.card is None or self.window().state != 'review':
self.window().moveToState('review') self.window().moveToState('review')
self.window().reviewer._showQuestion() self.window().reviewer._showQuestion()
return {'success': True} return True
def guiShowAnswer(self): def guiShowAnswer(self):
if self.window().reviewer.mw.state != 'review': if self.window().reviewer.mw.state != 'review':
return {'success': False, 'message': 'Window state is not review.'} return 'Window state is not review.'
else: else:
self.window().reviewer._showAnswer() self.window().reviewer._showAnswer()
return {'success': True} return True
def guiAnswerCard(self, id, ease): def guiAnswerCard(self, id, ease):
if self.window().reviewer.mw.state != 'review': if self.window().reviewer.mw.state != 'review':
return {'success': False, 'message': 'Window state is not review.'} return 'Window state is not review.'
elif self.window().reviewer.state != 'answer': elif self.window().reviewer.state != 'answer':
return {'success': False, 'message': 'Reviewer state is not answer.'} return 'Reviewer state is not answer.'
elif self.window().reviewer.card.id != id: elif self.window().reviewer.card.id != id:
return {'success': False, 'message': 'Given card does not match.'} return 'Given card does not match.'
elif self.window().col.sched.answerButtons(self.window().reviewer.card) < ease: elif self.window().col.sched.answerButtons(self.window().reviewer.card) < ease:
return {'success': False, 'message': 'Invalid ease provided.'} return 'Invalid ease provided.'
else: else:
self.window().reviewer._answerCard(ease) self.window().reviewer._answerCard(ease)
return {'success': True} return True
def checkState(self): def guiCheckState(self):
return { return {
'success': True, 'success': True,
'window_state': self.window().state, 'windowState': self.window().state,
'reviewer_state': self.window().reviewer.state 'reviewerState': self.window().reviewer.state
} }
@ -614,7 +617,7 @@ class AnkiConnect:
def api_checkState(self): def api_checkState(self):
return self.anki.checkState() return self.anki.guiCheckState()
# #
# Entry # Entry

View File

@ -338,12 +338,12 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
* **guiGetNextCard** * **guiGetNextCard**
Returns next/current card, calling this multiple times will not skip unanswered cards. Returns next/current card, calling this multiple times will not skip unanswered cards. The low level fields and card direction can be derived from the 'fieldOrder', 'fieldMap' and 'fields' keys in the response.
*Sample request*: *Sample request*:
``` ```
{ {
action: 'getNextCard', action: 'guiGetNextCard',
params: {} params: {}
} }
``` ```
@ -351,7 +351,7 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample response*: *Sample response*:
``` ```
{ {
'success': 'true', 'success': true,
'question': 'Hello', 'question': 'Hello',
'fieldMap': { 'fieldMap': {
'Front': [ 'Front': [
@ -399,11 +399,16 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
"Hola" "Hola"
], ],
'answer': 'Hello\n\n<hr id=answer>\n\nHola', 'answer': 'Hello\n\n<hr id=answer>\n\nHola',
'ord': 0, 'fieldOrder': 0,
'id': 1496751176292 'id': 1496751176292
} }
``` ```
*Sample error response*:
```
'There are no cards left to review.'
```
* **guiShowQuestion** * **guiShowQuestion**
Move Anki to the state of showing a question (window state = 'review' and reviewer state = 'question'). This is required in order to show the answer and can also be used to move from the showAnswer state back to the showQuestion state. Move Anki to the state of showing a question (window state = 'review' and reviewer state = 'question'). This is required in order to show the answer and can also be used to move from the showAnswer state back to the showQuestion state.
@ -411,14 +416,14 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'showQuestion', action: 'guiShowQuestion',
params: {} params: {}
} }
``` ```
*Sample response*: *Sample response*:
``` ```
{'success': 'true'} true
``` ```
* **guiShowAnswer** * **guiShowAnswer**
@ -428,29 +433,29 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'showAnswer', action: 'guiShowAnswer',
params: {} params: {}
} }
``` ```
*Sample response*: *Sample response*:
``` ```
{'success': 'true'} true
``` ```
*Sample error response*: *Sample error response*:
``` ```
{'success': 'false', 'message': 'Window state is not review.'} 'Window state is not review.'
``` ```
* **guiAnswerCard** * **guiAnswerCard**
Used to answer a card that is in the showAnswer state. Valid answers for this card can be found in the getNextCard response. Used to answer a card that is in the showAnswer state. Valid answers for this card can be found in the guiGetNextCard response.
*Sample request*: *Sample request*:
``` ```
{ {
action: 'answerCard', action: 'guiAnswerCard',
params: { params: {
id: 1496751176292, id: 1496751176292,
ease: 1 ease: 1
@ -460,35 +465,35 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample response*: *Sample response*:
``` ```
{'success': 'true'} true
``` ```
*Sample error responses*: *Sample error responses*:
``` ```
{'success': 'false', 'message': 'Window state is not review.'} 'Window state is not review.'
``` ```
``` ```
{'success': 'false', 'message': 'Reviewer state is not answer.'} 'Reviewer state is not answer.'
``` ```
``` ```
{'success': 'false', 'message': 'Given card does not match.'} 'Given card does not match.'
``` ```
``` ```
{'success': 'false', 'message': 'Invalid ease provided.'} 'Invalid ease provided.'
``` ```
* **checkState** * **guiCheckState**
Returns the window state and the reviewer state. Returns the window state and the reviewer state.
*Sample request*: *Sample request*:
``` ```
{action: 'checkState'} {action: 'guiCheckState'}
``` ```
*Sample response*: *Sample response*:
``` ```
{'window_state': 'review', 'reviewer_state': 'answer', 'success': 'true'} {'windowState': 'review', 'reviewerState': 'answer', 'success': true}
``` ```
* **upgrade** * **upgrade**