Skip to content

Commit dc0e0b7

Browse files
committed
fix(version): use v1 by default until v2 will be released
1 parent 5fe5645 commit dc0e0b7

File tree

10 files changed

+175
-64
lines changed

10 files changed

+175
-64
lines changed

Diff for: README.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ A simple wrapper for the Fossology REST API.
2626

2727
See `the OpenAPI specification <https://raw.githubusercontent.com/fossology/fossology/master/src/www/ui/api/documentation/openapi.yaml>`_ used to implement this library.
2828

29-
Current release is compatible with **Fossology version 4.4.0-rc2** - API version 2.0.0 (not all endpoints are supported)
29+
Current release is compatible with **Fossology version 4.4.0** - API version 1.6.1 (not all endpoints are supported)
3030

3131
`See release notes <https://github.com/fossology/fossology-python/releases>`_ for all details.
3232

3333
If you miss an API Endpoint, please open a new issue or contribute a pull request.
3434

35-
API v1 is supported too, it needs to be specified explicitly.
35+
API v2 is partially supported too, however the specification is not stable yet and not all endpoints are supported.
3636

3737
Documentation
3838
=============
@@ -68,14 +68,14 @@ Using the API
6868
FOSSOLOGY_PASSWORD = "fossy"
6969
TOKEN_NAME = "fossy_token"
7070
71-
# By default version v2 of the token generation API will be used
71+
# By default version v1 of the token generation API will be used
7272
token = fossology_token(
7373
FOSSOLOGY_SERVER,
7474
FOSSOLOGY_USER,
7575
FOSSOLOGY_PASSWORD,
7676
TOKEN_NAME,
7777
TokenScope.WRITE
78-
version="v2"
78+
version="v1"
7979
)
8080
8181
- Start using the API:
@@ -84,8 +84,8 @@ Using the API
8484
8585
from fossology import Fossology
8686
87-
# By default version v2 of the API will be used
88-
foss = Fossology(FOSSOLOGY_SERVER, token, FOSSOLOGY_USER, version="v2")
87+
# By default version v1 of the API will be used
88+
foss = Fossology(FOSSOLOGY_SERVER, token, FOSSOLOGY_USER, version="v1")
8989
print(f"Logged in as user {foss.user.name}")
9090
9191

Diff for: docs-source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
copyright = "2021, Siemens AG"
2323

2424
# The full version, including major/minor/patch tags
25-
release = "3.2.0"
25+
release = "3.2.1"
2626

2727

2828
# -- General configuration ---------------------------------------------------

Diff for: fossology/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def fossology_token(
3131
token_name,
3232
token_scope=TokenScope.READ,
3333
token_expire=None,
34-
version="v2",
34+
version="v1",
3535
):
3636
"""Generate an API token using username/password
3737
@@ -41,7 +41,7 @@ def fossology_token(
4141
4242
>>> from fossology import fossology_token # doctest: +SKIP
4343
>>> from fossology.obj import TokenScope # doctest: +SKIP
44-
>>> token = fossology_token("https://fossology.example.com/repo", "Me", "MyPassword", "MyToken", version="v2") # doctest: +SKIP
44+
>>> token = fossology_token("https://fossology.example.com/repo", "Me", "MyPassword", "MyToken", version="v1") # doctest: +SKIP
4545
4646
4747
:param url: the URL of the Fossology server
@@ -50,7 +50,7 @@ def fossology_token(
5050
:param name: the name of the token
5151
:param scope: the scope of the token (default: TokenScope.READ)
5252
:param expire: the expire date of the token, e.g. 2019-12-25 (default: max. 30 days)
53-
:param version: the version of the API to use (default: "v2")
53+
:param version: the version of the API to use (default: "v1")
5454
:type url: string
5555
:type username: string
5656
:type password: string
@@ -118,15 +118,15 @@ class Fossology(
118118
119119
:param url: URL of the Fossology instance
120120
:param token: The API token generated using the Fossology UI
121-
:param version: the version of the API to use (default: "v2")
121+
:param version: the version of the API to use (default: "v1")
122122
:type url: str
123123
:type token: str
124124
:type version: str
125125
:raises FossologyApiError: if a REST call failed
126126
:raises AuthenticationError: if the user couldn't be authenticated
127127
"""
128128

129-
def __init__(self, url, token, version="v2"):
129+
def __init__(self, url, token, version="v1"):
130130
self.host = url
131131
self.token = token
132132
self.users = list()

Diff for: fossology/uploads.py

