X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fservices%2Ffeathers.service.ts;fp=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fservices%2Ffeathers.service.ts;h=e5ba3eb1633c9e49b2a1211e49ae30a89c3dac16;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-frontend/client/src/app/shared/services/feathers.service.ts b/otf-frontend/client/src/app/shared/services/feathers.service.ts new file mode 100644 index 0000000..e5ba3eb --- /dev/null +++ b/otf-frontend/client/src/app/shared/services/feathers.service.ts @@ -0,0 +1,88 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + + +import { Injectable, OnInit } from '@angular/core'; + +import * as feathers from '@feathersjs/client'; +import * as io from 'socket.io-client'; +import * as socketio from '@feathersjs/socketio-client'; +import * as authentication from '@feathersjs/authentication-client'; +import { Observable, from, interval } from 'rxjs'; +import { now } from 'moment'; +import { AppGlobals } from 'app/app.global'; +import { CookieService } from 'ngx-cookie-service'; +import { Router } from '@angular/router'; + + +@Injectable({ + providedIn: 'root' +}) +export class FeathersService { + // There are no proper typings available for feathers, due to its plugin-heavy nature + private _feathers: any; + public _socket: any; + public auth: Observable; + + constructor(private route: Router) { + this._socket = io('/',{ + transports: ['websocket'] + }); // init socket.io + this._socket.on('connect_error', function(data){ + route.navigateByUrl('/login'); + }); + this._feathers = feathers(); // init Feathers // add hooks plugin + this._feathers.configure(socketio(this._socket, { + timeout: 100000000 + })); // add socket.io plugin + this._feathers.configure(authentication({ + storage: window.localStorage, + storageKey: 'access_token' + })); + + //set observiable for services to check before calling the service + this.auth = from(this._feathers.authenticate()); + + } + + // expose services + public service(name: string) { + return this._feathers.service(name); + } + + public socket(){ + return this._socket; + } + + // expose authentication + public authenticate(credentials?): Observable { + return new Observable(observer => { + this.auth = from(this._feathers.authenticate(credentials).then(res => { + observer.next(res); + }, err => { + observer.error(err); + this.route.navigate(['/login']) + })); + }); + + } + + // expose logout + public logout() { + return this._feathers.logout(); + } +} +