Skip to content

Commit c10aec8

Browse files
Merge pull request #3 from odeimaiz/odeimaiz-refactor/api-keys-service
🎨 [Frontend] Adapt to new ``api-keys`` API
2 parents 5b22949 + 9b937d0 commit c10aec8

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ qx.Class.define("osparc.data.Resources", {
763763
},
764764
delete: {
765765
method: "DELETE",
766-
url: statics.API + "/auth/api-keys"
766+
url: statics.API + "/auth/api-keys/{apiKeyId}"
767767
}
768768
}
769769
},

services/static-webserver/client/source/class/osparc/desktop/preferences/pages/TokensPage.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
7777
const formData = e.getData();
7878
const params = {
7979
data: {
80-
"display_name": formData["name"]
80+
"displayName": formData["name"]
8181
}
8282
};
8383
if (formData["expiration"]) {
@@ -90,14 +90,17 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
9090
.then(data => {
9191
this.__rebuildAPIKeysList();
9292

93-
const key = data["api_key"];
94-
const secret = data["api_secret"];
95-
const showAPIKeyWindow = new osparc.desktop.preferences.window.ShowAPIKey(key, secret);
93+
const key = data["apiKey"];
94+
const secret = data["apiSecret"];
95+
const baseUrl = data["apiBaseUrl"];
96+
const showAPIKeyWindow = new osparc.desktop.preferences.window.ShowAPIKey(key, secret, baseUrl);
9697
showAPIKeyWindow.center();
9798
showAPIKeyWindow.open();
9899
})
99100
.catch(err => {
100-
osparc.FlashMessenger.getInstance().logAs(err.message, "ERROR");
101+
const errorMsg = err.message || this.tr("Cannot create API Key");
102+
osparc.FlashMessenger.getInstance().logAs(errorMsg, "ERROR");
103+
console.error(err);
101104
})
102105
.finally(() => this.__requestAPIKeyBtn.setFetching(false));
103106
}, this);
@@ -109,26 +112,24 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
109112
osparc.data.Resources.get("apiKeys")
110113
.then(apiKeys => {
111114
apiKeys.forEach(apiKey => {
112-
const apiKeyForm = this.__createValidAPIKeyForm(apiKey);
115+
const apiKeyForm = this.__createAPIKeyEntry(apiKey);
113116
this.__apiKeysList.add(apiKeyForm);
114117
});
115118
})
116119
.catch(err => console.error(err));
117120
},
118121