+35-21
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def upload_file(
156156
157157
>>> from fossology import Fossology
158158
>>> from fossology.enums import AccessLevel
159-
>>> foss = Fossology(FOSS_URL, FOSS_TOKEN, username) # doctest: +SKIP
159+
>>> foss = Fossology(FOSS_URL, FOSS_TOKEN) # doctest: +SKIP
160160
>>> my_upload = foss.upload_file(
161161
... foss.rootFolder,
162162
... file="my-package.zip",
@@ -237,39 +237,53 @@ def upload_file(
237237
:raises FossologyApiError: if the REST call failed
238238
:raises AuthorizationError: if the REST call is not authorized
239239
"""
240-
headers = {"folderId": str(folder.id)}
241-
if description:
242-
headers["uploadDescription"] = description
243-
if access_level:
244-
headers["public"] = access_level.value
245-
if apply_global:
246-
headers["applyGlobal"] = "true"
247-
if ignore_scm:
248-
headers["ignoreScm"] = "true"
249-
if group:
240+
data = {
241+
"folderId": str(folder.id),
242+
"uploadDescription": description,
243+
"public": access_level.value
244+
if access_level
245+
else AccessLevel.PROTECTED.value,
246+
"applyGlobal": apply_global,
247+
"ignoreScm": ignore_scm,
248+
"uploadType": "file",
249+
}
250+
251+
headers = {}
252+
if "v1" in self.api:
253+
headers = {
254+
k: str(v).lower() if isinstance(v, bool) else v for k, v in data.items()
255+
} # Needed for API v1.x
250256
headers["groupName"] = group
257+
endpoint = f"{self.api}/uploads"
258+
else:
259+
if group:
260+
endpoint = f"{self.api}/uploads?groupName={group}"
261+
else:
262+
endpoint = f"{self.api}/uploads"
251263

252264
if file:
253-
headers["uploadType"] = "file"
265+
data["uploadType"] = headers["uploadType"] = "file"
254266
with open(file, "rb") as fp:
255267
files = {"fileInput": fp}
256268
response = self.session.post(
257-
f"{self.api}/uploads", files=files, headers=headers
269+
endpoint, files=files, headers=headers, data=data
258270
)
259271
elif vcs or url or server:
260-
data = dict
261272
if vcs:
262-
headers["uploadType"] = "vcs"
263-
data = {"location": vcs} # type: ignore
273+
data["location"] = vcs
274+
print(data)
275+
data["uploadType"] = headers["uploadType"] = "vcs"
264276
elif url:
265-
headers["uploadType"] = "url"
266-
data = {"location": url} # type: ignore
277+
data["location"] = url
278+
data["uploadType"] = headers["uploadType"] = "url"
267279
elif server:
268-
headers["uploadType"] = "server"
269-
data = {"location": server} # type: ignore
280+
data["location"] = server
281+
data["uploadType"] = headers["uploadType"] = "server"
270282
headers["Content-Type"] = "application/json"
271283
response = self.session.post(
272-
f"{self.api}/uploads", data=json.dumps(data), headers=headers
284+
endpoint,
285+
data=json.dumps(data),
286+
headers=headers,
273287
)
274288
else:
275289
logger.info(

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fossology"
3-
version = "3.2.0"
3+
version = "3.2.1"
44
description = "A library to automate Fossology from Python scripts"
55
authors = ["Marion Deveaud <[email protected]>"]
66
license = "MIT License"

Diff for: tests/conftest.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ def foss(foss_server: str, foss_token: str, foss_agents: Agents) -> fossology.Fo
175175

176176

177177
@pytest.fixture(scope="session")
178-
def foss_v1(
178+
def foss_v2(
179179
foss_server: str, foss_token: str, foss_agents: Agents
180180
) -> fossology.Fossology:
181181
try:
182-
foss = fossology.Fossology(foss_server, foss_token, version="v1")
182+
foss = fossology.Fossology(foss_server, foss_token, version="v2")
183183
except (FossologyApiError, AuthenticationError) as error:
184184
exit(error.message)
185185

@@ -231,20 +231,20 @@ def upload(
231231

232232

233233
@pytest.fixture(scope="function")
234-
def upload_v1(
235-
foss_v1: fossology.Fossology,
234+
def upload_v2(
235+
foss_v2: fossology.Fossology,
236236
test_file_path: str,
237237
) -> Generator:
238-
upload = foss_v1.upload_file(
239-
foss_v1.rootFolder,
238+
upload = foss_v2.upload_file(
239+
foss_v2.rootFolder,
240240
file=test_file_path,
241241
description="Test upload via fossology-python lib",
242242
access_level=AccessLevel.PUBLIC,
243243
wait_time=5,
244244
)
245-
jobs_lookup(foss_v1, upload)
245+
jobs_lookup(foss_v2, upload)
246246
yield upload
247-
foss_v1.delete_upload(upload)
247+
foss_v2.delete_upload(upload)
248248
time.sleep(5)
249249

250250

Diff for: tests/test_init.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ def test_get_info(foss: Fossology):
1515

1616

1717
@responses.activate
18-
def test_info_does_not_return_200(foss_server: str, foss: Fossology):
18+
def test_info_v2_does_not_return_200(foss_server: str, foss_v2: Fossology):
1919
responses.add(
2020
responses.GET,
2121
f"{foss_server}/api/v2/info",
2222
status=400,
2323
)
24+
with pytest.raises(FossologyApiError) as excinfo:
25+
foss_v2.get_info()
26+
assert "Error while getting API info" in str(excinfo.value)
27+
28+
29+
@responses.activate
30+
def test_info_does_not_return_200(foss_server: str, foss: Fossology):
31+
responses.add(
32+
responses.GET,
33+
f"{foss_server}/api/v1/info",
34+
status=400,
35+
)
2436
with pytest.raises(FossologyApiError) as excinfo:
2537
foss.get_info()
2638
assert "Error while getting API info" in str(excinfo.value)
@@ -36,9 +48,21 @@ def test_get_health(foss: Fossology):
3648
def test_health_does_not_return_200(foss_server: str, foss: Fossology):
3749
responses.add(
3850
responses.GET,
39-
f"{foss_server}/api/v2/health",
51+
f"{foss_server}/api/v1/health",
4052
status=503,
4153
)
4254
with pytest.raises(FossologyApiError) as excinfo:
4355
foss.get_health()
4456
assert "Error while getting health info" in str(excinfo.value)
57+
58+
59+
@responses.activate
60+
def test_health_v2_does_not_return_200(foss_server: str, foss_v2: Fossology):
61+
responses.add(
62+
responses.GET,
63+
f"{foss_server}/api/v2/health",
64+
status=503,
65+
)
66+
with pytest.raises(FossologyApiError) as excinfo:
67+
foss_v2.get_health()
68+
assert "Error while getting health info" in str(excinfo.value)

Diff for: tests/test_items.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def test_item_info(foss: Fossology, upload_with_jobs: Upload):
1616
assert info.meta_info
1717

1818

19-
def test_item_info_v1(foss_v1: Fossology, upload_with_jobs: Upload):
20-
files, _ = foss_v1.search(license="BSD")
21-
info: FileInfo = foss_v1.item_info(upload_with_jobs, files[0].uploadTreeId)
19+
def test_item_info_v2(foss_v2: Fossology, upload_with_jobs: Upload):
20+
files, _ = foss_v2.search(license="BSD")
21+
info: FileInfo = foss_v2.item_info(upload_with_jobs, files[0].uploadTreeId)
2222
assert info.meta_info
2323

2424

Diff for: tests/test_upload_from.py

+60
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ def test_upload_from_vcs(foss: Fossology):
4343
delete_upload(foss, vcs_upload)
4444

4545

46+
def test_upload_from_vcs_v2(foss_v2: Fossology):
47+
vcs = {
48+
"vcsType": "git",
49+
"vcsUrl": "https://github.com/fossology/fossology-python",
50+
"vcsName": "fossology-python-github-master",
51+
"vcsUsername": "",
52+
"vcsPassword": "",
53+
}
54+
vcs_upload = foss_v2.upload_file(
55+
foss_v2.rootFolder,
56+
vcs=vcs,
57+
description="Test upload from github repository via python lib",
58+
access_level=AccessLevel.PUBLIC,
59+
ignore_scm=False,
60+
wait_time=5,
61+
)
62+
assert vcs_upload.uploadname == vcs["vcsName"]
63+
# Cleanup
64+
delete_upload(foss_v2, vcs_upload)
65+
66+
4667
def test_upload_from_url(foss: Fossology):
4768
url = {
4869
"url": "https://github.com/fossology/fossology-python/archive/master.zip",
@@ -63,6 +84,26 @@ def test_upload_from_url(foss: Fossology):
6384
delete_upload(foss, url_upload)
6485

6586

87+
def test_upload_from_url_v2(foss_v2: Fossology):
88+
url = {
89+
"url": "https://github.com/fossology/fossology-python/archive/master.zip",
90+
"name": "fossology-python-master.zip",
91+
"accept": "zip",
92+
"reject": "",
93+
"maxRecursionDepth": "1",
94+
}
95+
url_upload = foss_v2.upload_file(
96+
foss_v2.rootFolder,
97+
url=url,
98+
description="Test upload from url via python lib",
99+
access_level=AccessLevel.PUBLIC,
100+
wait_time=5,
101+
)
102+
assert url_upload.uploadname == url["name"]
103+
# Cleanup
104+
delete_upload(foss_v2, url_upload)
105+
106+
66107
def test_upload_from_server(foss: Fossology):
67108
server = {
68109
"path": "/tmp/base-files-11",
@@ -80,3 +121,22 @@ def test_upload_from_server(foss: Fossology):
80121

81122
# Cleanup
82123
delete_upload(foss, server_upload)
124+
125+
126+
def test_upload_from_server_v2(foss_v2: Fossology):
127+
server = {
128+
"path": "/tmp/base-files-11",
129+
"name": "base-files-11",
130+
}
131+
server_upload = foss_v2.upload_file(
132+
foss_v2.rootFolder,
133+
server=server,
134+
description="Test upload from server via python lib",
135+
access_level=AccessLevel.PUBLIC,
136+
apply_global=True,
137+
wait_time=5,
138+
)
139+
assert server_upload.uploadname == server["name"]
140+
141+
# Cleanup
142+
delete_upload(foss_v2, server_upload)

0 commit comments

Comments
 (0)