66import requests
77from datetime import date , timedelta
88
9- from .obj import Agents , User , TokenScope , SearchTypes
10- from .folders import Folders
11- from .uploads import Uploads
12- from .jobs import Jobs
13- from .report import Report
14- from .exceptions import AuthenticationError , FossologyApiError
9+ from typing import List
10+ from fossology .obj import Agents , User , File , TokenScope , SearchTypes , get_options
11+ from fossology .folders import Folders
12+ from fossology .uploads import Uploads
13+ from fossology .jobs import Jobs
14+ from fossology .report import Report
15+ from fossology .exceptions import (
16+ AuthenticationError ,
17+ AuthorizationError ,
18+ FossologyApiError ,
19+ )
1520
1621logger = logging .getLogger (__name__ )
1722logger .setLevel (logging .DEBUG )
@@ -36,12 +41,12 @@ def fossology_token(
3641 :param password: the password of the user
3742 :param name: the name of the token
3843 :param scope: the scope of the token (default: READ)
39- :param expire: the expire date of the token (default max. 30 days)
44+ :param expire: the expire date of the token (default: max. 30 days)
4045 :type url: string
4146 :type username: string
4247 :type password: string
4348 :type name: string
44- :type scope: TokenScope (default TokenScope.READ)
49+ :type scope: TokenScope (default: TokenScope.READ)
4550 :type expire: string, e.g. 2019-12-25
4651 :return: the new token
4752 :rtype: string
@@ -208,7 +213,7 @@ def delete_user(self, user):
208213 :raises FossologyApiError: if the REST call failed
209214 """
210215 response = self .session .delete (f"{ self .api } /users/{ user .id } " )
211- print ( response . json ())
216+
212217 if response .status_code == 202 :
213218 return
214219 else :
@@ -224,6 +229,7 @@ def search(
224229 filesizemax = None ,
225230 license = None ,
226231 copyright = None ,
232+ group = None ,
227233 ):
228234 """Search for a specific file
229235
@@ -236,16 +242,19 @@ def search(
236242 :param filesizemax: Max filesize in bytes
237243 :param license: License search filter
238244 :param copyright: Copyright search filter
245+ :param group: the group name to choose while performing search (default: None)
239246 :type searchType: SearchType Enum
240247 :type filename: string
241248 :type tag: string
242249 :type filesizemin: int
243250 :type filesizemax: int
244251 :type license: string
245252 :type copyright: string
253+ :type group: string
246254 :return: list of items corresponding to the search criteria
247255 :rtype: JSON
248256 :raises FossologyApiError: if the REST call failed
257+ :raises AuthorizationError: if the user can't access the group
249258 """
250259 headers = {"searchType" : searchType .value }
251260 if filename :
@@ -260,10 +269,61 @@ def search(
260269 headers ["license" ] = license
261270 if copyright :
262271 headers ["copyright" ] = copyright
272+ if group :
273+ headers ["groupName" ] = group
263274
264275 response = self .session .get (f"{ self .api } /search" , headers = headers )
276+
265277 if response .status_code == 200 :
266278 return response .json ()
279+
280+ elif response .status_code == 403 :
281+ description = f"Searching { get_options (group )} not authorized"
282+ raise AuthorizationError (description , response )
283+
267284 else :
268285 description = "Unable to get a result with the given search criteria"
269286 raise FossologyApiError (description , response )
287+
288+ def filesearch (
289+ self , filelist : List = [], group : str = None ,
290+ ):
291+ """Search for files from hash sum
292+
293+ API Endpoint: POST /filesearch
294+
295+ The response does not generate Python objects yet, the plain JSON data is simply returned.
296+
297+ :param filelist: the list of files (or containers) to search for (default: [])
298+ :param group: the group name to choose while performing search (default: None)
299+ :type filelist: list
300+ :return: list of items corresponding to the search criteria
301+ :type group: string
302+ :rtype: JSON
303+ :raises FossologyApiError: if the REST call failed
304+ :raises AuthorizationError: if the user can't access the group
305+ """
306+ headers = {}
307+ if group :
308+ headers ["groupName" ] = group
309+
310+ response = self .session .post (
311+ f"{ self .api } /filesearch" , headers = headers , json = filelist
312+ )
313+
314+ if response .status_code == 200 :
315+ all_files = []
316+ for hash_file in response .json ():
317+ if hash_file .get ("findings" ):
318+ all_files .append (File .from_json (hash_file ))
319+ else :
320+ return "Unable to get a result with the given filesearch criteria"
321+ return all_files
322+
323+ elif response .status_code == 403 :
324+ description = f"Searching { get_options (group )} not authorized"
325+ raise AuthorizationError (description , response )
326+
327+ else :
328+ description = "Unable to get a result with the given filesearch criteria"
329+ raise FossologyApiError (description , response )
0 commit comments