Skip to content

Commit

Permalink
fix(version): use v1 by default until v2 will be released
Browse files Browse the repository at this point in the history
  • Loading branch information
deveaud-m committed Mar 28, 2024
1 parent 5fe5645 commit dc0e0b7
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 64 deletions.
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ A simple wrapper for the Fossology REST API.

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

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

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

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

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

Documentation
=============
Expand Down Expand Up @@ -68,14 +68,14 @@ Using the API
FOSSOLOGY_PASSWORD = "fossy"
TOKEN_NAME = "fossy_token"
# By default version v2 of the token generation API will be used
# By default version v1 of the token generation API will be used
token = fossology_token(
FOSSOLOGY_SERVER,
FOSSOLOGY_USER,
FOSSOLOGY_PASSWORD,
TOKEN_NAME,
TokenScope.WRITE
version="v2"
version="v1"
)
- Start using the API:
Expand All @@ -84,8 +84,8 @@ Using the API
from fossology import Fossology
# By default version v2 of the API will be used
foss = Fossology(FOSSOLOGY_SERVER, token, FOSSOLOGY_USER, version="v2")
# By default version v1 of the API will be used
foss = Fossology(FOSSOLOGY_SERVER, token, FOSSOLOGY_USER, version="v1")
print(f"Logged in as user {foss.user.name}")
Expand Down
2 changes: 1 addition & 1 deletion docs-source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
copyright = "2021, Siemens AG"

# The full version, including major/minor/patch tags
release = "3.2.0"
release = "3.2.1"


# -- General configuration ---------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions fossology/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fossology_token(
token_name,
token_scope=TokenScope.READ,
token_expire=None,
version="v2",
version="v1",
):
"""Generate an API token using username/password
Expand All @@ -41,7 +41,7 @@ def fossology_token(
>>> from fossology import fossology_token # doctest: +SKIP
>>> from fossology.obj import TokenScope # doctest: +SKIP
>>> token = fossology_token("https://fossology.example.com/repo", "Me", "MyPassword", "MyToken", version="v2") # doctest: +SKIP
>>> token = fossology_token("https://fossology.example.com/repo", "Me", "MyPassword", "MyToken", version="v1") # doctest: +SKIP
:param url: the URL of the Fossology server
Expand All @@ -50,7 +50,7 @@ def fossology_token(
:param name: the name of the token
:param scope: the scope of the token (default: TokenScope.READ)
:param expire: the expire date of the token, e.g. 2019-12-25 (default: max. 30 days)
:param version: the version of the API to use (default: "v2")
:param version: the version of the API to use (default: "v1")
:type url: string
:type username: string
:type password: string
Expand Down Expand Up @@ -118,15 +118,15 @@ class Fossology(
:param url: URL of the Fossology instance
:param token: The API token generated using the Fossology UI
:param version: the version of the API to use (default: "v2")
:param version: the version of the API to use (default: "v1")
:type url: str
:type token: str
:type version: str
:raises FossologyApiError: if a REST call failed
:raises AuthenticationError: if the user couldn't be authenticated
"""

