From 2218d3288be94bc1a924d6ca39f1c716d94c1bac Mon Sep 17 00:00:00 2001 From: Nicholas Vidovich Date: Wed, 24 Jan 2024 13:54:53 -0500 Subject: [PATCH] Updated get_products call to handle business_unit_id as optional, added new query for GET_PRODUCTS and a function for handling variables --- finite_state_sdk/__init__.py | 13 +++++--- finite_state_sdk/queries.py | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/finite_state_sdk/__init__.py b/finite_state_sdk/__init__.py index 76280d9..09ab26a 100644 --- a/finite_state_sdk/__init__.py +++ b/finite_state_sdk/__init__.py @@ -1,6 +1,7 @@ import json import requests import time +from warnings import warn import finite_state_sdk.queries as queries API_URL = 'https://platform.finitestate.io/api/v1/graphql' @@ -1108,7 +1109,10 @@ def get_all_products(token, organization_context): Returns: list: List of Product Objects + + .. deprecated:: 0.1.4. Use get_products instead. """ + warn('`get_all_products` is deprecated. Use: `get_products instead`', DeprecationWarning, stacklevel=2) return get_all_paginated_results(token, organization_context, queries.ALL_PRODUCTS['query'], queries.ALL_PRODUCTS['variables'], 'allProducts') @@ -1291,7 +1295,7 @@ def get_product_asset_versions(token, organization_context, product_id=None): return get_all_paginated_results(token, organization_context, queries.GET_PRODUCT_ASSET_VERSIONS['query'], queries.GET_PRODUCT_ASSET_VERSIONS['variables'](product_id), 'allProducts') -def get_products(token, organization_context, business_unit_id=None) -> list: +def get_products(token, organization_context, product_id=None, business_unit_id=None) -> list: """ Gets all the products for the specified business unit. Args: @@ -1299,6 +1303,8 @@ def get_products(token, organization_context, business_unit_id=None) -> list: Auth token. This is the token returned by get_auth_token(). Just the token, do not include "Bearer" in this string, that is handled inside the method. organization_context (str): Organization context. This is provided by the Finite State API management. It looks like "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx". + product_id (str, optional): + Product ID to get. If not provided, will get all products in the organization. business_unit_id (str, optional): Business Unit ID to get products for. If not provided, will get all products in the organization. Raises: @@ -1307,10 +1313,7 @@ def get_products(token, organization_context, business_unit_id=None) -> list: list: List of Product Objects """ - if not business_unit_id: - raise Exception("Business Unit ID is required") - - return get_all_paginated_results(token, organization_context, queries.GET_PRODUCTS_BUSINESS_UNIT['query'], queries.GET_PRODUCTS_BUSINESS_UNIT['variables'](business_unit_id), 'allProducts') + return get_all_paginated_results(token, organization_context, queries.GET_PRODUCTS['query'], queries.GET_PRODUCTS['variables'](product_id=product_id, business_unit_id=business_unit_id), 'allProducts') def generate_report_download_url(token, organization_context, asset_version_id=None, product_id=None, report_type=None, report_subtype=None, verbose=False) -> str: diff --git a/finite_state_sdk/queries.py b/finite_state_sdk/queries.py index 0467056..87cc3a4 100644 --- a/finite_state_sdk/queries.py +++ b/finite_state_sdk/queries.py @@ -247,11 +247,25 @@ def artifact_variables(artifact_id=None, business_unit_id=None): id name createdAt + createdBy { + id + email + __typename + } + deletedAt ctx { asset businessUnits products } + defaultVersion { + name + createdAt + __typename + } + _versionsMeta { + count + } __typename } } @@ -666,6 +680,57 @@ def _create_GET_SOFTWARE_COMPONENTS_VARIABLES(asset_version_id=None, type=None): } +def _create_GET_PRODUCTS_VARIABLES(product_id=None, business_unit_id=None): + variables = { + "filter": {}, + "after": None, + "first": 100 + } + + if product_id: + variables["filter"]["id"] = product_id + + if business_unit_id: + variables["filter"]["group"] = { + "id": business_unit_id + } + + return variables + + +GET_PRODUCTS = { + "query": """ + query GetAllProducts( + $filter: ProductFilter!, + $after: String, + $first: Int + ) { + allProducts( + filter: $filter, + after: $after, + first: $first + ) { + _cursor + id + name + createdAt + createdBy { + id + email + __typename + } + group { + id + name + } + __typename + } + } + """, + "variables": lambda product_id=None, business_unit_id=None: _create_GET_PRODUCTS_VARIABLES(product_id=product_id, business_unit_id=business_unit_id) +} + + GET_PRODUCTS_BUSINESS_UNIT = { "query": """ query GetAllProducts(