more cleanup

This commit is contained in:
Alex Yatskov 2017-07-01 17:12:11 -07:00
parent 28ac1bc7c5
commit d38fea7498
2 changed files with 82 additions and 97 deletions

View File

@ -536,9 +536,10 @@ class AnkiConnect:
def handler(self, request): def handler(self, request):
action = 'api_' + request.get('action', '') action = 'api_' + request.get('action', '')
params = request.get('params', {})
if hasattr(self, action): if hasattr(self, action):
try: try:
return getattr(self, action)(**(request.get('params') or {})) return getattr(self, action)(**params)
except TypeError: except TypeError:
return None return None

176
README.md
View File

@ -82,9 +82,7 @@ curl localhost:8765 -X POST -d '{"action": "version", "params": {}}'
### Supported Actions ### ### Supported Actions ###
Below is a list of currently supported actions. Note that the sample requests are written here as JavaScript objects Below is a list of currently supported actions:
rather than raw JSON. If you are writing raw requests be sure to send valid JSON (i.e. quote all keys and strings with
`"`, and remove any comments).
* **version** * **version**
@ -97,8 +95,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'version', "action": "version",
params: {} "params": {}
} }
``` ```
@ -113,16 +111,15 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'deckNames', "action": "deckNames",
params: {} "params": {}
} }
``` ```
*Sample response*: *Sample response*:
``` ```
[ [
'Default', "Default"
/* ... */
] ]
``` ```
@ -133,17 +130,16 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'modelNames', "action": "modelNames",
params: {} "params": {}
} }
``` ```
*Sample response*: *Sample response*:
``` ```
[ [
'Basic', "Basic",
'Basic (and reversed card)', "Basic (and reversed card)"
/* ... */
] ]
``` ```
@ -154,9 +150,9 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'modelFieldNames', "action": "modelFieldNames",
params: { "params": {
modelName: 'Basic' "modelName": "Basic"
} }
} }
``` ```
@ -164,9 +160,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample response*: *Sample response*:
``` ```
[ [
'Front', "Front",
'Back', "Back"
/* ... */
] ]
``` ```
@ -183,24 +178,22 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'addNote', "action": "addNote",
params: { "params": {
note: { "note": {
deckName: 'Default', "deckName": "Default",
modelName: 'Basic', "modelName": "Basic",
fields: { "fields": {
Front: 'front content', "Front": "front content",
Back: 'back content', "Back": "back content",
/* ... */
}, },
tags: [ "tags": [
'yomichan', "yomichan",
/* ... */
], ],
audio: /* optional */ { "audio": {
url: 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ', "url": "https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ",
filename: 'yomichan_ねこ_猫.mp3', "filename": "yomichan_ねこ_猫.mp3",
skipHash: '7e2c2f954ef6051373ba916f000168dc' "skipHash": "7e2c2f954ef6051373ba916f000168dc"
} }
} }
} }
@ -221,28 +214,25 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'addNotes', "action": "addNotes",
params: { "params": {
notes: [ "notes": [
{ {
deckName: 'Default', "deckName": "Default",
modelName: 'Basic', "modelName": "Basic",
fields: { "fields": {
Front: 'front content', "Front": "front content",
Back: 'back content', "Back": "back content"
/* ... */
}, },
tags: [ "tags": [
'yomichan', "yomichan"
/* ... */
], ],
audio: /* optional */ { "audio": {
url: 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ', "url": "https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ",
filename: 'yomichan_ねこ_猫.mp3', "filename": "yomichan_ねこ_猫.mp3",
skipHash: '7e2c2f954ef6051373ba916f000168dc' "skipHash": "7e2c2f954ef6051373ba916f000168dc"
} }
}, }
/* ... */
] ]
} }
} }
@ -252,8 +242,7 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
``` ```
[ [
1496198395707, 1496198395707,
null, null
/* ... */
] ]
``` ```
@ -265,23 +254,20 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'canAddNotes', "action": "canAddNotes",
params: { "params": {
notes: [ "notes": [
{ {
deckName: 'Default', "deckName": "Default",
modelName: 'Basic', "modelName": "Basic",
fields: { "fields": {
Front: 'front content', "Front": "front content",
Back: 'back content', "Back": "back content"
/* ... */
}, },
tags: [ "tags": [
'yomichan', "yomichan"
/* ... */
] ]
}, }
/* ... */
] ]
} }
} }
@ -290,8 +276,7 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample response*: *Sample response*:
``` ```
[ [
true, true
/* ... */
] ]
``` ```
@ -302,9 +287,9 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiBrowse', "action": "guiBrowse",
params: { "params": {
query: 'deck:current' "query": "deck:current"
} }
} }
``` ```
@ -314,8 +299,7 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
[ [
1494723142483, 1494723142483,
1494703460437, 1494703460437,
1494703479525, 1494703479525
/* ... */
] ]
``` ```
@ -326,8 +310,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiAddCards', "action": "guiAddCards",
params: {} "params": {}
} }
``` ```
@ -343,20 +327,20 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiCurrentCard', "action": "guiCurrentCard",
params: {} "params": {}
} }
``` ```
*Sample response*: *Sample response*:
``` ```
{ {
answer: "Back", "answer": "back content",
buttons: [1, 2, 3], "question": "front content",
cardId: 1498938915662, "deckName": "Default",
deckName: "Default", "modelName": "Basic",
modelName: "Basic", "cardId": 1498938915662,
question: "Front" "buttons": [1, 2, 3]
} }
``` ```
@ -367,8 +351,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiShowQuestion', "action": "guiShowQuestion",
params: {} "params": {}
} }
``` ```
@ -384,8 +368,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiShowAnswer', "action": "guiShowAnswer",
params: {} "params": {}
} }
``` ```
@ -402,9 +386,9 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'guiAnswerCard', "action": "guiAnswerCard",
params: { "params": {
ease: 1 "ease": 1
} }
} }
``` ```
@ -423,8 +407,8 @@ rather than raw JSON. If you are writing raw requests be sure to send valid JSON
*Sample request*: *Sample request*:
``` ```
{ {
action: 'upgrade', "action": "upgrade",
params: {} "params": {}
} }
``` ```