Skip to content

Commit 48a842f

Browse files
Add subscriptions and change version to 0.9.0
1 parent 6516c61 commit 48a842f

File tree

5 files changed

+70
-11
lines changed

5 files changed

+70
-11
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@faubourg-numerique/ngsi-ld",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {

src/ContextBroker.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
22

33
import { Entity } from "./Entity";
4+
import { Subscription } from "./Subscription";
45

56
export 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
}

src/Subscription.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class Subscription {
2+
private data: any = {
3+
id: ""
4+
};
5+
6+
constructor(data: any) {
7+
Object.assign(this.data, data);
8+
}
9+
10+
getId(): string {
11+
return this.data.id;
12+
}
13+
14+
setId(id: string) {
15+
this.data.id = id;
16+
}
17+
18+
toObject() {
19+
return this.data;
20+
}
21+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./ContextBroker";
22
export * from "./Entity";
3+
export * from "./Subscription";

0 commit comments

Comments
 (0)