Compare commits

..

13 Commits

Author SHA1 Message Date
Alexei Yatskov
1f7fc80b7e
Merge pull request #407 from PSeitz/patch-1
Update README.md
2023-10-09 19:16:44 -07:00
PSeitz
820300cc5c
Update README.md
add link to Anki docs in overview
2023-10-08 15:57:08 +02:00
Alexei Yatskov
4847fff790
Merge pull request #403 from Julian/auth
Make it slightly clearer how to enable authentication.
2023-09-28 20:48:43 -07:00
Julian Berman
82210ddf87 Make it slightly clearer how to enable authentication.
The README previously sort of covered this, but in particular it was not
obvious to me that the request parameter that corresponded to enabling
`apiKey` is called `key`, and not `apiKey`, which ultimately made me
have to look at the source code to figure this out (even though the word
`key` is mentioned elsewhere on the page, it still wasn't very easy to
make the connection).

This section puts all the required information in one spot.
2023-09-24 12:40:41 -04:00
Alexei Yatskov
b896b66736
Merge pull request #396 from mikkkee/master 2023-07-08 08:02:14 -07:00
mikkkee
5a153691fd Fix test timeout error 2023-07-07 20:08:33 +08:00
mikkkee
14a8a84150 Fix previous commit with version check for Anki and Qt 2023-07-07 01:03:46 +08:00
Alexei Yatskov
9f7dccaca0
Merge pull request #392 from mikkkee/master
Add a method to bring up Import File dialog
2023-07-05 20:06:08 -07:00
mikkkee
9e214e90e5 Add a test for guiImportFile 2023-07-05 22:17:30 +08:00
Alexei Yatskov
6693ab6b97
Merge pull request #394 from nihil-admirari/master
Correct JSON MIME type (Fixes #393)
2023-06-26 18:11:19 -07:00
nihil-admirari
423d3e2aa2
Correct JSON MIME type (Fixes #393) 2023-06-23 09:12:28 +03:00
mikkkee
cd66410ff3 Update documentation for new method guiImportFile 2023-06-17 17:52:14 +08:00
mikkkee
e999a44a47 Add a GUI action to bring up import file dialog with provided file path 2023-06-17 17:45:39 +08:00
4 changed files with 90 additions and 3 deletions

View File

@ -156,6 +156,13 @@ const result = await invoke('deckNames', 6);
console.log(`got list of decks: ${result}`);
```
### Authentication
Anki-Connect supports requiring authentication in order to make API requests.
This support is *disabled* by default, but can be enabled by setting the `apiKey` field of Anki-Config's settings (Tools->Add-ons->AnkiConnect->Config) to a desired string.
If you have done so, you should see the [`requestPermission`](#requestpermission) API request return `true` for `requireApiKey`.
You then must include an additional parameter called `key` in any further API request bodies, whose value must match the configured API key.
### Hey, could you add a new action to support $FEATURE?
The primary goal for Anki-Connect was to support real-time flash card creation from the
@ -177,6 +184,7 @@ be serviced*. Make sure that your pull request meets the following criteria:
Documentation for currently supported actions is split up by category and is referenced below. Note that deprecated APIs
will continue to function despite not being listed on this page as long as your request is labeled with a version number
corresponding to when the API was available for use.
Search parameters are passed to Anki, check the docs for more information: https://docs.ankiweb.net/searching.html
* [Card Actions](#card-actions)
* [Deck Actions](#deck-actions)
@ -1617,6 +1625,35 @@ corresponding to when the API was available for use.
```
</details>
#### `guiImportFile`
* Invokes the *Import... (Ctrl+Shift+I)* dialog with an optional file path. Brings up the dialog for user to review the import. Supports all file types that Anki supports. Brings open file dialog if no path is provided. Forward slashes must be used in the path on Windows. Only supported for Anki 2.1.52+.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "guiImportFile",
"version": 6,
"params": {
"path": "C:/Users/Desktop/cards.txt"
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": null,
"error": null
}
```
</details>
#### `guiExitAnki`
* Schedules a request to gracefully close Anki. This operation is asynchronous, so it will return immediately and

View File

@ -1852,6 +1852,48 @@ class AnkiConnect:
return False
@util.api()
def guiImportFile(self, path=None):
"""
Open Import File (Ctrl+Shift+I) dialog with provided file path.
If no path is given, the user will be prompted to select a file.
Only supported from Anki version >=2.1.52
path: string
import file path, note on Windows you must use forward slashes.
"""
if anki_version >= (2, 1, 52):
from aqt.import_export.importing import import_file, prompt_for_file_then_import
else:
raise Exception('guiImportFile is only supported from Anki version >=2.1.52')
if hasattr(Qt, 'WindowStaysOnTopHint'):
# Qt5
WindowOnTopFlag = Qt.WindowStaysOnTopHint
elif hasattr(Qt, 'WindowType') and hasattr(Qt.WindowType, 'WindowStaysOnTopHint'):
# Qt6
WindowOnTopFlag = Qt.WindowType.WindowStaysOnTopHint
else:
# Unsupported, don't try to bring window to top
WindowOnTopFlag = None
# Bring window to top for user to review import settings.
if WindowOnTopFlag is not None:
try:
# [Step 1/2] set always on top flag, show window (it stays on top for now)
self.window().setWindowFlags(self.window().windowFlags() | WindowOnTopFlag)
self.window().show()
finally:
# [Step 2/2] clear always on top flag, show window (it doesn't stay on top anymore)
self.window().setWindowFlags(self.window().windowFlags() & ~WindowOnTopFlag)
self.window().show()
if path is None:
prompt_for_file_then_import(self.window())
else:
import_file(self.window(), path)
@util.api()
def guiExitAnki(self):
timer = QTimer()

View File

@ -248,7 +248,7 @@ class WebServer:
def buildHeaders(self, corsOrigin, body):
return [
['HTTP/1.1 200 OK', None],
['Content-Type', 'text/json'],
['Content-Type', 'application/json'],
['Access-Control-Allow-Origin', corsOrigin],
['Access-Control-Allow-Headers', '*'],
['Content-Length', str(len(body))]

View File

@ -1,6 +1,7 @@
import pytest
from unittest import mock
from conftest import ac, wait_until, \
from conftest import ac, anki_version, wait_until, \
close_all_dialogs_and_wait_for_them_to_run_closing_callbacks, \
get_dialog_instance
@ -28,6 +29,13 @@ def test_guiDeckOverview(setup):
assert ac.guiDeckOverview(name="test_deck") is True
def test_guiImportFile(setup):
if anki_version >= (2, 1, 52):
with mock.patch('aqt.import_export.importing.prompt_for_file_then_import') as mock_prompt_for_file_then_import:
mock_prompt_for_file_then_import.return_value = True
ac.guiImportFile()
class TestAddCards:
note = {
"deckName": "test_deck",