def get_all_repos(): """ Returns all repos the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_list = _get_all_repos(user_id) except Exception as e: return make_error("Error while getting list of repos: " + str(e)) return make_success(repo_list)
def reset_state(repo_id): """ Authorize request, then reset state for cloud node corresponding to given repo_id """ claims = authorize_user(request) if claims is None: return make_unauthorized_error() user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) is_demo = repo_details["IsDemo"] reset_cloud_node(repo_id, is_demo) except Exception as e: return make_error("Error resetting state: " + str(e)) return make_success(repo_id)
def get_logs(repo_id): """ Returns all logs for a Repo the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: _assert_user_can_read_repo(user_id, repo_id) logs = _get_logs(repo_id) except Exception as e: return make_error("Error getting logs: " + str(e)) return make_success(logs)
def delete_repo(repo_id): """ Deletes a repo under the authenticated user. """ # Check authorization claims = authorize_user(request) if not claims: return make_unauthorized_error() user_id = claims["pk"] try: _delete_repo(user_id, repo_id) except Exception as e: # TODO: Revert things. return make_error("Error deleting repo: " + str(e)) return make_success(repo_id)
def get_repo(repo_id): """ Returns a Repo's information (if the user has access to it). """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) except Exception as e: return make_error("Error while getting the details for this repo: " + str(e)) return make_success(repo_details)
def get_user_data(): """ Returns the authenticated user's data. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: user_data = _get_user_data(user_id) repos_remaining = user_data['ReposRemaining'] except Exception as e: return make_error("Error getting user data: " + str(e)) return make_success( {"ReposRemaining": True if repos_remaining > 0 else False})
def get_task_status(repo_id): """ Returns the status of the Cloud Node associated to a Repo the user has access to. """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get data user_id = claims["pk"] try: repo_details = _get_repo_details(user_id, repo_id) task_arns = [ repo_details["CloudTaskArn"], repo_details["ExploraTaskArn"] ] is_demo = repo_details["IsDemo"] status = get_status(task_arns, repo_id, is_demo) except Exception as e: return make_error("Error checking status: " + str(e)) return make_success(status)
def download_model(): """ Returns the download url of a model. Example HTTP POST Request body (JSON format): { "RepoId": "test", "SessionId": "c257efa4-791d-4130-bdc6-b0d3cee5fa25", "Round": 5 } """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get parameters params = request.get_json() if "RepoId" not in params: return make_error("Missing repo id from request.") if "SessionId" not in params: return make_error("Missing session id from request.") if "Round" not in params: return make_error("Missing round from request.") repo_id = params.get('RepoId', None) session_id = params.get('SessionId', None) round = params.get('Round', None) bucket_name = "updatestore" object_name = "{0}/{1}/{2}/model.h5".format(repo_id, session_id, round) # Get presigned URL user_id = claims["pk"] try: _assert_user_can_read_repo(user_id, repo_id) url = _create_presigned_url(bucket_name, object_name) except Exception as e: return make_error("Error getting download URL: " + str(e)) # Return url return make_success(url)
def create_new_repo(): """ Creates a new repo under the authenticated user. Example HTTP POST Request body (JSON format): { "RepoName": "repo_name", "RepoDescription": "Some description here." } """ # Check authorization claims = authorize_user(request) if claims is None: return make_unauthorized_error() # Get parameters # TODO: Sanitize inputs. params = request.get_json() if "RepoName" not in params: return make_error("Missing repo name from request.") if "RepoDescription" not in params: return make_error("Missing repo description from request.") repo_name = params["RepoName"][:20] repo_description = params["RepoDescription"][:80] # TODO: Check repo doesn't already exist. repo_name = re.sub('[^a-zA-Z0-9-]', '-', repo_name) user_id = claims["pk"] repo_id, api_key, token = _create_repo_id_and_api_key_and_token() try: _create_new_repo(user_id, repo_id, api_key, repo_name, \ repo_description, token, False) except Exception as e: # TODO: Revert things. return make_error("Error creating new repo: " + str(e)) return make_success({"RepoId": repo_id, "ApiKey": api_key})