Skip to content

Commit

Permalink
Added fetching of job settings to client
Browse files Browse the repository at this point in the history
  • Loading branch information
zarya committed Nov 9, 2024
1 parent 5bc33b8 commit 7c887f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/static_src/js/oauthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ class OauthClient {
* Create OAuth Client instance.
*
* @param {string} client_id - oauth client_id
* @param {function} callback - Has token callback
*/
constructor(client_id) {
constructor(client_id, callback) {
this.token = undefined;
this.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
this.callback = callback;
this.getToken(client_id);
}

Expand Down Expand Up @@ -148,6 +150,7 @@ class OauthClient {
const token = await this.sendTokenRequest(auth[0], client_id, verifier);
this.fullToken = token;
this.token = token.access_token;
if (this.callback) this.callback(this.token);
return this.token;
}
}
Expand Down
35 changes: 33 additions & 2 deletions src/static_src/js/uploadClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UploadClient {
this.queue = []
this.client_id = client_id
this.client_uuid = ""
this.oauth = new OauthClient(client_id);
this.oauth = new OauthClient(client_id, (token) => this.oauthReady(token));
this.finished = [];
const cookie = this.getCookie("uc_uuid");
if (cookie) {
Expand All @@ -20,6 +20,15 @@ class UploadClient {
}
}

/**
* OAuth token callback
*
* @param {string} token - OAuth token
*/
oauthReady(_token) {
this.getClientConfig();
}

/**
* Add uploaded file to process queue
*
Expand All @@ -46,7 +55,7 @@ class UploadClient {
maxWidth: width,
maxHeight: height,
mimeType: type,
convertSize: 50000000,
convertSize: -1,
success(result) {
resolve(result);
},
Expand Down Expand Up @@ -329,4 +338,26 @@ class UploadClient {
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}

/**
* Get client configuration from server.
*
*/
async getClientConfig() {
try {
const response = await fetch(`/api/v1/json/jobs/settings/`, {
headers: {
"Authorization": `Bearer ${this.oauth.token}`,
},
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
this.finished = [];
const json = await response.json();
this.config = json.bma_response;
} catch (error) {
console.error(error.message);
}
}
}

0 comments on commit 7c887f0

Please sign in to comment.