/*
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*/
import {Injectable} from '@angular/core';
import {Client} from '@openstapps/api/lib/client';
import {SCSearchQuery, SCSearchResponse, SCThingOriginType, SCThings, SCThingType} from '@openstapps/core';
import {SCSaveableThing} from '@openstapps/core';
import {StorageProvider} from '../storage/storage.provider';
import {StAppsWebHttpClient} from './stapps-web-http-client.provider';
export enum DataScope {
Local = 'local',
Remote = 'remote',
}
/*
Generated class for the DataProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class DataProvider {
private _storagePrefix: string = 'stapps.data';
// @TODO: get backendUrl and appVersion and storagePrefix from config (module)
appVersion: string = '1.0.6';
backendUrl: string = 'http://localhost:3000';
client: Client;
storageProvider: StorageProvider;
constructor(stAppsWebHttpClient: StAppsWebHttpClient, storageProvider: StorageProvider) {
this.client = new Client(stAppsWebHttpClient, this.backendUrl, this.appVersion);
this.storageProvider = storageProvider;
}
get storagePrefix(): string {
return this._storagePrefix;
}
set storagePrefix(storagePrefix) {
this._storagePrefix = storagePrefix;
}
/**
* Provides key for storing data into the local database
*
* @param uid Unique identifier of a resource
*/
getDataKey(uid: string): string {
return `${this.storagePrefix}.${uid}`;
}
/**
* Provides a saveable thing from the local database using the provided UID
*/
async get(uid: string, scope: DataScope.Local): Promise>;
/**
* Provides a thing from the backend
*/
async get(uid: string, scope: DataScope.Remote): Promise>;
/**
* Provides a thing from both local database and backend
*/
async get(uid: string): Promise