SDK
SDK Python
Installation, utilisation sync et async, exemples complets.
Version
Package PyPI `coffrify` - Python ≥ 3.10. Client sync et async (`asyncio`) inclus.
Installation
terminal
$ pip install coffrifySuccessfully installed coffrify-0.2.0
Client synchrone
main.pypython
import coffrify, os
client = coffrify.Client(
api_key=os.environ['COFFRIFY_API_KEY'],
timeout=30, # secondes
max_retries=2,
)
transfer = client.transfers.create(
files=[{'name': 'rapport.pdf', 'size': 1_048_576}],
expires_in='7d',
password='secret',
)
print(transfer.short_code) # "X9aB2c"
print(transfer.share_url)Upload & finalisation
upload.pypython
import httpx
with open('rapport.pdf', 'rb') as f:
httpx.put(transfer.upload_urls[0], content=f.read())
finalized = client.transfers.finalize(transfer.id)
print(finalized.status) # 'active'Client asynchrone (asyncio)
async_main.pypython
import asyncio
import coffrify, os
async def main():
async with coffrify.AsyncClient(api_key=os.environ['COFFRIFY_API_KEY']) as client:
transfer = await client.transfers.create(
files=[{'name': 'doc.pdf', 'size': 512_000}],
expires_in='30d',
)
print(transfer.share_url)
asyncio.run(main())Gestion des erreurs
from coffrify.exceptions import CoffrifyAPIError, CoffrifyRateLimitError
try:
transfer = client.transfers.create(files=[])
except CoffrifyRateLimitError as e:
print(f'Retry after {e.retry_after}s')
except CoffrifyAPIError as e:
print(f'{e.status_code} {e.code}: {e.message}')