Skip to content

Commit

Permalink
Improved error handling to assist customer (#48)
Browse files Browse the repository at this point in the history
* Improve error handling for GET call to /groups API
  • Loading branch information
CSMonkee authored Mar 22, 2024
1 parent 5e38ac6 commit 1e2e5f9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
36 changes: 21 additions & 15 deletions project-collections/build_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,27 @@ def next_page(response):

def add_proj_to_collection(headers, args, org, collection_id):
op_pagination = None
while True:
# Use of the project_tags ensures only those with the right tag are returned
op_response = json.loads(
utils.rest_api.org_projects(headers, args["api_ver"], org,
args["project_tags"], op_pagination))

for project in op_response['data']:
# iterate over the tags in each project and persist it i it has one of the tags
# of interest
utils.rest_api.add_project_to_collection(headers, args, org, collection_id, project)

# Next page?
op_pagination = next_page(op_response)
if op_pagination is None:
break
op_response = None
try:
while True:
# Use of the project_tags ensures only those with the right tag are returned
op_response = json.loads(
utils.rest_api.org_projects(headers, args["api_ver"], org,
args["project_tags"], op_pagination))

for project in op_response['data']:
# iterate over the tags in each project and persist it i it has one of the tags
# of interest
utils.rest_api.add_project_to_collection(headers, args, org, collection_id, project)

# Next page?
op_pagination = next_page(op_response)
if op_pagination is None:
break
except Exception:
print("POST call to /collections - Unable to build collection")
print(json.dumps(op_response, indent=4))
return



Expand Down
34 changes: 22 additions & 12 deletions project-collections/utils/util_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ def process_collection(headers, args, func):

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

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

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

# Now the named org has been processed, there's no need to continue
return

# Next page?
go_pagination = next_page(go_response)
if go_pagination is None:
Expand All @@ -53,19 +56,26 @@ def find_collection(headers, args, org):

c_pagination = None
collections = []
response = None

try:
while True:
response = json.loads(utils.rest_api.get_collections(headers, args["api_ver"], org, c_pagination))
collections = collections + response["data"]

while True:
response = json.loads(utils.rest_api.get_collections(headers, args["api_ver"], org, c_pagination))
collections = collections + response["data"]
# Next page?
c_pagination = next_page(response)
if c_pagination is None:
break

# Next page?
c_pagination = next_page(response)
if c_pagination is None:
break
for coll in response["data"]:
if coll['attributes']['name'] == args["collection_name"]:
return coll['id']

for coll in response["data"]:
if coll['attributes']['name'] == args["collection_name"]:
return coll['id']
return utils.rest_api.create_a_collection(headers, args, org)
except Exception:
print("GET call to /collections API returned no 'data'")
print(json.dumps(response, indent=4))
return

return utils.rest_api.create_a_collection(headers, args, org)

0 comments on commit 1e2e5f9

Please sign in to comment.