11import axios , { AxiosInstance , AxiosRequestConfig } from "axios" ;
22
33import { Entity } from "./Entity" ;
4+ import { Subscription } from "./Subscription" ;
45
56export class ContextBroker {
67 private client : AxiosInstance ;
@@ -16,11 +17,6 @@ export class ContextBroker {
1617 this . contextBrokerMaxLimit = contextBrokerMaxLimit ;
1718 }
1819
19- async findEntity ( id : string ) {
20- const response = await this . client . get ( `/entities/${ encodeURIComponent ( id ) } ` ) ;
21- return new Entity ( response . data ) ;
22- }
23-
2420 async findEntities ( { type, query } : { type ?: string , query ?: string } ) {
2521 const config : AxiosRequestConfig = {
2622 params : {
@@ -46,6 +42,11 @@ export class ContextBroker {
4642 return entities ;
4743 }
4844
45+ async findEntity ( id : string ) {
46+ const response = await this . client . get ( `/entities/${ encodeURIComponent ( id ) } ` ) ;
47+ return new Entity ( response . data ) ;
48+ }
49+
4950 async insertEntity ( entity : Entity ) {
5051 await this . client . post ( "/entities" , entity . toObject ( ) ) ;
5152 }
@@ -57,4 +58,40 @@ export class ContextBroker {
5758 async deleteEntity ( entity : Entity ) {
5859 await this . client . delete ( `/entities/${ encodeURIComponent ( entity . getId ( ) ) } ` ) ;
5960 }
61+
62+ async findSubscriptions ( ) {
63+ const config : AxiosRequestConfig = {
64+ params : {
65+ limit : this . contextBrokerMaxLimit ,
66+ offset : 0
67+ }
68+ } ;
69+ const subscriptions : Subscription [ ] = [ ] ;
70+ while ( true ) {
71+ const response = await this . client . get ( "/subscriptions" , config ) ;
72+ subscriptions . push ( ...response . data . map ( ( subscription : any ) => new Subscription ( subscription ) ) ) ;
73+
74+ if ( response . data . length && response . data . length === this . contextBrokerMaxLimit ) {
75+ config . params . offset += response . data . length ;
76+ continue ;
77+ }
78+
79+ break ;
80+ }
81+
82+ return subscriptions ;
83+ }
84+
85+ async findSubscription ( id : string ) {
86+ const response = await this . client . get ( `/subscriptions/${ encodeURIComponent ( id ) } ` ) ;
87+ return new Subscription ( response . data ) ;
88+ }
89+
90+ async insertSubscription ( subscription : Subscription ) {
91+ await this . client . post ( "/subscriptions" , subscription . toObject ( ) ) ;
92+ }
93+
94+ async deleteSubscription ( subscription : Subscription ) {
95+ await this . client . delete ( `/subscriptions/${ encodeURIComponent ( subscription . getId ( ) ) } ` ) ;
96+ }
6097}
0 commit comments