Decode Digital Files/Containers
Example Overview
This example showcases the ability to:
-
Decode containers to extract:
-
Full document sets, or
-
Specific files on demand.
-
Here’s a simple Python script that demonstrates how to use the
.env
file to interact with the Locktera API for encoding and decoding documents.
-
-
Prerequisites
1.Install dependencies
pip install requests python-dotenv
2. Create a .env
file (as previously shown) in the same directory.
Decode.py
import os
import requests
from dotenv import load_dotenv
# Load .env variables
load_dotenv()
API_URL = os.getenv("API_URL")
ORG_ID = os.getenv("ORG_ID")
API_KEY = os.getenv("API_KEY")
DECODE_DIR = os.getenv("DECODE_DIR")
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Org-ID": ORG_ID
}
def list_containers():
"""List containers for the organization"""
url = f"{API_URL}/users/me/containers"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
return response.json()
def decode_manifest(container_id):
"""Decode a container manifest"""
url = f"{API_URL}/users/me/containers/{container_id}/decode"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
return response.json()
def decode_file(container_id, file_name, output_dir=DECODE_DIR):
"""Download a specific file from a container"""
url = f"{API_URL}/users/me/containers/{container_id}/decode/{file_name}"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
os.makedirs(output_dir, exist_ok=True)
file_path = os.path.join(output_dir, file_name)
with open(file_path, "wb") as f:
f.write(response.content)
print(f"File saved to {file_path}")
# Example usage
if __name__ == "__main__":
containers = list_containers()
if containers:
first_container = containers[0]
container_id = first_container['id']
manifest = decode_manifest(container_id)
print("Decoded manifest:", manifest)
# Download the first file in the manifest (if exists)
files = manifest.get("files", [])
if files:
decode_file(container_id, files[0]['name'])
This script:
-
Loads environment variables.
-
Lists the user's containers.
-
Decodes a manifest from the first container.
-
Downloads a file from the container.