-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathclient.js
More file actions
117 lines (100 loc) · 3.12 KB
/
client.js
File metadata and controls
117 lines (100 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { QdrantClient } from '@qdrant/js-client-rest';
import { getQdrantPrefix } from './utils';
/**
* Extended QdrantClient class with additional methods
* @class
* @extends QdrantClient
*/
export class QdrantClientExtended extends QdrantClient {
#downloadController;
constructor({ url, apiKey, port, prefix = '' }) {
super({ url, apiKey, port, prefix, checkCompatibility: false, headers: { 'x-inference-proxy': 'true' } });
this.downloadSnapshot = this.downloadSnapshot.bind(this);
this.getSnapshotUploadUrl = this.getSnapshotUploadUrl.bind(this);
this.getApiKey = this.getApiKey.bind(this);
this.abortDownload = this.abortDownload.bind(this);
this.url = url;
this.apiKey = apiKey;
this.port = port;
this.prefix = prefix;
this.#downloadController = new AbortController();
}
/**
* Download snapshot from the server
* @param {string} collectionName - name of the collection
* @param {string} snapshotName - name of the snapshot
* @param {boolean} blob - return blob instead of response, default false
* @return {Promise<any>} - promise with response or blob
* @example <caption>Download snapshot</caption>
* const response = await client.downloadSnapshot('collection', 'snapshot');
* const blob = await client.downloadSnapshot('collection', 'snapshot', true);
*/
async downloadSnapshot(collectionName, snapshotName, blob = false) {
const headers = {
'Content-Disposition': `attachment; filename="${snapshotName}"`,
'Content-Type': 'application/gzip',
};
if (this.apiKey) {
headers['api-key'] = this.apiKey;
}
const snapshotUrl = new URL(
`${this.prefix}/collections/${encodeURIComponent(collectionName)}/snapshots/${encodeURIComponent(snapshotName)}`,
this.url
).href;
const request = new Request(snapshotUrl, {
method: 'GET',
headers,
});
const response = await fetch(request, { signal: this.#downloadController.signal });
if (blob) {
return await response.blob();
}
return response;
}
abortDownload() {
this.#downloadController.abort();
this.#downloadController = new AbortController();
}
/**
* Get url for snapshot upload
* @param {string} collectionName
* @return {module:url.URL} - URL object for snapshot upload
*/
getSnapshotUploadUrl(collectionName) {
return new URL(`${this.prefix}/collections/${encodeURIComponent(collectionName)}/snapshots/upload`, this.url);
}
/**
* Get api key
* @return {string} - api key
*/
getApiKey() {
return this.apiKey;
}
}
export default function qdrantClient({ apiKey }) {
let url;
let prefix = ''
let port = 6333;
if (process.env.NODE_ENV === 'development') {
url = 'http://localhost:6333';
} else {
url = window.location.origin;
prefix = getQdrantPrefix();
if (window.location.port) {
port = window.location.port;
} else {
if (window.location.protocol === 'https:') {
port = 443;
} else {
port = 80;
}
}
}
const options = {
url,
apiKey,
port,
prefix
};
return new QdrantClientExtended(options);
}