Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions sw360/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __get_components_filtered(
:type page_size: int
:param sort: sort order for the components (Sort by name if `None`)
:type sort: SortParam
:return: list of components
:return: response from API
:rtype: list of JSON component objects
:raises SW360Error: if there is a negative HTTP response
"""
Expand All @@ -55,12 +55,7 @@ def __get_components_filtered(
else:
resp = self.api_get(full_url)

if (resp and
"_embedded" in resp and
"sw360:components" in resp["_embedded"]):
return resp["_embedded"]["sw360:components"]

return []
return resp

def get_all_components(
self, fields: str = "", page: int = -1, page_size: int = -1,
Expand Down Expand Up @@ -99,8 +94,16 @@ def get_all_components(
if sort is None:
sort = ComponentSortColumn.NAME.asc()

return self.__get_components_filtered(url_with_param, page, page_size,
resp = self.__get_components_filtered(url_with_param, page, page_size,
sort)
if (resp and
"_embedded" in resp and
"sw360:components" in resp["_embedded"]):
if page == -1:
return resp["_embedded"]["sw360:components"]
return resp

return []

def get_components_by_type(
self, component_type: str, page: int = -1, page_size: int = -1,
Expand Down Expand Up @@ -135,9 +138,14 @@ def get_components_by_type(
if sort is None:
sort = ComponentSortColumn.NAME.asc()

return self.__get_components_filtered(url_with_param, page, page_size,
resp = self.__get_components_filtered(url_with_param, page, page_size,
sort)

if resp and ("_embedded" in resp) and ("sw360:components" in resp["_embedded"]):
return resp["_embedded"]["sw360:components"]

return []

def get_component(self, component_id: str) -> Optional[Dict[str, Any]]:
"""Get information of about a component

Expand Down Expand Up @@ -175,7 +183,7 @@ def get_component_by_url(self, component_url: str) -> Optional[Dict[str, Any]]:
def get_component_by_name(
self, component_name: str, page: int = -1, page_size: int = -1,
sort: Optional[SortParam] = None
) -> Dict[str, Any]:
) -> Optional[Dict[str, Any]]:
"""Get information of about a component

API endpoint: GET /components?name=
Expand Down Expand Up @@ -206,9 +214,11 @@ def get_component_by_name(
else:
sort = ComponentSortColumn.NAME.asc()

return self.__get_components_filtered(url_with_param, page, page_size,
resp = self.__get_components_filtered(url_with_param, page, page_size,
sort)

return resp

def get_components_by_external_id(self, ext_id_name: str, ext_id_value: str = "") -> List[Dict[str, Any]]:
"""Get components by external id. `ext_id_value` can be left blank to
search for all components with `ext_id_name`.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_sw360_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def test_get_all_components_with_fields_and_paging(self) -> None:
adding_headers={"Authorization": "Token " + self.MYTOKEN},
)

components = lib.get_all_components("ownerCountry", 1, 2)
data = lib.get_all_components("ownerCountry", 1, 2)
components = data["_embedded"]["sw360:components"]
self.assertIsNotNone(components)
self.assertTrue(len(components) > 0)
self.assertEqual("Tethys.Logging", components[0]["name"])
Expand Down
Loading