Integrate VIUCraft with your Python applications.
Using Requests
Python
import os
import requests
API_KEY = os.environ.get(VIUCRAFT_API_KEY)
BASE_URL = https://api.viucraft.com
def upload_image(file_path: str) -> dict:
headers = {X-API-Key: API_KEY}
with open(file_path, rb) as f:
files = {image: f}
response = requests.post(
f{BASE_URL}/upload,
headers=headers,
files=files
)
response.raise_for_status()
return response.json()
VIUCraft Client Class
Python
class VIUCraftClient:
def __init__(self, api_key=None, subdomain=yoursubdomain):
self.api_key = api_key or os.environ.get(VIUCRAFT_API_KEY)
self.subdomain = subdomain
self.base_url = https://api.viucraft.com
self.session = requests.Session()
self.session.headers[X-API-Key] = self.api_key
def upload(self, file_path: str) -> dict:
with open(file_path, rb) as f:
response = self.session.post(
f{self.base_url}/upload,
files={image: f}
)
response.raise_for_status()
return response.json()
def get_url(self, image_id: str, operations=None) -> str:
operation_path = /.join(operations) if operations else
if operation_path:
return f https://{self.subdomain}.viucraft.com/{operation_path}/{image_id}.jpg
return fhttps://{self.subdomain}.viucraft.com/{image_id}.jpg