Refactor and remove getAll
This commit is contained in:
parent
d8a3e8e93e
commit
60ed4e5109
1 changed files with 18 additions and 56 deletions
|
|
@ -18,6 +18,7 @@ export interface CheckConnectionResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SyncService {
|
export class SyncService {
|
||||||
|
private static readonly NETWORK_RETRY_INTERVAL_MS = 1000;
|
||||||
private client: Client<paths>;
|
private client: Client<paths>;
|
||||||
private pingClient: Client<paths>;
|
private pingClient: Client<paths>;
|
||||||
private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch;
|
private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch;
|
||||||
|
|
@ -244,47 +245,27 @@ export class SyncService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAll(
|
public async checkConnection(): Promise<CheckConnectionResult> {
|
||||||
since?: VaultUpdateId
|
try {
|
||||||
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
|
const response = await this.pingClient.GET("/ping", {
|
||||||
return this.withRetries(async () => {
|
params: {
|
||||||
const { vaultName } = this.settings.getSettings();
|
header: {
|
||||||
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
const response = await this.client.GET(
|
|
||||||
"/vaults/{vault_id}/documents",
|
|
||||||
{
|
|
||||||
params: {
|
|
||||||
path: {
|
|
||||||
vault_id: vaultName
|
|
||||||
},
|
|
||||||
header: {
|
|
||||||
authorization: `Bearer ${this.settings.getSettings().token}`
|
|
||||||
},
|
|
||||||
query: {
|
|
||||||
since_update_id: since
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.debug(
|
||||||
|
`Ping response: ${JSON.stringify(response.data)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const { error } = response;
|
if (!response.data) {
|
||||||
if (error) {
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to get documents: ${SyncService.formatError(response.error)}`
|
`Failed to ping server: ${SyncService.formatError(response.error)}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.debug(
|
const result = response.data;
|
||||||
`Got ${response.data.latestDocuments.length} document metadata`
|
|
||||||
);
|
|
||||||
|
|
||||||
return response.data;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async checkConnection(): Promise<CheckConnectionResult> {
|
|
||||||
try {
|
|
||||||
const result = await this.ping();
|
|
||||||
if (result.isAuthenticated) {
|
if (result.isAuthenticated) {
|
||||||
return {
|
return {
|
||||||
isSuccessful: true,
|
isSuccessful: true,
|
||||||
|
|
@ -304,27 +285,6 @@ export class SyncService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No retries
|
|
||||||
private async ping(): Promise<components["schemas"]["PingResponse"]> {
|
|
||||||
const response = await this.pingClient.GET("/ping", {
|
|
||||||
params: {
|
|
||||||
header: {
|
|
||||||
authorization: `Bearer ${this.settings.getSettings().token}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.logger.debug(`Ping response: ${JSON.stringify(response.data)}`);
|
|
||||||
|
|
||||||
if (!response.data) {
|
|
||||||
throw new Error(
|
|
||||||
`Failed to ping server: ${SyncService.formatError(response.error)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a client and a ping client for the given remote URI.
|
* Create a client and a ping client for the given remote URI.
|
||||||
*/
|
*/
|
||||||
|
|
@ -355,8 +315,10 @@ export class SyncService {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.error(`Failed network call (${e}), retrying`);
|
this.logger.error(
|
||||||
await sleep(1000);
|
`Failed network call (${e}), retryingin ${SyncService.NETWORK_RETRY_INTERVAL_MS}ms`
|
||||||
|
);
|
||||||
|
await sleep(SyncService.NETWORK_RETRY_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue