1+ import json as jsonlib
2+ import warnings
13from typing import List
24
35from thehive4py .endpoints ._base import EndpointBase
68
79
810class TaskLogEndpoint (EndpointBase ):
9- def create (self , task_id : str , task_log : InputTaskLog ) -> OutputTaskLog :
11+ def create (
12+ self ,
13+ task_id : str ,
14+ task_log : InputTaskLog ,
15+ ) -> OutputTaskLog :
16+ """Create a task log.
17+
18+ Args:
19+ task_id: The id of the task to create the log in.
20+ task_log: The body of the task log.
21+
22+ Returns:
23+ The created task_log.
24+ """
25+ if "attachments" in task_log :
26+ files : List [tuple ] = [
27+ ("attachments" , self ._fileinfo_from_filepath (attachment_path ))
28+ for attachment_path in task_log ["attachments" ]
29+ ]
30+ files .append (("_json" , jsonlib .dumps (task_log )))
31+ kwargs : dict = {"files" : files }
32+ else :
33+ kwargs = {"json" : task_log }
1034 return self ._session .make_request (
11- "POST" , path = f"/api/v1/task/{ task_id } /log" , json = task_log
35+ "POST" , path = f"/api/v1/task/{ task_id } /log" , ** kwargs
1236 )
1337
14- def get (self , task_log_id : str ) -> OutputTaskLog :
15- # TODO: temp implementation until a dedicated get endpoint
16- logs = self ._session .make_request (
17- "POST" ,
18- path = "/api/v1/query" ,
19- json = {"query" : [{"_name" : "getLog" , "idOrName" : task_log_id }]},
20- )
38+ def delete (self , task_log_id : str ) -> None :
39+ """Delete a task log.
2140
22- try :
23- return logs [0 ]
24- except IndexError :
25- raise TheHiveError ("404 - Task Log Not Found" )
41+ Args:
42+ task_log_id: The id of the task log.
2643
27- def delete (self , task_log_id : str ) -> None :
44+ Returns:
45+ N/A
46+ """
2847 return self ._session .make_request ("DELETE" , path = f"/api/v1/log/{ task_log_id } " )
2948
3049 def update (self , task_log_id : str , fields : InputUpdateTaskLog ) -> None :
50+ """Update a task log.
51+
52+ Args:
53+ task_log_id: The id of the task log.
54+ fields: The fields of the task log to update.
55+
56+ Returns:
57+ N/A
58+ """
3159 return self ._session .make_request (
3260 "PATCH" , path = f"/api/v1/log/{ task_log_id } " , json = fields
3361 )
3462
63+ def add_attachment (self , task_log_id : str , attachment_paths : List [str ]) -> None :
64+ """Add attachments to a task log.
65+
66+ Args:
67+ task_log_id: The id of the task log.
68+ attachment_paths: List of paths to the attachments to create.
69+
70+ Returns:
71+ The created task log attachments.
72+ """
73+ files = [
74+ ("attachments" , self ._fileinfo_from_filepath (attachment_path ))
75+ for attachment_path in attachment_paths
76+ ]
77+ return self ._session .make_request (
78+ "POST" , f"/api/v1/log/{ task_log_id } /attachments" , files = files
79+ )
80+
3581 def add_attachments (self , task_log_id : str , attachment_paths : List [str ]) -> None :
82+ """Add attachments to a task log.
83+
84+ !!! warning
85+ Deprecated: use [task_log.add_attachment]
86+ [thehive4py.endpoints.task_log.TaskLogEndpoint.add_attachment]
87+ instead
88+
89+ Args:
90+ task_log_id: The id of the task log.
91+ attachment_paths: List of paths to the attachments to create.
92+
93+ Returns:
94+ The created task log attachments.
95+ """
96+ warnings .warn (
97+ message = ("Deprecated: use the task_log.add_attachment method instead" ),
98+ category = DeprecationWarning ,
99+ stacklevel = 2 ,
100+ )
36101 files = [
37102 ("attachments" , self ._fileinfo_from_filepath (attachment_path ))
38103 for attachment_path in attachment_paths
@@ -42,6 +107,36 @@ def add_attachments(self, task_log_id: str, attachment_paths: List[str]) -> None
42107 )
43108
44109 def delete_attachment (self , task_log_id : str , attachment_id : str ) -> None :
110+ """Delete a task log attachment.
111+
112+ Args:
113+ task_log_id: The id of the task log.
114+ attachment_id: The id of the task log attachment.
115+
116+ Returns:
117+ N/A
118+ """
45119 return self ._session .make_request (
46120 "DELETE" , f"/api/v1/log/{ task_log_id } /attachments/{ attachment_id } "
47121 )
122+
123+ def get (self , task_log_id : str ) -> OutputTaskLog :
124+ """Get a task log by id.
125+
126+ Args:
127+ task_log_id: The id of the task log.
128+
129+ Returns:
130+ The task log specified by the id.
131+ """
132+ # TODO: temp implementation until a dedicated get endpoint [if ever ;)]
133+ logs = self ._session .make_request (
134+ "POST" ,
135+ path = "/api/v1/query" ,
136+ json = {"query" : [{"_name" : "getLog" , "idOrName" : task_log_id }]},
137+ )
138+
139+ try :
140+ return logs [0 ]
141+ except IndexError :
142+ raise TheHiveError ("404 - Task Log Not Found" )
0 commit comments