Skip to content

Commit 058db3c

Browse files
committed
Refactor data construction in API class to use json.dumps for better readability and security
1 parent 4205e67 commit 058db3c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

pushbullet/pushbullet.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
from requests.structures import CaseInsensitiveDict
3+
import json
34

45

56
class API():
@@ -18,7 +19,11 @@ def send_note(self, title, body):
1819
headers = CaseInsensitiveDict()
1920
headers["Access-Token"] = self.token
2021
headers["Content-Type"] = "application/json"
21-
data = '{"body":"' + body + '","title":"' + title + '","type":"note"}'
22+
data = json.dumps({
23+
"body": body,
24+
"title": title,
25+
"type": "note"
26+
})
2227
url = self.url + "v2/pushes"
2328
resp = requests.post(url, headers=headers, data=data)
2429
return resp
@@ -28,7 +33,12 @@ def send_link(self, title, body, url_to_send):
2833
headers = CaseInsensitiveDict()
2934
headers["Access-Token"] = self.token
3035
headers["Content-Type"] = "application/json"
31-
data = '{"body":"' + body + '","title":"' + title + '","type":"link", "url":"' + url_to_send + '"}'
36+
data = json.dumps({
37+
"body": body,
38+
"title": title,
39+
"type": "link",
40+
"url": url_to_send
41+
})
3242
url = self.url + "v2/pushes"
3343
resp = requests.post(url, headers=headers, data=data)
3444
return resp
@@ -38,7 +48,14 @@ def send_file(self, title, body, file_name, file_type, file_url):
3848
headers = CaseInsensitiveDict()
3949
headers["Access-Token"] = self.token
4050
headers["Content-Type"] = "application/json"
41-
data = '{"body":"' + body + '","title":"' + title + '","file_name":"' + file_name + '","file_type":"' + file_type + '","file_url":"' + file_url + '","type":"file"}'
51+
data = json.dumps({
52+
"body": body,
53+
"title": title,
54+
"file_name": file_name,
55+
"file_type": file_type,
56+
"file_url": file_url,
57+
"type": "file"
58+
})
4259
url = self.url + "v2/pushes"
4360
resp = requests.post(url, headers=headers, data=data)
4461
return resp

0 commit comments

Comments
 (0)