الدليل الشامل لحزمة Python الرسمية (hosteday_python)
حزمة
hosteday_pythonالرسمية هي مكتبة غير متزامنة بالكامل (Async-First) ومُدققة الأنواع وفق PEP 561 (mypy --strict) لربط تطبيقات Python ووكلاء الذكاء الاصطناعي وتطبيقات سطح المكتب وواجهات CLI بمنصة HosteDay عبرhosteday_config.jsonأو متغيرات البيئة، مع دعم قياسي لـ CRUD والمصادقة والبث اللحظي WebSockets والإشعارات والمهام الخلفية ومحرك ويب مدمجHostedayWebApp.
- صفحة الحزمة الرسمية: HosteDay Python SDK
- صفحة الحزمة على PyPI: pypi.org/project/hosteday_python
- مستودع GitHub: mustafa3max/hosteday-python
- الإصدار الحالي:
v0.1.0| المتطلبات الدنيا:Python >= 3.10| الترخيص:MIT
1. التثبيت وأداة التهيئة السحابية (hosteday-setup)
أ. التثبيت القياسي داخل بيئة افتراضية (موصى به)
python3 -m venv .venv
source .venv/bin/activate # Linux / macOS
# <span id="heading-95fffe4035bd1eb79636ad62caba6d02"></span>.venv\Scripts\activate # Windows PowerShell
pip install hosteday_python
تثبيت الإضافات الاختيارية عند الحاجة:
pip install "hosteday_python[crypto]" # القنوات الخاصة المشفّرة (PyNaCl)
pip install "hosteday_python[encrypted]" # حفظ الجلسة في OS Keyring
pip install "hosteday_python[cli]" # تنسيق غني لأداة سطر الأوامر (Rich)
pip install "hosteday_python[all]" # جميع الإضافات الاختيارية
ب. حل خطأ externally-managed-environment (PEP 668) على Ubuntu/Debian
في توزيعات Ubuntu 23.04+ و Debian 12+ يمنع النظام تثبيت pip عالمياً مباشرةً:
- للمشاريع: استخدم دائماً بيئة افتراضية
.venv. - لتثبيت أداة
hosteday-setupعالمياً: استخدمpipx:
sudo apt install pipx
pipx ensurepath
pipx install hosteday_python
ج. التهيئة السحابية الصفرية عبر hosteday-setup
تتصل أداة hosteday-setup بلوحة التحكم (https://dash.hosteday.com/api/cli/manifest) وتنشئ ملف الإعدادات بصلاحيات مشددة 0600:
# <span id="heading-e6e3b8be4f2077ecb4fa6244720260db"></span>توليد hosteday_config.json (الافتراضي)
hosteday-setup --project my-app --token "<setup_token>" --output hosteday_config.json
# <span id="heading-c35bd47d2dfc235d0008a60eab5c3e2f"></span>أو توليد ملف .env بمتغيرات HOSTEDAY_*
hosteday-setup --project my-app --token "<setup_token>" --format env --output .env
تنبيه أمني: لا تقم أبداً برفع
hosteday_config.jsonأوfirebase-config.jsonأو.hosteday_session.jsonأو.envإلى مستودع Git، وأضفها دائماً إلى.gitignore.
2. التهيئة السريعة وإدارة دورة الحياة (Hosteday و HostedayClient)
يمكن الاستيراد عبر from hosteday import Hosteday أو from hosteday_python import Hosteday:
import asyncio
from pathlib import Path
from hosteday import Hosteday, HostedayClient, HostedayConfig, JsonFileHostedayAuthStorage
async def main() -> None:
# 1. الواجهة الموحدة (Singleton) مع اكتشاف تلقائي لملف firebase-config.json المجاور
await Hosteday.initialize_app(
config_path=Path("hosteday_config.json"),
auth_storage=JsonFileHostedayAuthStorage(Path(".hosteday_session.json")),
)
print("Online:", Hosteday.is_online, "| Host:", Hosteday.client.host)
await Hosteday.dispose()
# 2. أو تشغيل عدة مشاريع معزولة في نفس العملية عبر async with ومتغيرات HOSTEDAY_*
env_cfg = HostedayConfig.from_env()
async with HostedayClient(config=env_cfg) as client:
print("Isolated project:", client.api_base_url)
asyncio.run(main())
الروابط الثابتة وملفات التخزين العام
تطبّق الحزمة حماية أصل النطاق (Origin Lock) وتمنع Path Traversal وتضيف بادئة /api تلقائياً:
from hosteday import Hosteday
privacy_url = Hosteday.client.static_pages["privacy"] # https://my-app.hosteday.com/privacy
avatar_url = Hosteday.client.storage_url("avatars/1.png") # https://my-app.hosteday.com/storage/avatars/1.png
3. عمليات CRUD القياسية وباني الاستعلامات (index, show, create, update, delete)
جميع المسارات لا تحتاج إلى كتابة /api/ يدوياً، وتفك الحزمة غلاف الاستجابة تلقائياً (HostedayResponseReader) مع دعم الترقيم HostedayPage وفلاتر Laravel (QueryBuilder) والبديل التلقائي من PATCH إلى PUT:
from hosteday import Hosteday
# <span id="heading-1299db216b68ec1a474d20cb999607f2"></span>قراءة مقسّمة مع بحث وفلاتر متعددة القيم
page = await Hosteday.client.index(
"notes",
page=1,
per_page=15,
search="meeting",
filters={"status": ["active", "pinned"]},
with_auth=True,
)
for item in page.items:
print(item["id"], item["data"])
# <span id="heading-d089c6bf0fd40cb39545535c528b3e57"></span>جلب سجل واحد مباشرةً
record = await Hosteday.client.show("notes", id=page.items[0]["id"], with_auth=True)
# <span id="heading-dd2778c78543c3207ab05e1c751f263f"></span>إنشاء سجل جديد (يُرجع القاموس المفكوك مباشرةً)
created = await Hosteday.client.create("notes", body={"data": "Hello HosteDay!"})
# <span id="heading-c231f8758f74ca2e982775d223d343f3"></span>تحديث (PATCH مع رجوع تلقائي إلى PUT عند 404/405)
updated = await Hosteday.client.update("notes", id=created["id"], body={"data": "Updated note"})
# <span id="heading-accb54319f57f3fb9f273e8741999740"></span>حذف سجل
result = await Hosteday.client.delete("notes", id=updated["id"])
assert result["deleted"] is True
4. المصادقة ومخازن الجلسات التفاعلية (HostedayAuth)
تدعم الحزمة 10 عمليات مصادقة كاملة مع 3 مخازن جلسات قابلة للتبديل (MemoryHostedayAuthStorage، JsonFileHostedayAuthStorage بصلاحية 0600، وKeyringHostedayAuthStorage لخزنة نظام التشغيل):
from pathlib import Path
from hosteday import Hosteday
cred = await Hosteday.auth.sign_in_with_email_and_password(
email="user@example.com",
password="StrongPassword!123",
)
print("مرحباً:", cred.user.display_name)
await Hosteday.auth.update_profile(name="Mustafa")
await Hosteday.auth.update_avatar(data=Path("avatar.png").read_bytes(), extension="png")
await Hosteday.auth.send_email_verification()
await Hosteday.auth.reload()
# <span id="heading-97e437259def6684cfb2b88592920f7b"></span>مراقبة حالة المستخدم عبر Async Stream تفاعلي
async def watch_user() -> None:
async for user in Hosteday.auth.user_changes():
print("User changed:", user.display_name if user else "Signed out")
await Hosteday.auth.sign_out()
5. البث اللحظي WebSockets (HostedayRealtimeClient)
يدعم محرك البث بروتوكول Pusher v7 / Laravel Reverb مع إعادة الاتصال التلقائي (Exponential Backoff) والقنوات الأربع: العامة (PUBLIC)، الخاصة (PRIVATE)، التواجد (PRESENCE)، والمشفّرة (PRIVATE_ENCRYPTED عبر PyNaCl):
from hosteday import Hosteday
# <span id="heading-6131fd8fd86cdbb35288b1c57a94fada"></span>1. استقبال الإشعارات الفورية على سطح المكتب / CLI عبر البث المزدوج (Hybrid Dual-Dispatch)
await Hosteday.realtime.listen_public(
channel="announcements",
event="NotificationReceived",
on_event=lambda e: print("إعلان جديد:", e.payload),
)
# <span id="heading-4f1f8e967cfc2bdf797c620289d5c1a2"></span>2. الاستماع لإشعارات المستخدم الخاصة (private-user.{id})
await Hosteday.realtime.listen_private(
channel="user.42",
event="NotificationReceived",
on_event=lambda e: print("إشعار خاص:", e.payload),
)
# <span id="heading-2b6ff0be3050b2a678b988f874b05876"></span>3. غرفة تواجد (Presence) مع متابعة الأعضاء والنشر عبر REST API
room = await Hosteday.realtime.subscribe_presence("auction.1")
await Hosteday.realtime.listen_presence_members_list(
channel="auction.1",
on_event=lambda _e: print("المتصلون الآن:", room.members_count),
)
await Hosteday.realtime.listen_presence(
channel="auction.1",
event="AuctionBidPlaced",
on_event=lambda e: print("مزايدة جديدة:", e.payload),
)
await Hosteday.client.publish_presence_event(
channel="auction.1",
event="AuctionBidPlaced",
payload={"amount": 1500.0, "bidder_name": "Mustafa"},
)
6. الإشعارات الفورية والمهام الخلفية والاتصال (Notifications, Jobs, Connectivity)
from hosteday import Hosteday
# <span id="heading-b3909bd5121bb803adceaee53c573c8b"></span>الإشعارات الفورية (FCM + بث Reverb مزدوج تلقائي)
await Hosteday.notifications.sync_device_token(
token="<fcm_token>",
device_type="web",
device_id="browser-01",
device_name="Chrome Linux",
)
res = await Hosteday.notifications.send_notification(
title="تنبيه مهم",
body="تبقى 5 دقائق على انتهاء المزاد!",
recipient_user_id="42",
)
# <span id="heading-fd0864737a30db414d1917e7695ab828"></span>المهام الخلفية (Background Jobs)
job = await Hosteday.jobs.dispatch(
name="ExportOrdersCsv",
payload={"month": "2026-11"},
delay_seconds=10,
)
status = await Hosteday.jobs.get_status(job.job_id)
# <span id="heading-9ae6d40018aec0aaea6bb8a81b9725be"></span>فحص الاتصال ومزايا الباقة
is_online = await Hosteday.connectivity.check_now()
print("Online:", is_online, "| Realtime:", Hosteday.features.is_realtime_enabled)
7. محرك الويب المدمج (HostedayWebApp) ومعالجة الأخطاء (HostedayException)
يخفي HostedayWebApp تفاصيل FastAPI و Uvicorn تماماً ويوفّر نقاط BFF جاهزة للمصادقة والبث والإشعارات وخدمة SPA، مع تسجيل CRUD كامل بسطر واحد:
from pathlib import Path
from hosteday import Hosteday, HostedayException, HostedayWebError
web = Hosteday.create_web_app(
title="HosteDay Notes App",
config_path=Path("hosteday_config.json"),
session_path=Path(".hosteday_web_session.json"),
static_dir=Path("static"),
)
# <span id="heading-6c4283f7d5597a945e86814ea638af87"></span>تسجيل GET/POST/PUT/DELETE على /bff/notes بسطر واحد
web.crud("notes", owner_field="user_id")
# <span id="heading-27742cfd2422fe96ba874c2bc12fa55b"></span>أو مسار مخصص يضاف تلقائياً تحت /bff/summary
@web.get("summary")
async def get_summary() -> dict[str, int]:
try:
page = await Hosteday.client.index("notes", with_auth=True)
return {"count": len(page.items)}
except HostedayException as exc:
raise HostedayWebError(exc.status_code or 400, exc.display_message) from exc
app = web.asgi_app
if __name__ == "__main__":
web.serve(host="127.0.0.1", port=8000)