aboutsummaryrefslogtreecommitdiffstats
path: root/tools/compare-models/src/apiClient.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tools/compare-models/src/apiClient.ts')
-rw-r--r--tools/compare-models/src/apiClient.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/tools/compare-models/src/apiClient.ts b/tools/compare-models/src/apiClient.ts
new file mode 100644
index 00000000..f3a960cb
--- /dev/null
+++ b/tools/compare-models/src/apiClient.ts
@@ -0,0 +1,65 @@
+import { createKarakeepClient } from "@karakeep/sdk";
+
+import type { Bookmark } from "./types";
+import { config } from "./config";
+
+export class KarakeepAPIClient {
+ private readonly client: ReturnType<typeof createKarakeepClient>;
+
+ constructor() {
+ this.client = createKarakeepClient({
+ baseUrl: `${config.KARAKEEP_SERVER_ADDR}/api/v1/`,
+ headers: {
+ "Content-Type": "application/json",
+ authorization: `Bearer ${config.KARAKEEP_API_KEY}`,
+ },
+ });
+ }
+
+ async fetchBookmarks(limit: number): Promise<Bookmark[]> {
+ const bookmarks: Bookmark[] = [];
+ let cursor: string | null = null;
+ let hasMore = true;
+
+ while (hasMore && bookmarks.length < limit) {
+ const params: {
+ limit: number;
+ includeContent: true;
+ archived?: boolean;
+ cursor?: string;
+ } = {
+ limit: Math.min(limit - bookmarks.length, 50),
+ includeContent: true,
+ archived: false,
+ };
+
+ if (cursor) {
+ params.cursor = cursor;
+ }
+
+ const { data, response, error } = await this.client.GET("/bookmarks", {
+ params: {
+ query: params,
+ },
+ });
+
+ if (error) {
+ throw new Error(`Failed to fetch bookmarks: ${String(error)}`);
+ }
+
+ if (!response.ok) {
+ throw new Error(`Failed to fetch bookmarks: ${response.status}`);
+ }
+
+ const batchBookmarks = (data?.bookmarks || [])
+ .filter((b) => b.content?.type === "link")
+ .map((b) => b as Bookmark);
+
+ bookmarks.push(...batchBookmarks);
+ cursor = data?.nextCursor || null;
+ hasMore = !!cursor;
+ }
+
+ return bookmarks.slice(0, limit);
+ }
+}