def __init__(self, url, token, version="v2"):
def __init__(self, url, token, version="v1"):
self.host = url
self.token = token
self.users = list()
Expand Down
56 changes: 35 additions & 21 deletions fossology/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def upload_file(
>>> from fossology import Fossology
>>> from fossology.enums import AccessLevel
>>> foss = Fossology(FOSS_URL, FOSS_TOKEN, username) # doctest: +SKIP
>>> foss = Fossology(FOSS_URL, FOSS_TOKEN) # doctest: +SKIP
>>> my_upload = foss.upload_file(
... foss.rootFolder,
... file="my-package.zip",
Expand Down Expand Up @@ -237,39 +237,53 @@ def upload_file(
:raises FossologyApiError: if the REST call failed
:raises AuthorizationError: if the REST call is not authorized
"""
headers = {"folderId": str(folder.id)}
if description:
headers["uploadDescription"] = description
if access_level:
headers["public"] = access_level.value
if apply_global:
headers["applyGlobal"] = "true"
if ignore_scm:
headers["ignoreScm"] = "true"
if group:
data = {
"folderId": str(folder.id),
"uploadDescription": description,
"public": access_level.value
if access_level
else AccessLevel.PROTECTED.value,
"applyGlobal": apply_global,
"ignoreScm": ignore_scm,
"uploadType": "file",
}

headers = {}
if "v1" in self.api:
headers = {
k: str(v).lower() if isinstance(v, bool) else v for k, v in data.items()
} # Needed for API v1.x
headers["groupName"] = group
endpoint = f"{self.api}/uploads"
else:
if group:
endpoint = f"{self.api}/uploads?groupName={group}"
else:
endpoint = f"{self.api}/uploads"

if file:
headers["uploadType"] = "file"
data["uploadType"] = headers["uploadType"] = "file"
with open(file, "rb") as fp:
files = {"fileInput": fp}
response = self.session.post(
f"{self.api}/uploads", files=files, headers=headers
endpoint, files=files, headers=headers, data=data
)
elif vcs or url or server:
data = dict
if vcs:
headers["uploadType"] = "vcs"
data = {"location": vcs} # type: ignore
data["location"] = vcs
print(data)
data["uploadType"] = headers["uploadType"] = "vcs"
elif url:
headers["uploadType"] = "url"
data = {"location": url} # type: ignore
data["location"] = url
data["uploadType"] = headers["uploadType"] = "url"
elif server:
headers["uploadType"] = "server"
data = {"location": server} # type: ignore
data["location"] = server
data["uploadType"] = headers["uploadType"] = "server"
headers["Content-Type"] = "application/json"
response = self.session.post(
f"{self.api}/uploads", data=json.dumps(data), headers=headers
endpoint,
data=json.dumps(data),
headers=headers,
)
else:
logger.info(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fossology"
version = "3.2.0"
version = "3.2.1"
description = "A library to automate Fossology from Python scripts"
authors = ["Marion Deveaud <marion.deveaud@siemens.com>"]
license = "MIT License"
Expand Down
16 changes: 8 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def foss(foss_server: str, foss_token: str, foss_agents: Agents) -> fossology.Fo


@pytest.fixture(scope="session")
def foss_v1(
def foss_v2(
foss_server: str, foss_token: str, foss_agents: Agents
) -> fossology.Fossology:
try:
foss = fossology.Fossology(foss_server, foss_token, version="v1")
foss = fossology.Fossology(foss_server, foss_token, version="v2")
except (FossologyApiError, AuthenticationError) as error:
exit(error.message)

Expand Down Expand Up @@ -231,20 +231,20 @@ def upload(


@pytest.fixture(scope="function")
def upload_v1(
foss_v1: fossology.Fossology,
def upload_v2(
foss_v2: fossology.Fossology,
test_file_path: str,
) -> Generator:
upload = foss_v1.upload_file(
foss_v1.rootFolder,
upload = foss_v2.upload_file(
foss_v2.rootFolder,
file=test_file_path,
description="Test upload via fossology-python lib",
access_level=AccessLevel.PUBLIC,
wait_time=5,
)
jobs_lookup(foss_v1, upload)
jobs_lookup(foss_v2, upload)
yield upload
foss_v1.delete_upload(upload)
foss_v2.delete_upload(upload)
time.sleep(5)


Expand Down
28 changes: 26 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ def test_get_info(foss: Fossology):


@responses.activate
def test_info_does_not_return_200(foss_server: str, foss: Fossology):
def test_info_v2_does_not_return_200(foss_server: str, foss_v2: Fossology):
responses.add(
responses.GET,
f"{foss_server}/api/v2/info",
status=400,
)
with pytest.raises(FossologyApiError) as excinfo:
foss_v2.get_info()
assert "Error while getting API info" in str(excinfo.value)


@responses.activate
def test_info_does_not_return_200(foss_server: str, foss: Fossology):
responses.add(
responses.GET,
f"{foss_server}/api/v1/info",
status=400,
)
with pytest.raises(FossologyApiError) as excinfo:
foss.get_info()
assert "Error while getting API info" in str(excinfo.value)
Expand All @@ -36,9 +48,21 @@ def test_get_health(foss: Fossology):
def test_health_does_not_return_200(foss_server: str, foss: Fossology):
responses.add(
responses.GET,
f"{foss_server}/api/v2/health",
f"{foss_server}/api/v1/health",
status=503,
)
with pytest.raises(FossologyApiError) as excinfo:
foss.get_health()
assert "Error while getting health info" in str(excinfo.value)


@responses.activate
def test_health_v2_does_not_return_200(foss_server: str, foss_v2: Fossology):
responses.add(
responses.GET,
f"{foss_server}/api/v2/health",
status=503,
)
with pytest.raises(FossologyApiError) as excinfo:
foss_v2.get_health()
assert "Error while getting health info" in str(excinfo.value)
6 changes: 3 additions & 3 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def test_item_info(foss: Fossology, upload_with_jobs: Upload):
assert info.meta_info


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


Expand Down
60 changes: 60 additions & 0 deletions tests/test_upload_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ def test_upload_from_vcs(foss: Fossology):
delete_upload(foss, vcs_upload)


def test_upload_from_vcs_v2(foss_v2: Fossology):
vcs = {
"vcsType": "git",
"vcsUrl": "https://github.com/fossology/fossology-python",
"vcsName": "fossology-python-github-master",
"vcsUsername": "",
"vcsPassword": "",
}
vcs_upload = foss_v2.upload_file(
foss_v2.rootFolder,
vcs=vcs,
description="Test upload from github repository via python lib",
access_level=AccessLevel.PUBLIC,
ignore_scm=False,
wait_time=5,
)
assert vcs_upload.uploadname == vcs["vcsName"]
# Cleanup
delete_upload(foss_v2, vcs_upload)


def test_upload_from_url(foss: Fossology):
url = {
"url": "https://github.com/fossology/fossology-python/archive/master.zip",
Expand All @@ -63,6 +84,26 @@ def test_upload_from_url(foss: Fossology):
delete_upload(foss, url_upload)


def test_upload_from_url_v2(foss_v2: Fossology):
url = {
"url": "https://github.com/fossology/fossology-python/archive/master.zip",
"name": "fossology-python-master.zip",
"accept": "zip",
"reject": "",
"maxRecursionDepth": "1",
}
url_upload = foss_v2.upload_file(
foss_v2.rootFolder,
url=url,
description="Test upload from url via python lib",
access_level=AccessLevel.PUBLIC,
wait_time=5,
)
assert url_upload.uploadname == url["name"]
# Cleanup
delete_upload(foss_v2, url_upload)


def test_upload_from_server(foss: Fossology):
server = {
"path": "/tmp/base-files-11",
Expand All @@ -80,3 +121,22 @@ def test_upload_from_server(foss: Fossology):

# Cleanup
delete_upload(foss, server_upload)


def test_upload_from_server_v2(foss_v2: Fossology):
server = {
"path": "/tmp/base-files-11",
"name": "base-files-11",
}
server_upload = foss_v2.upload_file(
foss_v2.rootFolder,
server=server,
description="Test upload from server via python lib",
access_level=AccessLevel.PUBLIC,
apply_global=True,
wait_time=5,
)
assert server_upload.uploadname == server["name"]

# Cleanup
delete_upload(foss_v2, server_upload)
Loading

0 comments on commit dc0e0b7

Please # to comment.