2222 THE SOFTWARE.
2323"""
2424
25+ import http .client as http_client
2526import logging
2627import os
2728import sys
2829import time
30+ import uuid
2931from json .decoder import JSONDecodeError
32+
3033import requests
31- import uuid
32- import http .client as http_client
3334import urllib3
34-
3535from pypac import PACSession
3636from pypac .parser import PACFile
3737from urllib3 .exceptions import InsecureRequestWarning
3838
39- from .scanossbase import ScanossBase
4039from . import __version__
41-
40+ from .constants import DEFAULT_TIMEOUT , MIN_TIMEOUT
41+ from .scanossbase import ScanossBase
4242
4343DEFAULT_URL = 'https://api.osskb.org/scan/direct' # default free service URL
4444DEFAULT_URL2 = 'https://api.scanoss.com/scan/direct' # default premium service URL
@@ -52,7 +52,7 @@ class ScanossApi(ScanossBase):
5252 Currently support posting scan requests to the SCANOSS streaming API
5353 """
5454
55- def __init__ ( # noqa: PLR0913, PLR0915
55+ def __init__ ( # noqa: PLR0912, PLR0913, PLR0915
5656 self ,
5757 scan_format : str = None ,
5858 flags : str = None ,
@@ -61,7 +61,7 @@ def __init__( # noqa: PLR0913, PLR0915
6161 debug : bool = False ,
6262 trace : bool = False ,
6363 quiet : bool = False ,
64- timeout : int = 180 ,
64+ timeout : int = DEFAULT_TIMEOUT ,
6565 ver_details : str = None ,
6666 ignore_cert_errors : bool = False ,
6767 proxy : str = None ,
@@ -87,30 +87,28 @@ def __init__( # noqa: PLR0913, PLR0915
8787 HTTPS_PROXY='http://<ip>:<port>'
8888 """
8989 super ().__init__ (debug , trace , quiet )
90- self .url = url
91- self .api_key = api_key
9290 self .sbom = None
9391 self .scan_format = scan_format if scan_format else 'plain'
9492 self .flags = flags
95- self .timeout = timeout if timeout > 5 else 180
93+ self .timeout = timeout if timeout > MIN_TIMEOUT else DEFAULT_TIMEOUT
9694 self .retry_limit = retry if retry >= 0 else 5
9795 self .ignore_cert_errors = ignore_cert_errors
9896 self .req_headers = req_headers if req_headers else {}
9997 self .headers = {}
100-
98+ # Set the correct URL/API key combination
99+ self .url = url if url else SCANOSS_SCAN_URL
100+ self .api_key = api_key if api_key else SCANOSS_API_KEY
101+ if self .api_key and not url and not os .environ .get ('SCANOSS_SCAN_URL' ):
102+ self .url = DEFAULT_URL2 # API key specific and no alternative URL, so use the default premium
101103 if ver_details :
102104 self .headers ['x-scanoss-client' ] = ver_details
103105 if self .api_key :
104106 self .headers ['X-Session' ] = self .api_key
105107 self .headers ['x-api-key' ] = self .api_key
106- self .headers ['User-Agent' ] = f'scanoss-py/{ __version__ } '
107- self .headers ['user-agent' ] = f'scanoss-py/{ __version__ } '
108- self .load_generic_headers ()
109-
110- self .url = url if url else SCANOSS_SCAN_URL
111- self .api_key = api_key if api_key else SCANOSS_API_KEY
112- if self .api_key and not url and not os .environ .get ('SCANOSS_SCAN_URL' ):
113- self .url = DEFAULT_URL2 # API key specific and no alternative URL, so use the default premium
108+ user_agent = f'scanoss-py/{ __version__ } '
109+ self .headers ['User-Agent' ] = user_agent
110+ self .headers ['user-agent' ] = user_agent
111+ self .load_generic_headers (url )
114112
115113 if self .trace :
116114 logging .basicConfig (stream = sys .stderr , level = logging .DEBUG )
@@ -133,7 +131,7 @@ def __init__( # noqa: PLR0913, PLR0915
133131 if self .proxies :
134132 self .session .proxies = self .proxies
135133
136- def scan (self , wfp : str , context : str = None , scan_id : int = None ):
134+ def scan (self , wfp : str , context : str = None , scan_id : int = None ): # noqa: PLR0912, PLR0915
137135 """
138136 Scan the specified WFP and return the JSON object
139137 :param wfp: WFP to scan
@@ -192,7 +190,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
192190 else :
193191 self .print_stderr (f'Warning: No response received from { self .url } . Retrying...' )
194192 time .sleep (5 )
195- elif r .status_code == 503 : # Service limits have most likely been reached
193+ elif r .status_code == requests . codes . service_unavailable : # Service limits most likely reached
196194 self .print_stderr (
197195 f'ERROR: SCANOSS API rejected the scan request ({ request_id } ) due to '
198196 f'service limits being exceeded'
@@ -202,7 +200,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
202200 f'ERROR: { r .status_code } - The SCANOSS API request ({ request_id } ) rejected '
203201 f'for { self .url } due to service limits being exceeded.'
204202 )
205- elif r .status_code >= 400 :
203+ elif r .status_code >= requests . codes . bad_request :
206204 if retry > self .retry_limit : # No response retry_limit or more times, fail
207205 self .save_bad_req_wfp (scan_files , request_id , scan_id )
208206 raise Exception (
@@ -269,7 +267,7 @@ def set_sbom(self, sbom):
269267 self .sbom = sbom
270268 return self
271269
272- def load_generic_headers (self ):
270+ def load_generic_headers (self , url ):
273271 """
274272 Adds custom headers from req_headers to the headers collection.
275273
@@ -279,7 +277,7 @@ def load_generic_headers(self):
279277 if self .req_headers : # Load generic headers
280278 for key , value in self .req_headers .items ():
281279 if key == 'x-api-key' : # Set premium URL if x-api-key header is set
282- if not self . url and not os .environ .get ('SCANOSS_SCAN_URL' ):
280+ if not url and not os .environ .get ('SCANOSS_SCAN_URL' ):
283281 self .url = DEFAULT_URL2 # API key specific and no alternative URL, so use the default premium
284282 self .api_key = value
285283 self .headers [key ] = value
0 commit comments