Added d4s download method

This commit is contained in:
Vincenzo Cestone 2022-03-30 11:40:05 +02:00
parent 33f4c25e5d
commit ac1f18cf2d
2 changed files with 79 additions and 43 deletions

View File

@ -1,4 +1,12 @@
/**
* D4Science boot component that handles keykloak authz
* <d4s-boot-2
* /@url: keycloak url, for example: https://accounts.pre.d4science.org/auth]
* /@realm: is the realm [defaults to: d4science]
* /@gateway: is the client id, for example: "pre.d4science.org"
* /@context: is the audience, for example: "%2Fpred4s%2Fpreprod%2FpreVRE"
* /@redirect-url: where to go on logout
*/
window.customElements.define('d4s-boot-2', class extends HTMLElement {
#keycloak = null
@ -15,7 +23,6 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
constructor() {
super()
//this.attachShadow({mode: "closed"})
}
connectedCallback() {
@ -63,7 +70,7 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
}).catch(function(err) {
console.error("Failed to initialize d4science boot component")
console.error(err)
})
}
@ -130,6 +137,32 @@ window.customElements.define('d4s-boot-2', class extends HTMLElement {
})
}
download(endpoint, name) {
this.#keycloak.updateToken(30).then(() => {
const audience = encodeURIComponent(this.#audience)
this.#authorization.entitlement(audience).then(function (rpt) {
fetch(endpoint, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + rpt
}
})
.then(response => response.blob())
.then(blob => {
const objectURL = URL.createObjectURL(blob)
var tmplnk = document.createElement("a")
tmplnk.download = name
tmplnk.href = objectURL
document.body.appendChild(tmplnk)
tmplnk.click()
document.body.removeChild(tmplnk)
})
})
}).catch(function() {
onForbidden('Failed to refresh token')
})
}
logout() {
if (this.#keycloak) {
if (!this.#redirectUrl) {

View File

@ -1,42 +1,6 @@
window.customElements.define('d4s-social-search', class extends HTMLElement {
constructor() {
super()
}
connectedCallback() {
//console.log(typeof d4s)
//console.log(this)
//this.attachShadow({mode: "open"})
//console.log(this.shadowRoot.querySelector("*[name=search]"))
//const shadowRoot = this.attachShadow({mode: 'open'})
//console.log(shadowRoot.innerHTML)
console.log("d4s-social-search connectedCallback")
var d4s = document.querySelector('d4s-boot-2')
/*
d4s.service(
'https://api.dev.d4science.org/rest/2/full-text-search/search-by-query?query=d4science',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
d4s.service(
'https://api.dev.d4science.org/rest/2/posts/get-posts-user-quantity',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
*/
}
})
/**
* Prints VRE d4s-social-posts (limiting to @quantity number)
*/
window.customElements.define('d4s-social-posts', class extends HTMLElement {
#basepath = 'https://api.d4science.org/rest'
@ -122,3 +86,42 @@ window.customElements.define('d4s-social-posts', class extends HTMLElement {
this.setAttribute("quantity", quantity)
}
})
window.customElements.define('d4s-social-search', class extends HTMLElement {
constructor() {
super()
}
connectedCallback() {
//console.log(typeof d4s)
//console.log(this)
//this.attachShadow({mode: "open"})
//console.log(this.shadowRoot.querySelector("*[name=search]"))
//const shadowRoot = this.attachShadow({mode: 'open'})
//console.log(shadowRoot.innerHTML)
console.log("d4s-social-search connectedCallback")
var d4s = document.querySelector('d4s-boot-2')
/*
d4s.service(
'https://api.dev.d4science.org/rest/2/full-text-search/search-by-query?query=d4science',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
d4s.service(
'https://api.dev.d4science.org/rest/2/posts/get-posts-user-quantity',
'GET', null,
(resp) => {
document.getElementById('output').innerText = resp
},
() => { alert('Forbidden') }
)
*/
}
})