FooSoft / anki-connect

Anki plugin to expose a remote API for creating flash cards.
https://foosoft.net/projects/anki-connect/
Other
1.92k stars 218 forks source link

Updated documentation on multi #365

Closed Aquafina-water-bottle closed 1 year ago

Aquafina-water-bottle commented 1 year ago

The documentation for the multi action seems to be incorrect, so I updated it with the results that I got from the example code below.

In particular, on successful calls, multi does not return entries in format {"result": ..., "error": ...}

Example python code to generate the result ```python import json import urllib.request def request(action, **params): return {'action': action, 'params': params, 'version': 6} def invoke(action, **params): requestJson = json.dumps(request(action, **params)).encode('utf-8') response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson))) print(json.dumps(response)) if len(response) != 2: raise Exception('response has an unexpected number of fields') if 'error' not in response: raise Exception('response is missing required error field') if 'result' not in response: raise Exception('response is missing required result field') if response['error'] is not None: raise Exception(response['error']) return response['result'] invoke("multi", actions=[ {"action": "deckNames"}, {"action": "invalidAction", "params": {"useless": "param"}} ]) ```
FooSoft commented 1 year ago

Looks good to me, let me know when this PR is ready.

Aquafina-water-bottle commented 1 year ago

Sorry for the delay, I was waiting for a different PR for a different project for another pair of eyes to verify the differences. I added some extra examples for when the action is version 6. It should be ready now! @FooSoft

Example python code + result ```python import json import urllib.request def request(action, **params): return {'action': action, 'params': params, 'version': 6} def invoke(action, **params): requestJson = json.dumps(request(action, **params)).encode('utf-8') response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', requestJson))) print(json.dumps(response, indent=2)) if len(response) != 2: raise Exception('response has an unexpected number of fields') if 'error' not in response: raise Exception('response is missing required error field') if 'result' not in response: raise Exception('response is missing required result field') if response['error'] is not None: raise Exception(response['error']) return response['result'] invoke("multi", actions=[ {"action": "deckNames"}, {"action": "deckNames", "version": 6}, {"action": "invalidAction", "params": {"useless":"param"}}, {"action": "invalidAction", "version": 6, "params": {"useless":"param"}} ]) ``` Result: ```json { "result": [ [ "Default" ], { "result": [ "Default" ], "error": null }, { "result": null, "error": "unsupported action" }, { "result": null, "error": "unsupported action" } ], "error": null } ```

Edit: forgot to mark as ready