1
+ from pathlib import Path
2
+ from typing import Union
3
+
1
4
import requests
2
5
3
6
from linode_api4 .errors import ApiError , UnexpectedResponseError
@@ -139,7 +142,7 @@ def post_reply(self, description):
139
142
r = TicketReply (self ._client , result ["id" ], self .id , result )
140
143
return r
141
144
142
- def upload_attachment (self , attachment ):
145
+ def upload_attachment (self , attachment : Union [ Path , str ] ):
143
146
"""
144
147
Uploads an attachment to an existing Support Ticket.
145
148
@@ -151,27 +154,25 @@ def upload_attachment(self, attachment):
151
154
:returns: Whether the upload operation was successful.
152
155
:rtype: bool
153
156
"""
157
+ if not isinstance (attachment , Path ):
158
+ attachment = Path (attachment )
154
159
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." )
161
162
162
163
headers = {
163
- "Authorization" : "token {}" .format (self ._client .token ),
164
- "Content-type" : "multipart/form-data" ,
164
+ "Authorization" : "Bearer {}" .format (self ._client .token ),
165
165
}
166
166
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
+ )
175
176
176
177
if not result .status_code == 200 :
177
178
errors = []
0 commit comments