Rename gateway_url
to gateway_base_url
In order to prepare for using more than the SMS sending endpoint.
This commit is contained in:
parent
19aede533b
commit
5019844761
|
@ -35,7 +35,7 @@ class Gateway:
|
||||||
options: Dict[str, Any] = {}
|
options: Dict[str, Any] = {}
|
||||||
username: str
|
username: str
|
||||||
password: str
|
password: str
|
||||||
gateway_url: str = "https://api.cpsms.dk/v2/send"
|
gateway_base_url: str = "https://api.cpsms.dk"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def default_options(self) -> Dict[str, Any]:
|
def default_options(self) -> Dict[str, Any]:
|
||||||
|
@ -58,7 +58,7 @@ class Gateway:
|
||||||
password: str,
|
password: str,
|
||||||
sender_name: str,
|
sender_name: str,
|
||||||
options: Dict[str, Any] = None,
|
options: Dict[str, Any] = None,
|
||||||
gateway_url: str = None,
|
gateway_base_url: str = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize SMS gateway."""
|
"""Initialize SMS gateway."""
|
||||||
self.username = username
|
self.username = username
|
||||||
|
@ -70,8 +70,8 @@ class Gateway:
|
||||||
if options is not None:
|
if options is not None:
|
||||||
self.options.update(options)
|
self.options.update(options)
|
||||||
|
|
||||||
if gateway_url:
|
if gateway_base_url:
|
||||||
self.gateway_url = gateway_url
|
self.gateway_base_url = gateway_base_url
|
||||||
|
|
||||||
def send(self, to: str = None, message: str = None) -> Tuple[bool, str]:
|
def send(self, to: str = None, message: str = None) -> Tuple[bool, str]:
|
||||||
"""
|
"""
|
||||||
|
@ -94,8 +94,7 @@ class Gateway:
|
||||||
if len(self.options["message"]) > 459:
|
if len(self.options["message"]) > 459:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Message not allowed to be more than 459 characters."
|
"Message not allowed to be more than 459 characters."
|
||||||
"Current message is %i characters."
|
"Current message is %i characters." % len(self.options["message"])
|
||||||
% len(self.options["message"])
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update recipient if specified.
|
# Update recipient if specified.
|
||||||
|
@ -122,10 +121,8 @@ class Gateway:
|
||||||
auth_header = "{}:{}".format(self.username, self.password)
|
auth_header = "{}:{}".format(self.username, self.password)
|
||||||
auth_header = base64.b64encode(auth_header.encode()).decode()
|
auth_header = base64.b64encode(auth_header.encode()).decode()
|
||||||
|
|
||||||
http_request = request.Request(self.gateway_url, data=data)
|
http_request = request.Request(self.gateway_base_url + "/v2/send", data=data)
|
||||||
http_request.add_header(
|
http_request.add_header("Authorization", "Basic {}".format(auth_header))
|
||||||
"Authorization", "Basic {}".format(auth_header)
|
|
||||||
)
|
|
||||||
response = request.urlopen(http_request)
|
response = request.urlopen(http_request)
|
||||||
|
|
||||||
return json.loads(response.read().decode())
|
return json.loads(response.read().decode())
|
||||||
|
|
Loading…
Reference in a new issue