File: ObjServiceFHShopify.py
Lines: 1538-2618 (1,080 lines)
Complexity: VERY HIGH
The CreateProduct method is extremely complex with:
Break CreateProduct into smaller, focused methods:
_build_product_input(sku, data, option_level)
_build_variants_input(variants_data, option_level)
_build_options_input(option_level, data)
_build_metafields_input(metafields_data)
_get_promotion_data(sku)
_calculate_variant_price(base_price, promotion_data)
_save_product_response(response, sku, product_id)
Reduce CreateProduct to orchestration only:
def CreateProduct(self, param1, param2, js_data, option_level=1):
"""Create or update product using GraphQL"""
self.debug(f"CreateProduct: {param1}, Options: {option_level}")
# Parse input
product_data = self._parse_product_data(js_data)
# Get promotion info
promotion_data = self._get_promotion_data(param1)
# Build ProductInput
product_input = self._build_product_input(
param1,
product_data,
option_level
)
# Add variants
product_input["variants"] = self._build_variants_input(
product_data.get("variants", []),
option_level,
promotion_data
)
# Add options
product_input["options"] = self._build_options_input(
option_level,
product_data
)
# Add metafields
if "metafields" in product_data:
product_input["metafields"] = self._build_metafields_input(
product_data["metafields"]
)
# Check if exists
existing_gid = self._get_product_graphql_id(param1)
# Create or update
if existing_gid:
product_input["id"] = existing_gid
response = self._execute_graphql_query(
GRAPHQL_PRODUCT_UPDATE,
{"input": product_input}
)
mutation_key = "productUpdate"
else:
response = self._execute_graphql_query(
GRAPHQL_PRODUCT_CREATE,
{"input": product_input}
)
mutation_key = "productCreate"
# Process response
if response and mutation_key in response:
product = response[mutation_key].get("product")
if product:
product_id = self._convert_gid_to_rest_id(product["id"])
self._save_product_response(product, param1, product_id)
# Handle images separately
if "images" in product_data:
self.UpdateImages(
param1,
product_id,
product_data["images"]
)
return True
return False
Result: Main method ~80 lines instead of 1,080
Each helper method should be testable independently:
# Test variant building
def test_build_variants_input():
variants_data = [
{"sku": "TEST-001", "price": "100.00", "weight": 5.0}
]
result = service._build_variants_input(variants_data, 0)
assert len(result) == 1
assert result[0]["sku"] == "TEST-001"
Create basic working version that handles core scenarios:
Extract helper methods and improve structure:
Optimize and enhance: