chore: Compartimentalize AnkiService a little into different functions
This commit is contained in:
parent
4d58b2d299
commit
ea472e6afb
|
@ -13,7 +13,7 @@ class AnkiService
|
|||
|
||||
private function request(string $action, array $params): mixed
|
||||
{
|
||||
$res = $this->client->request(
|
||||
['error' => $error, 'result' => $result] = $this->client->request(
|
||||
'POST',
|
||||
'http://localhost:8765',
|
||||
['json' => [
|
||||
|
@ -23,19 +23,37 @@ class AnkiService
|
|||
]]
|
||||
)->toArray();
|
||||
|
||||
throw new \Exception('AnkiConnect returned error: ' . $res['error']);
|
||||
|
||||
return $res['result'];
|
||||
if ($error != null) {
|
||||
throw new \Exception('AnkiConnect returned error: ' . $error);
|
||||
}
|
||||
|
||||
public function getLatestNote(): Note
|
||||
{
|
||||
$latestId = max($this->request(
|
||||
'findNotes',
|
||||
['aquery' => 'added:1 "note:Japanese sentences"']
|
||||
));
|
||||
return $result;
|
||||
}
|
||||
|
||||
$noteInfo = $this->request('notesInfo', ['notes' => [$latestId]])[0];
|
||||
public function getAllNoteIds(): array
|
||||
{
|
||||
return $this->request(
|
||||
'findNotes',
|
||||
['query' => '"note:Japanese sentences"']
|
||||
);
|
||||
}
|
||||
|
||||
public function getNotesInfo(array $noteIds): array
|
||||
{
|
||||
return $this->request('notesInfo', ['notes' => $noteIds]);
|
||||
}
|
||||
|
||||
public function getNoteInfo(int $noteId): array
|
||||
{
|
||||
return $this->getNotesInfo([$noteId])[0];
|
||||
}
|
||||
|
||||
public function getLatestNote(): ?Note
|
||||
{
|
||||
// NoteIDs are just timestamps in milliseconds, so the latest is just
|
||||
// the biggest numerically
|
||||
$latestId = max($this->getAllNoteIds());
|
||||
$noteInfo = $this->getNoteInfo($latestId);
|
||||
|
||||
return Note::fromAnki($noteInfo);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue