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

apiReflect #324

Merged
merged 3 commits into from
May 29, 2022
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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,43 @@ corresponding to when the API was available for use.
}
```


* **apiReflect**

Gets information about the AnkiConnect APIs available. The request supports the following params:

* `scopes` - An array of scopes to get reflection information about.
The only currently supported value is `"actions"`.
* `actions` - Either `null` or an array of API method names to check for.
If the value is `null`, the result will list all of the available API actions.
If the value is an array of strings, the result will only contain actions which were in this array.

The result will contain a list of which scopes were used and a value for each scope.
For example, the `"actions"` scope will contain a `"actions"` property which contains a list of supported action names.

*Sample request*:
```json
{
"action": "apiReflect",
"params": {
"scopes": ["actions", "invalidType"],
"actions": ["apiReflect", "invalidMethod"]
},
"version": 6
}
```

*Sample result*:
```json
{
"result": {
"scopes": ["actions"],
"actions": ["apiReflect"]
},
"error": null
}
```

* **sync**

Synchronizes the local Anki collections with AnkiWeb.
Expand Down
30 changes: 30 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,36 @@ def importPackage(self, path):

return False


@util.api()
def apiReflect(self, scopes=None, actions=None):
if not isinstance(scopes, list):
raise Exception('scopes has invalid value')
if not (actions is None or isinstance(actions, list)):
raise Exception('actions has invalid value')

cls = type(self)
scopes2 = []
result = {'scopes': scopes2}

if 'actions' in scopes:
if actions is None:
actions = dir(cls)

methodNames = []
for methodName in actions:
if not isinstance(methodName, str):
pass
method = getattr(cls, methodName, None)
if method is not None and getattr(method, 'api', False):
methodNames.append(methodName)

scopes2.append('actions')
result['actions'] = methodNames

return result


#
# Entry
#
Expand Down
11 changes: 11 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def test_reloadCollection(setup):
ac.reloadCollection()


def test_apiReflect(setup):
result = ac.apiReflect(
scopes=["actions", "invalidType"],
actions=["apiReflect", "invalidMethod"]
)
assert result == {
"scopes": ["actions"],
"actions": ["apiReflect"]
}


class TestProfiles:
def test_getProfiles(self, session_with_profile_loaded):
result = ac.getProfiles()
Expand Down