|
| 1 | +from copy import deepcopy |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import List, Literal, Optional, Union |
| 4 | + |
| 5 | +from pydantic import BaseModel, Field, field_validator, validate_call |
| 6 | +from vonage_http_client.http_client import HttpClient |
| 7 | +from vonage_sms.errors import SmsError |
| 8 | + |
| 9 | + |
| 10 | +class SmsMessage(BaseModel): |
| 11 | + to: str |
| 12 | + from_: str = Field(..., alias="from") |
| 13 | + text: str |
| 14 | + type: Optional[str] = None |
| 15 | + sig: Optional[str] = Field(None, min_length=16, max_length=60) |
| 16 | + status_report_req: Optional[int] = Field( |
| 17 | + None, |
| 18 | + alias="status-report-req", |
| 19 | + description="Set to 1 to receive a Delivery Receipt", |
| 20 | + ) |
| 21 | + client_ref: Optional[str] = Field( |
| 22 | + None, alias="client-ref", description="Your own reference. Up to 40 characters." |
| 23 | + ) |
| 24 | + network_code: Optional[str] = Field( |
| 25 | + None, |
| 26 | + alias="network-code", |
| 27 | + description="A 4-5 digit number that represents the mobile carrier network code", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class SmsResponse: |
| 33 | + id: str |
| 34 | + |
| 35 | + |
| 36 | +class Sms: |
| 37 | + """Calls Vonage's SMS API.""" |
| 38 | + |
| 39 | + def __init__(self, http_client: HttpClient) -> None: |
| 40 | + self._http_client = deepcopy(http_client) |
| 41 | + self._auth_type = 'basic' |
| 42 | + |
| 43 | + @validate_call |
| 44 | + def send(self, message: SmsMessage) -> SmsResponse: |
| 45 | + """Send an SMS message.""" |
| 46 | + response = self._http_client.post( |
| 47 | + self._http_client.api_host, |
| 48 | + '/v2/ni', |
| 49 | + message.model_dump(), |
| 50 | + self._auth_type, |
| 51 | + ) |
0 commit comments