Repository navigation
Expand file tree
/
Copy pathAdminControl.ts
More file actions
95 lines (80 loc) · 2.49 KB
/
Copy pathAdminControl.ts
File metadata and controls
95 lines (80 loc) · 2.49 KB
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
class AdminControl extends HTMLElement {
#oauthHasBeenInitialised: boolean;
#clientId: string;
#clientSecret: string;
#currentAccessToken: string;
#tokenUrl: string;
#baseApiUrl: string;
connectedCallback() {
console.log("Connected callback called!");
this.innerHTML = '<h1>Hello World.</h1>'
console.log("testing *");
}
constructor() {
debugger;
console.log("Constructor called");
super();
this.#oauthHasBeenInitialised = false;
}
async InitializeOauth(): Promise<string> {
console.log("InitializeOauth called");
debugger;
try {
var oauthResponse = await fetch(this.#tokenUrl, {
method: 'POST',
body: 'grant_type=client_credentials&client_id=' + this.#clientId + '&client_secret=' + this.#clientSecret,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
var oauthResult = await oauthResponse.json();
return oauthResult.access_token;
}
catch (e) {
console.log("Exception when trying to get OAuth: " + e);
throw (e);
}
}
async InitializeApi(baseApiUrl: string, tokenUrl: string, clientId: string, clientSecret: string) {
debugger;
if(this.#oauthHasBeenInitialised)return;
this.#oauthHasBeenInitialised = false;
this.#baseApiUrl = baseApiUrl;
this.#tokenUrl = tokenUrl;
this.#clientId = clientId;
this.#clientSecret = clientSecret;
this.#currentAccessToken = "";
try {
this.#currentAccessToken = await this.InitializeOauth();
console.log("OAuth successful");
this.#oauthHasBeenInitialised = true;
}
catch (e) {
console.log("Could not initialize the connection " + e);
}
}
//currently defaults to GET method
async RunApiQuery(resourceUrl: string): Promise<string> {
debugger;
try {
if (!this.#oauthHasBeenInitialised) {
throw new Error("Please Initialize before using RunApiQuery using InitializeApi function");
}
var fullUrlAddress = this.#baseApiUrl + resourceUrl;
const response = await fetch(fullUrlAddress, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + this.#currentAccessToken,
'x-sap-sac-custom-auth': 'true',
'x-csrf-token': 'fetch'
}
});
const data = await response.json();
return JSON.stringify(data, null, "\t");
} catch (error) {
console.log(error);
return "";
}
}
}
customElements.define('corr-admin-control', AdminControl);