Files¶
Read and change repository files by numeric project ID and repository-relative path. Pass paths as ordinary strings; the client URL-encodes them.
Setup¶
These snippets share this client. Replace the URL and token with your DataHub details. The complete sample files use offline responses when run by the documentation checks.
#r "nuget: DataHubClient"
open DataHubClient
let client =
DataHubClient("https://hub.example", Authentication.PersonalAccessToken("your-datahub-pat"))
import { DataHubClient } from "@nfdi4plants/datahub-client";
import { Authentication } from "@nfdi4plants/datahub-client/Http/Authentication.js";
const client = new DataHubClient(
"https://hub.example", Authentication.PersonalAccessToken("your-datahub-pat"));
import asyncio
import base64
from datahub_client import DataHubClient, Authentication
client = DataHubClient(
"https://hub.example", Authentication.PersonalAccessToken("your-datahub-pat"))
GetAsync¶
Read a file from a branch, tag, or commit SHA. Content is usually base64-encoded; check Encoding before decoding. These text-file examples use UTF-8; the JavaScript and TypeScript examples use Node.js Buffer.
let file =
client.Files.GetAsync(42, "README.md", "main") |> Async.RunSynchronously
let text =
if file.Encoding = "base64" then
System.Convert.FromBase64String(file.Content) |> System.Text.Encoding.UTF8.GetString
else file.Content
printfn "%s" text
const file = await client.Files.GetAsync(42, "README.md", "main");
const text = file.Encoding === "base64"
? Buffer.from(file.Content, "base64").toString("utf8") : file.Content;
console.log(text);
async def get_example():
file = await client.Files.GetAsync(42, "README.md", "main")
text = (base64.b64decode(file.Content).decode("utf-8")
if file.Encoding == "base64" else file.Content)
print(text)
asyncio.run(get_example())
CreateAsync¶
Create a file and a commit on the target branch. Omit encoding for plain text; pass "base64" when the supplied content is encoded. The method reads the file back after the write and returns a RepoFile.
let created =
client.Files.CreateAsync(42, "notes.txt", "main", "ARC", "Add notes") |> Async.RunSynchronously
printfn "Created %s on %s" created.FilePath created.Ref
const created = await client.Files.CreateAsync(42, "notes.txt", "main", "ARC", "Add notes");
console.log(`Created ${created.FilePath} on ${created.Ref}`);
async def create_example():
created = await client.Files.CreateAsync(42, "notes.txt", "main", "ARC", "Add notes")
print(f"Created {created.FilePath} on {created.Ref}")
asyncio.run(create_example())
UpdateAsync¶
Replace a file and commit the change. This example supplies base64 content. As with creation, a second request fetches the resulting RepoFile; a read-back failure is reported even if the write succeeded.
let updated =
client.Files.UpdateAsync(42, "notes.txt", "main", "QVJD", "Update notes", encoding = "base64") |> Async.RunSynchronously
printfn "Updated %s at %s" updated.FilePath updated.CommitId
const updated = await client.Files.UpdateAsync(42, "notes.txt", "main", "QVJD", "Update notes", "base64");
console.log(`Updated ${updated.FilePath} at ${updated.CommitId}`);
async def update_example():
updated = await client.Files.UpdateAsync(42, "notes.txt", "main", "QVJD", "Update notes", encoding="base64")
print(f"Updated {updated.FilePath} at {updated.CommitId}")
asyncio.run(update_example())
DeleteAsync¶
Delete a file and create a commit. Completion confirms success; the method has no result value.
client.Files.DeleteAsync(42, "notes.txt", "main", "Remove notes") |> Async.RunSynchronously
printfn "Deleted notes.txt"
await client.Files.DeleteAsync(42, "notes.txt", "main", "Remove notes");
console.log("Deleted notes.txt");
async def delete_example():
await client.Files.DeleteAsync(42, "notes.txt", "main", "Remove notes")
print("Deleted notes.txt")
asyncio.run(delete_example())