Skip to content

Commit d2f85f2

Browse files
authored
Improved error handling to assist customer (#48)
* Improve error handling for GET call to /groups API
1 parent 5d7e764 commit d2f85f2

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

project-collections/build_collection.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,27 @@ def next_page(response):
3535

3636
def add_proj_to_collection(headers, args, org, collection_id):
3737
op_pagination = None
38-
while True:
39-
# Use of the project_tags ensures only those with the right tag are returned
40-
op_response = json.loads(
41-
utils.rest_api.org_projects(headers, args["api_ver"], org,
42-
args["project_tags"], op_pagination))
43-
44-
for project in op_response['data']:
45-
# iterate over the tags in each project and persist it i it has one of the tags
46-
# of interest
47-
utils.rest_api.add_project_to_collection(headers, args, org, collection_id, project)
48-
49-
# Next page?
50-
op_pagination = next_page(op_response)
51-
if op_pagination is None:
52-
break
38+
op_response = None
39+
try:
40+
while True:
41+
# Use of the project_tags ensures only those with the right tag are returned
42+
op_response = json.loads(
43+
utils.rest_api.org_projects(headers, args["api_ver"], org,
44+
args["project_tags"], op_pagination))
45+
46+
for project in op_response['data']:
47+
# iterate over the tags in each project and persist it i it has one of the tags
48+
# of interest
49+
utils.rest_api.add_project_to_collection(headers, args, org, collection_id, project)
50+
51+
# Next page?
52+
op_pagination = next_page(op_response)
53+
if op_pagination is None:
54+
break
55+
except Exception:
56+
print("POST call to /collections - Unable to build collection")
57+
print(json.dumps(op_response, indent=4))
58+
return
5359

5460

5561

project-collections/utils/util_func.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ def process_collection(headers, args, func):
3131

3232
# Iterate to the named Org
3333
for org in go_response['data']:
34-
if org['attributes']['name'] == args["org_name"]:
34+
if org['attributes']['name'] == args["org_name"] or org['attributes']['slug'] == args["org_name"]:
3535

3636
# Find the collection id
3737
collection_id = find_collection(headers, args, org)
3838

3939
# Do the collection process within the passed function
4040
func(headers, args, org, collection_id)
4141

42+
# Now the named org has been processed, there's no need to continue
43+
return
44+
4245
# Next page?
4346
go_pagination = next_page(go_response)
4447
if go_pagination is None:
@@ -53,19 +56,26 @@ def find_collection(headers, args, org):
5356

5457
c_pagination = None
5558
collections = []
59+
response = None
60+
61+
try:
62+
while True:
63+
response = json.loads(utils.rest_api.get_collections(headers, args["api_ver"], org, c_pagination))
64+
collections = collections + response["data"]
5665

57-
while True:
58-
response = json.loads(utils.rest_api.get_collections(headers, args["api_ver"], org, c_pagination))
59-
collections = collections + response["data"]
66+
# Next page?
67+
c_pagination = next_page(response)
68+
if c_pagination is None:
69+
break
6070

61-
# Next page?
62-
c_pagination = next_page(response)
63-
if c_pagination is None:
64-
break
71+
for coll in response["data"]:
72+
if coll['attributes']['name'] == args["collection_name"]:
73+
return coll['id']
6574

66-
for coll in response["data"]:
67-
if coll['attributes']['name'] == args["collection_name"]:
68-
return coll['id']
75+
return utils.rest_api.create_a_collection(headers, args, org)
76+
except Exception:
77+
print("GET call to /collections API returned no 'data'")
78+
print(json.dumps(response, indent=4))
79+
return
6980

70-
return utils.rest_api.create_a_collection(headers, args, org)
7181

0 commit comments

Comments
 (0)