forked from DAVFoundation/dav-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentity.js
120 lines (119 loc) · 4.58 KB
/
Identity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const common_types_1 = require("./common-types");
const Need_1 = require("./Need");
const Bid_1 = require("./Bid");
const Mission_1 = require("./Mission");
const Kafka_1 = require("./Kafka");
const axios_1 = require("axios");
const KafkaMessageFactory_1 = require("./KafkaMessageFactory");
/**
* @class The Identity class represent registered DAV identity instance.
*/
class Identity {
constructor(id, davId, _config) {
this.id = id;
this.davId = davId;
this._config = _config;
this.topics = {};
/**/
}
async registerNewTopic() {
const topic = Kafka_1.default.generateTopicId();
try {
await Kafka_1.default.createTopic(topic, this._config);
}
catch (err) {
// TODO: move this general message to kafka class
throw new Error(`Fail to create a topic: ${err}`);
}
return topic;
}
/**
* @method publishNeed Used to create a new need and publish it to the relevant service providers.
* @param needParams the need parameters.
* @returns the created need.
*/
async publishNeed(needParams) {
const bidsChannelName = await this.registerNewTopic(); // Channel#3
needParams.id = bidsChannelName;
needParams.davId = this.davId || needParams.davId;
try {
await axios_1.default.post(`${this._config.apiSeedUrls[0]}/publishNeed/${bidsChannelName}`, needParams.serialize());
}
catch (err) {
throw new Error(`Fail to publish need: ${err}`);
}
return new Need_1.default(bidsChannelName, needParams, this._config);
}
/**
* @method needsForType Used to subscribe for specific needs (filtered by params).
* @param needFilterParams the filter parameters.
* @returns Observable for needs subscription.
*/
async needsForType(needFilterParams) {
const formattedParams = needFilterParams.serialize();
let needTypeTopic = '';
if (this.topics[formattedParams.protocol]) {
needTypeTopic = this.topics[formattedParams.protocol];
}
else {
needTypeTopic = await this.registerNewTopic();
this.topics[formattedParams.protocol] = needTypeTopic;
try {
await axios_1.default.post(`${this._config.apiSeedUrls[0]}/needsForType/${needTypeTopic}`, formattedParams);
}
catch (err) {
throw new Error(`Needs registration failed: ${err}`);
}
}
const kafkaMessageStream = await Kafka_1.default.messages(needTypeTopic, this._config); // Channel#2
const needParamsStream = kafkaMessageStream.filterType(KafkaMessageFactory_1.default.instance.getMessageTypes(needFilterParams.protocol, KafkaMessageFactory_1.MessageCategories.Need));
const observable = common_types_1.Observable.fromObservable(needParamsStream.map((needParams) => new Need_1.default(needTypeTopic, needParams, this._config)), needParamsStream.topic);
return observable;
}
/**
* @method missions Used to subscribe for missions.
* @returns Observable for missions subscription.
*/
async missions() {
throw new Error('Not implemented in this version');
}
/**
* @method messages Used to subscribe for messages.
* @returns Observable for messages subscription.
*/
async messages() {
throw new Error('Not implemented in this version');
}
/**
* @method need Used to restore an existed need.
* @param needSelfId The selfId that used to create the bid.
* @param params The need parameters.
* @returns The restored need.
*/
need(needSelfId, params) {
return new Need_1.default(needSelfId, params, this._config);
}
/**
* @method bid Used to restore an existed bid.
* @param bidSelfId The selfId that used to create the bid.
* @param params The bid parameters.
* @returns The restored bid.
*/
bid(bidSelfId, params) {
return new Bid_1.default(bidSelfId, params, this._config);
}
/**
* @method mission Used to restore an existed mission.
* @param missionSelfId The mission self topic ID.
* @param missionPeerId The mission peer topic ID.
* @param params The mission parameters.
* @returns The restored mission.
*/
mission(missionSelfId, missionPeerId, params) {
return new Mission_1.default(missionSelfId, missionPeerId, params, this._config);
}
}
exports.default = Identity;
//# sourceMappingURL=Identity.js.map