//native
/**
* Import Workspace Object
* Import a notebook or file into the workspace. Pass content as plain text — it is base64-encoded for you. For SOURCE format set the language (PYTHON, SQL, SCALA, R).
*/
export async function main(
auth: RT.Databricks,
path: string,
content: string,
format: "SOURCE" | "HTML" | "JUPYTER" | "DBC" | "AUTO" = "SOURCE",
language: "PYTHON" | "SQL" | "SCALA" | "R" | undefined,
overwrite: boolean = false
) {
const base = auth.workspace_url.replace(/\/$/, "")
const url = new URL(`${base}/api/2.0/workspace/import`)
// base64-encode the UTF-8 content without overflowing the call stack on large inputs
const bytes = new TextEncoder().encode(content)
let binary = ""
for (const b of bytes) binary += String.fromCharCode(b)
const encoded = btoa(binary)
const body: { [key: string]: any } = {
path,
content: encoded,
format,
overwrite,
}
if (language !== undefined) body.language = language
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const text = await response.text()
return text ? JSON.parse(text) : { success: true }
}
Submitted by hugo989 5 days ago