119-
__createValidAPIKeyForm: function(apiKeyLabel) {
122+
__createAPIKeyEntry: function(apiKey) {
120123
const grid = this.__createValidEntryLayout();
121124

122-
const nameLabel = new qx.ui.basic.Label(apiKeyLabel);
125+
const nameLabel = new qx.ui.basic.Label(apiKey["displayName"]);
123126
grid.add(nameLabel, {
124127
row: 0,
125128
column: 0
126129
});
127130

128131
const delAPIKeyBtn = new qx.ui.form.Button(null, "@FontAwesome5Solid/trash/14");
129-
delAPIKeyBtn.addListener("execute", e => {
130-
this.__deleteAPIKey(apiKeyLabel);
131-
}, this);
132+
delAPIKeyBtn.addListener("execute", () => this.__deleteAPIKey(apiKey["id"]), this);
132133
grid.add(delAPIKeyBtn, {
133134
row: 0,
134135
column: 1
@@ -137,7 +138,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
137138
return grid;
138139
},
139140

140-
__deleteAPIKey: function(apiKeyLabel) {
141+
__deleteAPIKey: function(apiKeyId) {
141142
if (!osparc.data.Permissions.getInstance().canDo("user.apikey.delete", true)) {
142143
return;
143144
}
@@ -153,13 +154,17 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
153154
win.addListener("close", () => {
154155
if (win.getConfirmed()) {
155156
const params = {
156-
data: {
157-
"display_name": apiKeyLabel
157+
url: {
158+
"apiKeyId": apiKeyId
158159
}
159160
};
160161
osparc.data.Resources.fetch("apiKeys", "delete", params)
161162
.then(() => this.__rebuildAPIKeysList())
162-
.catch(err => console.error(err));
163+
.catch(err => {
164+
const errorMsg = err.message || this.tr("Cannot delete API Key");
165+
osparc.FlashMessenger.getInstance().logAs(errorMsg, "ERROR");
166+
console.error(err)
167+
});
163168
}
164169
}, this);
165170
},
@@ -203,7 +208,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
203208
const supportedExternalServices = osparc.utils.Utils.deepCloneObject(this.__supportedExternalServices());
204209

205210
tokensList.forEach(token => {
206-
const tokenForm = this.__createValidTokenEntry(token);
211+
const tokenForm = this.__createTokenEntry(token);
207212
this.__validTokensGB.add(tokenForm);
208213
const idx = supportedExternalServices.findIndex(srv => srv.name === token.service);
209214
if (idx > -1) {
@@ -244,7 +249,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
244249
.catch(err => console.error(err));
245250
},
246251

247-
__createValidTokenEntry: function(token) {
252+
__createTokenEntry: function(token) {
248253
const grid = this.__createValidEntryLayout();
249254

250255
const service = token["service"];

services/static-webserver/client/source/class/osparc/desktop/preferences/window/ShowAPIKey.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
1717
extend: osparc.desktop.preferences.window.APIKeyBase,
1818

19-
construct: function(key, secret) {
19+
construct: function(key, secret, baseUrl) {
2020
const caption = this.tr("API Key");
2121
const infoText = this.tr("For your protection, store your access keys securely and do not share them. You will not be able to access the key again once this window is closed.");
2222
this.base(arguments, caption, infoText);
@@ -25,39 +25,60 @@ qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
2525
clickAwayClose: false
2626
});
2727

28-
this.__populateTokens(key, secret);
28+
this.__populateTokens(key, secret, baseUrl);
2929
},
3030

3131
members: {
32-
__populateTokens: function(key, secret) {
33-
const hBox1 = this.__createEntry(this.tr("<b>Key:</b>"), key);
32+
__populateTokens: function(key, secret, baseUrl) {
33+
const hBox1 = this.__createStarredEntry(this.tr("<b>Key:</b>"), key);
3434
this._add(hBox1);
3535

36-
const hBox2 = this.__createEntry(this.tr("<b>Secret:</b>"), secret);
36+
const hBox2 = this.__createStarredEntry(this.tr("<b>Secret:</b>"), secret);
3737
this._add(hBox2);
3838

39-
const hBox3 = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
39+
const hBox3 = this.__createEntry(this.tr("<b>Base url:</b>"), baseUrl);
40+
this._add(hBox3);
41+
42+
const buttonsLayout = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
4043
appearance: "margined-layout"
4144
});
42-
const copyAPIKeyBtn = new qx.ui.form.Button(this.tr("Copy API Key"));
45+
const copyAPIKeyBtn = new qx.ui.form.Button(this.tr("API Key"), "@FontAwesome5Solid/copy/12");
4346
copyAPIKeyBtn.addListener("execute", e => {
4447
if (osparc.utils.Utils.copyTextToClipboard(key)) {
4548
copyAPIKeyBtn.setIcon("@FontAwesome5Solid/check/12");
4649
}
4750
});
48-
hBox3.add(copyAPIKeyBtn, {
49-
width: "50%"
51+
buttonsLayout.add(copyAPIKeyBtn, {
52+
flex: 1
5053
});
51-
const copyAPISecretBtn = new qx.ui.form.Button(this.tr("Copy API Secret"));
54+
const copyAPISecretBtn = new qx.ui.form.Button(this.tr("API Secret"), "@FontAwesome5Solid/copy/12");
5255
copyAPISecretBtn.addListener("execute", e => {
5356
if (osparc.utils.Utils.copyTextToClipboard(secret)) {
5457
copyAPISecretBtn.setIcon("@FontAwesome5Solid/check/12");
5558
}
5659
});
57-
hBox3.add(copyAPISecretBtn, {
58-
width: "50%"
60+
buttonsLayout.add(copyAPISecretBtn, {
61+
flex: 1
5962
});
60-
this._add(hBox3);
63+
const copyBaseUrlBtn = new qx.ui.form.Button(this.tr("Base URL"), "@FontAwesome5Solid/copy/12");
64+
copyBaseUrlBtn.addListener("execute", e => {
65+
if (osparc.utils.Utils.copyTextToClipboard(baseUrl)) {
66+
copyBaseUrlBtn.setIcon("@FontAwesome5Solid/check/12");
67+
}
68+
});
69+
buttonsLayout.add(copyBaseUrlBtn, {
70+
flex: 1
71+
});
72+
this._add(buttonsLayout);
73+
},
74+
75+
__createStarredEntry: function(title, label) {
76+
const hBox = this.__createEntry(title);
77+
if (label) {
78+
// partially hide the key and secret
79+
hBox.getChildren()[1].setValue(label.substring(1, 8) + "****")
80+
}
81+
return hBox;
6182
},
6283

6384
__createEntry: function(title, label) {
@@ -66,13 +87,13 @@ qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
6687
});
6788
const sTitle = new qx.ui.basic.Label(title).set({
6889
rich: true,
69-
width: 40
90+
width: 60
7091
});
7192
hBox.add(sTitle);
7293
const sLabel = new qx.ui.basic.Label();
7394
if (label) {
7495
// partially hide the key and secret
75-
sLabel.setValue(label.substring(1, 8) + "****")
96+
sLabel.setValue(label);
7697
}
7798
hBox.add(sLabel);
7899
return hBox;

0 commit comments

Comments
 (0)