File size: 3,599 Bytes
525d39f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
OpenWA Python SDK

Official client library for the OpenWA WhatsApp API Gateway.

Example usage::

    from openwa import OpenWAClient

    client = OpenWAClient(
        base_url="http://localhost:2785",
        api_key="your-api-key",
    )

    # Send a text message
    result = client.messages.send_text("session-1", {
        "chatId": "628123456789@c.us",
        "text": "Hello from OpenWA Python SDK!",
    })
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class OpenWAClientConfig:
    """Configuration for the OpenWA client."""

    base_url: str
    api_key: str
    timeout: float = 30.0


@dataclass
class MessageResponse:
    """Response from sending a message."""

    message_id: str
    timestamp: int


class OpenWAClient:
    """OpenWA API client.

    This is a scaffold — methods will be auto-generated from the OpenAPI spec.
    """

    def __init__(self, base_url: str, api_key: str, timeout: float = 30.0) -> None:
        self.config = OpenWAClientConfig(
            base_url=base_url.rstrip("/"),
            api_key=api_key,
            timeout=timeout,
        )

    # Placeholder — will be auto-generated from OpenAPI spec
    @property
    def sessions(self) -> "_SessionsResource":
        return _SessionsResource(self)

    @property
    def messages(self) -> "_MessagesResource":
        return _MessagesResource(self)

    def _request(self, method: str, path: str, json: Any = None) -> Any:
        """Internal HTTP request helper. Requires httpx."""
        try:
            import httpx
        except ImportError:
            raise ImportError(
                "httpx is required for the OpenWA SDK. "
                "Install it with: pip install openwa-sdk"
            )

        with httpx.Client(timeout=self.config.timeout) as client:
            response = client.request(
                method,
                f"{self.config.base_url}{path}",
                headers={
                    "Content-Type": "application/json",
                    "X-API-Key": self.config.api_key,
                },
                json=json,
            )
            response.raise_for_status()

            if response.status_code == 204:
                return None

            return response.json()


class _SessionsResource:
    def __init__(self, client: OpenWAClient) -> None:
        self._client = client

    def list(self) -> list[dict]:
        return self._client._request("GET", "/api/sessions")

    def get(self, session_id: str) -> dict:
        return self._client._request("GET", f"/api/sessions/{session_id}")

    def create(self, name: str) -> dict:
        return self._client._request("POST", "/api/sessions", {"name": name})

    def start(self, session_id: str) -> dict:
        return self._client._request("POST", f"/api/sessions/{session_id}/start")

    def stop(self, session_id: str) -> dict:
        return self._client._request("POST", f"/api/sessions/{session_id}/stop")

    def delete(self, session_id: str) -> None:
        self._client._request("DELETE", f"/api/sessions/{session_id}")


class _MessagesResource:
    def __init__(self, client: OpenWAClient) -> None:
        self._client = client

    def send_text(
        self, session_id: str, data: dict[str, str]
    ) -> MessageResponse:
        result = self._client._request(
            "POST", f"/api/sessions/{session_id}/messages/text", data
        )
        return MessageResponse(
            message_id=result["messageId"],
            timestamp=result["timestamp"],
        )