Skip to content

Commit fef9197

Browse files
authored
Merge pull request #42 from microsoftgraph/feature/js-sdk
- fixess #41: replaces http calls by js sdk
2 parents 46ea584 + f404c7f commit fef9197

File tree

6 files changed

+682
-153
lines changed

6 files changed

+682
-153
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
"no-param-reassign": 0,
1616
"no-use-before-define": 0,
1717
"linebreak-style": 0,
18-
"no-plusplus": 0
18+
"no-plusplus": 0,
19+
"max-len": 0
1920
}
2021
};

helpers/requestHelper.js

Lines changed: 35 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,39 @@
1-
import https from 'https';
2-
3-
const host = 'graph.microsoft.com';
4-
5-
/**
6-
* Generates a POST request (of Content-type ```application/json```)
7-
* @param {string} path the path, relative to the host, to which this request will be sent
8-
* @param {string} token the access token with which the request should be authenticated
9-
* @param {string} data the data which will be 'POST'ed
10-
* @param {callback} callback
11-
*/
12-
export function postData(path, token, data, callback) {
13-
const options = {
14-
host: host,
15-
path: path,
16-
method: 'POST',
17-
headers: {
18-
'Content-Type': 'application/json',
19-
Authorization: 'Bearer ' + token,
20-
'Content-Length': data.length
21-
}
22-
};
23-
24-
const req = https.request(options, res => {
25-
let subscriptionData = '';
26-
27-
res.on('data', chunk => { subscriptionData += chunk; });
28-
res.on('end', () => {
29-
if (res.statusCode === 201) callback(null, JSON.parse(subscriptionData));
30-
else callback(JSON.parse(subscriptionData), null);
1+
import 'isomorphic-fetch';
2+
import * as MicrosoftGraph from '@microsoft/microsoft-graph-client';
3+
4+
export class SubscriptionManagementService {
5+
constructor(accessToken) {
6+
this.accessToken = accessToken;
7+
this.subscriptionPath = '/subscriptions';
8+
}
9+
10+
getGraphClient() {
11+
const client = MicrosoftGraph.Client.init({
12+
authProvider: (done) => {
13+
done(null, this.accessToken);
14+
}
3115
});
32-
});
33-
34-
req.write(data);
35-
req.end();
36-
37-
req.on('error', error => callback(error, null));
38-
}
39-
40-
/**
41-
* Generates a GET request (of Content-type ```application/json```)
42-
* @param {string} path the path, relative to the host, to which this request will be sent
43-
* @param {string} token the acess token with which the request should be authenticated
44-
* @param {callback} callback
45-
*/
46-
export function getData(path, token, callback) {
47-
const options = {
48-
host: host,
49-
path: path,
50-
method: 'GET',
51-
headers: {
16+
return client;
17+
}
18+
19+
async deleteSubscription(subscriptionId) {
20+
const client = this.getGraphClient();
21+
await client.api(`${this.subscriptionPath}/${subscriptionId}`).delete();
22+
}
23+
24+
async createSubscription(subscriptionCreationInformation) {
25+
const client = this.getGraphClient();
26+
const subscription = await client.api(this.subscriptionPath).create(subscriptionCreationInformation);
27+
return subscription;
28+
}
29+
30+
async getData(path) {
31+
const client = this.getGraphClient();
32+
const result = await client.api(path).headers({
5233
'Content-Type': 'application/json',
5334
Accept: 'application/json;odata.metadata=minimal;'
54-
+ 'odata.streaming=true;IEEE754Compatible=false',
55-
Authorization: 'Bearer ' + token
56-
}
57-
};
58-
59-
const req = https.request(options, res => {
60-
let endpointData = '';
61-
62-
res.on('data', chunk => { endpointData += chunk; });
63-
res.on('end', () => {
64-
if (res.statusCode === 200) callback(null, JSON.parse(endpointData));
65-
else callback(JSON.parse(endpointData), null);
66-
});
67-
});
68-
69-
req.write('');
70-
req.end();
71-
72-
req.on('error', error => callback(error, null));
73-
}
74-
75-
/**
76-
* Generates a DELETE request
77-
* @param {string} path the path, relative to the host, to which this request will be sent
78-
* @param {string} token the acess token with which the request should be authenticated
79-
* @param {callback} callback
80-
*/
81-
export function deleteData(path, token, callback) {
82-
const options = {
83-
host: host,
84-
path: path,
85-
method: 'POST',
86-
headers: {
87-
'Content-Type': 'text/plain',
88-
'X-HTTP-Method': 'DELETE',
89-
Authorization: 'Bearer ' + token
90-
}
91-
};
92-
93-
const req = https.request(options, res => {
94-
res.on('end', () => callback(null));
95-
});
96-
97-
req.end();
98-
99-
req.on('error', error => callback(error));
35+
+ 'odata.streaming=true;IEEE754Compatible=false'
36+
}).get();
37+
return result;
38+
}
10039
}

0 commit comments

Comments
 (0)