Skip to content

Commit adb4876

Browse files
committed
feat: get_components_by_type() and get_components_by_name() support now paging
1 parent 8406f4e commit adb4876

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

sw360/components.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,43 @@ def get_all_components(self, fields: str = "", page: int = -1, page_size: int =
7070

7171
return resp
7272

73-
def get_components_by_type(self, component_type: str) -> List[Dict[str, Any]]:
73+
def get_components_by_type(
74+
self,
75+
component_type: str,
76+
page: int = -1,
77+
page_size: int = -1,
78+
sort: str = "") -> List[Dict[str, Any]]:
7479
"""Get information of about all components for certain type
7580
7681
API endpoint: GET /components
7782
7883
:param component_type: the type of the component to be requested, one
7984
of INTERNAL, OSS, COTS, FREESOFTWARE, INNER_SOURCE, SERVICE
8085
:type component_type: string
86+
:param page: page to retrieve
87+
:type page: int
88+
:param page_size: page size to use
89+
:type page_size: int
90+
:param sort: sort order for the components ("name,desc"; "name,asc")
91+
:type sort: str
8192
:return: list of components
8293
:rtype: list of JSON component objects
8394
:raises SW360Error: if there is a negative HTTP response
8495
"""
8596

86-
resp = self.api_get(self.url + "resource/api/components?type=" + component_type)
97+
url = self.url + "resource/api/components?type=" + component_type
98+
99+
if page > -1:
100+
url = self._add_param(url, "page=" + str(page))
101+
url = self._add_param(url, "page_entries=" + str(page_size))
102+
103+
if sort:
104+
# ensure HTML encoding
105+
sort = sort.replace(",", "%2C")
106+
url = self._add_param(url, "sort=" + sort)
107+
108+
resp = self.api_get(url)
109+
87110
if resp and ("_embedded" in resp) and ("sw360:components" in resp["_embedded"]):
88111
return resp["_embedded"]["sw360:components"]
89112

@@ -119,19 +142,40 @@ def get_component_by_url(self, component_url: str) -> Optional[Dict[str, Any]]:
119142
resp = self.api_get(component_url)
120143
return resp
121144

122-
def get_component_by_name(self, component_name: str) -> Optional[Dict[str, Any]]:
145+
def get_component_by_name(
146+
self,
147+
component_name: str,
148+
page: int = -1,
149+
page_size: int = -1,
150+
sort: str = "") -> Optional[Dict[str, Any]]:
123151
"""Get information of about a component
124152
125153
API endpoint: GET /components
126154
127155
:param component_name: the name of the component to look for
128156
:type component_name: string
157+
:param page: page to retrieve
158+
:type page: int
159+
:param page_size: page size to use
160+
:type page_size: int
161+
:param sort: sort order for the components ("name,desc"; "name,asc")
162+
:type sort: str
129163
:return: list of components
130164
:rtype: list of JSON component objects
131165
:raises SW360Error: if there is a negative HTTP response
132166
"""
133167

134-
resp = self.api_get(self.url + "resource/api/components?name=" + component_name)
168+
url = self.url + "resource/api/components?name=" + component_name
169+
if page > -1:
170+
url = self._add_param(url, "page=" + str(page))
171+
url = self._add_param(url, "page_entries=" + str(page_size))
172+
173+
if sort:
174+
# ensure HTML encoding
175+
sort = sort.replace(",", "%2C")
176+
url = self._add_param(url, "sort=" + sort)
177+
178+
resp = self.api_get(url)
135179
return resp
136180

137181
def get_components_by_external_id(self, ext_id_name: str, ext_id_value: str = "") -> List[Dict[str, Any]]:

tests/test_sw360_components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,4 @@ def test_get_recent_components(self) -> None:
909909

910910
if __name__ == "__main__":
911911
x = Sw360TestComponents()
912-
x.test_get_all_components_with_fields_and_paging()
912+
x.test_get_all_components_by_type()

0 commit comments

Comments
 (0)