Skip to content

Commit

Permalink
Updated get_products call to handle business_unit_id as optional, add…
Browse files Browse the repository at this point in the history
…ed new query for GET_PRODUCTS and a function for handling variables
  • Loading branch information
nickvido committed Jan 24, 2024
1 parent 5a8794f commit 2218d32
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
13 changes: 8 additions & 5 deletions finite_state_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')


Expand Down Expand Up @@ -1291,14 +1295,16 @@ 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:
token (str):
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:
Expand All @@ -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:
Expand Down
65 changes: 65 additions & 0 deletions finite_state_sdk/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2218d32

Please sign in to comment.