Skip to content

Commit a416b02

Browse files
authored
Document/#11 update readme (#14)
* docs: Add Error types in README.md (#11) * chore: Install @types/node * docs: Add a login example in README.md (#11) * docs: Add a login example in README.md (#11) * docs: Modify login example in README.md * docs: Fix login example error catch in README.md * feat: Add a login example with client-js SDK * docs: Fix client clonfig example (#11) * docs: Add connection mode options in README.md (#11) * docs: Fix a typo * feat: Add head tag in login.html * fix: Modify to html stardard
1 parent df94ade commit a416b02

4 files changed

Lines changed: 151 additions & 0 deletions

File tree

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,45 @@ client.createIfNotExists('python:latest', 'my-session-id')
106106
The result objects returned with success has different formats API by API.
107107
Please check out [our official documentation](https://docs.backend.ai/).
108108

109+
When using backend.ai, you can use with `SESSION` mode or `API` mode.
110+
If you want to use backend.ai with `SESSION` mode you need to input `user_id`, `password`, `api_endpoint` and `SESSION` to specify the mode.
111+
If you want to use backend.ai with `API` mode you need to input `ACCESS_KEY` and `SECRET_KEY` instead of `user_id` and `password`. Also you don't need to input the mode because `API` mode is default value.
112+
113+
```javascript
114+
async Login() {
115+
config = new ai.backend.ClientConfig(
116+
'[ADD_USER_ID_HERE]',
117+
'[ADD_PASSWORD_HERE]',
118+
'[ENDPOINT_HERE]',
119+
'[CONNECTION_MODE_HERE]'
120+
);
121+
client = new ai.backend.Client(
122+
config,
123+
`Backend.AI Console.`,
124+
);
125+
let isLogon = await client.check_login();
126+
if (isLogon === false) {
127+
client.login().then(reponse => {
128+
if (reponse === false) {
129+
if (user_id != '' && password != '') {
130+
console.log(`Login information mismatch. Please check your login information.`);
131+
}
132+
} else if (reponse.fail_reason) {
133+
if (user_id != '' && password != '') {
134+
console.log(`Login failed: ${response.fail_reason}`);
135+
}
136+
} else {
137+
console.log(`Login succeeded.`);
138+
}
139+
}).catch(err => {
140+
console.log(`Login failed: ${err.message}`);
141+
});
142+
} else {
143+
console.log(`Login already succeeded.`)
144+
}
145+
}
146+
```
147+
109148
`err.type` is one of the following values:
110149

111150
* `ai.backend.Client.ERR_SERVER`: The server responded with failure.
@@ -115,3 +154,8 @@ Please check out [our official documentation](https://docs.backend.ai/).
115154
`err.message` includes an exception value passed from your Javascript runtime.
116155
* `ai.backend.Client.ERR_REQUEST`: An error occurred while sending the request.
117156
`err.message` includes an exception value passed from your Javascript runtime.
157+
* `ai.backend.Client.ERR_ABORT`: An error occurred while request aborted by user.
158+
* `ai.backend.Client.ERR_TIMEOUT`: An error occurred while no response returned during the timeout period.
159+
* `ai.backend.Client.ERR_UNKNOWN`: An error occurred while unknown error occurs.
160+
In this case, `err.message` includes HTTP status and additional error information
161+
returned by the API server.

examples/login.html

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
5+
<meta name="Description" content="This is a backend.ai node.js SDK example."/>
6+
<title>Backend.AI node.js SDK Example</title>
7+
</head>
8+
<body>
9+
<h3>
10+
Backend.AI node.js SDK Example
11+
</h3>
12+
<div>
13+
<h4>Login</h4>
14+
<form>
15+
API Endpoint: <br/>
16+
<input type="text" id="api_endpoint" placeholder="https://cloud.backend.ai" value="https://cloud.backend.ai" /><br/>
17+
ID: <br/>
18+
<input type="text" id="user_id" autocomplete="username" placeholder="Username"/><br/>
19+
PW: <br/>
20+
<input type="password" id="password" autocomplete="current-password" placeholder="Password"/><br/><br/>
21+
<input type="button" id="login_button" value="Login" />
22+
</form>
23+
</div>
24+
<div>
25+
<h4>DL Code</h4>
26+
<textarea id="dl_code" style="height: 300px; width: 500px" placeholder="Write your DL code."></textarea><br/>
27+
<button id="dl_run">Run</button>
28+
<h4>DL Result</h4>
29+
<div id="dl_result"></div>
30+
</div>
31+
<script type="module">
32+
// Can be changed according to your directory path.
33+
import "./backend.ai-client-js/backend.ai-client-es6.js";
34+
document.querySelector("#login_button").addEventListener("click", login.bind(this));
35+
document.querySelector("#dl_run").addEventListener("click", execute.bind(this));
36+
let client;
37+
38+
export async function login() {
39+
// Create a client configuration object.
40+
let config = new ai.backend.ClientConfig(
41+
document.querySelector("#user_id").value,
42+
document.querySelector("#password").value,
43+
document.querySelector("#api_endpoint").value,
44+
"SESSION",
45+
);
46+
47+
// Create a client object with configuration object.
48+
client = new ai.backend.Client(
49+
config,
50+
"Backend.AI ES6 App.",
51+
);
52+
53+
let isLogon = await client.check_login();
54+
if (isLogon === false) {
55+
client.login().then(response => {
56+
if (response === false) {
57+
if (config._userId != "" && config._password != "") {
58+
console.log("Login information mistmatch. Please check your login information,");
59+
} else if (response.fail_reason) {
60+
if (config._userId != "" && config._password != "") {
61+
console.log(`Login falied: ${response.fail_reason}.`);
62+
}
63+
} else {
64+
console.log("Login succeeded.");
65+
}
66+
}
67+
}).catch(err => {
68+
console.log(`Login failed: ${err.message}`);
69+
});
70+
} else {
71+
console.log("Login already succeeded.");
72+
}
73+
}
74+
75+
export async function execute() {
76+
if (typeof client === "undefined") {
77+
await login();
78+
}
79+
let version = await client.getManagerVersion();
80+
let code = document.querySelector("#dl_code").value;
81+
client.createIfNotExists("index.docker.io/lablup/python:3.8-ubuntu18.04", "test", {"cpu": 1, "mem": "1g"}).then(response => {
82+
console.log(`My session is created: ${response.sessionId}`);
83+
return client.execute(response.sessionId, 1, "query", code, {});
84+
}).then(response => {
85+
if (response.result.exitCode === 0) {
86+
console.log( response.result.console);
87+
document.querySelector("#dl_result").innerHTML = response.result.console[0][1];
88+
}
89+
}).catch(err => {
90+
switch (err.type) {
91+
case ai.backend.Client.ERR_SERVER:
92+
console.log(`Session creation failed: ${err.message}`);
93+
break;
94+
default:
95+
console.log(`request/response failed: ${err.message}`);
96+
}
97+
});
98+
}
99+
</script>
100+
</body>
101+
</html>

package-lock.json

Lines changed: 5 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"homepage": "https://github.com/lablup/backend.ai-client-js",
2929
"dependencies": {
30+
"@types/node": "^14.0.27",
3031
"node-fetch": "^2.6.0"
3132
},
3233
"devDependencies": {

0 commit comments

Comments
 (0)