-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
41 lines (37 loc) · 1.31 KB
/
__init__.py
File metadata and controls
41 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import json
import urllib.parse
from socketdev.log import log
from ..core.dedupe import Dedupe
class Purl:
def __init__(self, api):
self.api = api
def post(self, license: str = "false", components: list = None, **kwargs) -> list:
path = "purl?"
if components is None:
components = []
purls = {"components": components}
purls = json.dumps(purls)
query_args = {
"license": license,
}
if kwargs:
query_args.update(kwargs)
params = urllib.parse.urlencode(query_args)
path += params
response = self.api.do_request(path=path, payload=purls, method="POST")
if response.status_code == 200:
purl = []
result = response.text
result = result.strip('"').strip()
for line in result.split("\n"):
if line and line != '"':
try:
item = json.loads(line)
purl.append(item)
except json.JSONDecodeError:
continue
purl_deduped = Dedupe.dedupe(purl, batched=True)
return purl_deduped
log.error(f"Error posting {components} to the Purl API: {response.status_code}")
log.error(response.text)
return []