Skip to content

Commit 1610f83

Browse files
Fix upload_attachment (#270)
## 📝 Description closes #17 Fix `upload_attachment` method of `SupportTicket` class. ## ✔️ How to Test Manually test it
1 parent cfdc8a1 commit 1610f83

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

linode_api4/objects/support.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from pathlib import Path
2+
from typing import Union
3+
14
import requests
25

36
from linode_api4.errors import ApiError, UnexpectedResponseError
@@ -139,7 +142,7 @@ def post_reply(self, description):
139142
r = TicketReply(self._client, result["id"], self.id, result)
140143
return r
141144

142-
def upload_attachment(self, attachment):
145+
def upload_attachment(self, attachment: Union[Path, str]):
143146
"""
144147
Uploads an attachment to an existing Support Ticket.
145148
@@ -151,27 +154,25 @@ def upload_attachment(self, attachment):
151154
:returns: Whether the upload operation was successful.
152155
:rtype: bool
153156
"""
157+
if not isinstance(attachment, Path):
158+
attachment = Path(attachment)
154159

155-
content = None
156-
with open(attachment) as f:
157-
content = f.read()
158-
159-
if not content:
160-
raise ValueError("Nothing to upload!")
160+
if not attachment.exists():
161+
raise ValueError("File not exist, nothing to upload.")
161162

162163
headers = {
163-
"Authorization": "token {}".format(self._client.token),
164-
"Content-type": "multipart/form-data",
164+
"Authorization": "Bearer {}".format(self._client.token),
165165
}
166166

167-
result = requests.post(
168-
"{}{}/attachments".format(
169-
self._client.base_url,
170-
SupportTicket.api_endpoint.format(id=self.id),
171-
),
172-
headers=headers,
173-
files=content,
174-
)
167+
with open(attachment, "rb") as f:
168+
result = requests.post(
169+
"{}{}/attachments".format(
170+
self._client.base_url,
171+
SupportTicket.api_endpoint.format(id=self.id),
172+
),
173+
headers=headers,
174+
files={"file": f},
175+
)
175176

176177
if not result.status_code == 200:
177178
errors = []

0 commit comments

Comments
 (0)