refactor langflow calls

This commit is contained in:
phact 2025-12-04 15:16:25 -05:00
parent 50d62c2cc0
commit d33cf7ecc8

View file

@ -568,49 +568,41 @@ class AppClients:
self, name: str, value: str, modify: bool = False self, name: str, value: str, modify: bool = False
): ):
"""Create a global variable in Langflow via API""" """Create a global variable in Langflow via API"""
api_key = await get_langflow_api_key()
if not api_key:
logger.warning(
"Cannot create Langflow global variable: No API key", variable_name=name
)
return
url = f"{LANGFLOW_URL}/api/v1/variables/"
payload = { payload = {
"name": name, "name": name,
"value": value, "value": value,
"default_fields": [], "default_fields": [],
"type": "Credential", "type": "Credential",
} }
headers = {"x-api-key": api_key, "Content-Type": "application/json"}
try: try:
async with httpx.AsyncClient() as client: response = await self.langflow_request(
response = await client.post(url, headers=headers, json=payload) "POST", "/api/v1/variables/", json=payload
)
if response.status_code in [200, 201]: if response.status_code in [200, 201]:
logger.info(
"Successfully created Langflow global variable",
variable_name=name,
)
elif response.status_code == 400 and "already exists" in response.text:
if modify:
logger.info( logger.info(
"Successfully created Langflow global variable", "Langflow global variable already exists, attempting to update",
variable_name=name, variable_name=name,
) )
elif response.status_code == 400 and "already exists" in response.text: await self._update_langflow_global_variable(name, value)
if modify:
logger.info(
"Langflow global variable already exists, attempting to update",
variable_name=name,
)
await self._update_langflow_global_variable(name, value)
else:
logger.info(
"Langflow global variable already exists",
variable_name=name,
)
else: else:
logger.warning( logger.info(
"Failed to create Langflow global variable", "Langflow global variable already exists",
variable_name=name, variable_name=name,
status_code=response.status_code,
) )
else:
logger.warning(
"Failed to create Langflow global variable",
variable_name=name,
status_code=response.status_code,
)
except Exception as e: except Exception as e:
logger.error( logger.error(
"Exception creating Langflow global variable", "Exception creating Langflow global variable",
@ -620,76 +612,62 @@ class AppClients:
async def _update_langflow_global_variable(self, name: str, value: str): async def _update_langflow_global_variable(self, name: str, value: str):
"""Update an existing global variable in Langflow via API""" """Update an existing global variable in Langflow via API"""
api_key = await get_langflow_api_key()
if not api_key:
logger.warning(
"Cannot update Langflow global variable: No API key", variable_name=name
)
return
headers = {"x-api-key": api_key, "Content-Type": "application/json"}
try: try:
async with httpx.AsyncClient() as client: # First, get all variables to find the one with the matching name
# First, get all variables to find the one with the matching name get_response = await self.langflow_request("GET", "/api/v1/variables/")
get_response = await client.get(
f"{LANGFLOW_URL}/api/v1/variables/", headers=headers if get_response.status_code != 200:
logger.error(
"Failed to retrieve variables for update",
variable_name=name,
status_code=get_response.status_code,
) )
return
if get_response.status_code != 200: variables = get_response.json()
logger.error( target_variable = None
"Failed to retrieve variables for update",
variable_name=name,
status_code=get_response.status_code,
)
return
variables = get_response.json() # Find the variable with matching name
target_variable = None for variable in variables:
if variable.get("name") == name:
target_variable = variable
break
# Find the variable with matching name if not target_variable:
for variable in variables: logger.error("Variable not found for update", variable_name=name)
if variable.get("name") == name: return
target_variable = variable
break
if not target_variable: variable_id = target_variable.get("id")
logger.error("Variable not found for update", variable_name=name) if not variable_id:
return logger.error("Variable ID not found for update", variable_name=name)
return
variable_id = target_variable.get("id") # Update the variable using PATCH
if not variable_id: update_payload = {
logger.error("Variable ID not found for update", variable_name=name) "id": variable_id,
return "name": name,
"value": value,
"default_fields": target_variable.get("default_fields", []),
}
# Update the variable using PATCH patch_response = await self.langflow_request(
update_payload = { "PATCH", f"/api/v1/variables/{variable_id}", json=update_payload
"id": variable_id, )
"name": name,
"value": value,
"default_fields": target_variable.get("default_fields", []),
}
patch_response = await client.patch( if patch_response.status_code == 200:
f"{LANGFLOW_URL}/api/v1/variables/{variable_id}", logger.info(
headers=headers, "Successfully updated Langflow global variable",
json=update_payload, variable_name=name,
variable_id=variable_id,
)
else:
logger.warning(
"Failed to update Langflow global variable",
variable_name=name,
variable_id=variable_id,
status_code=patch_response.status_code,
response_text=patch_response.text,
) )
if patch_response.status_code == 200:
logger.info(
"Successfully updated Langflow global variable",
variable_name=name,
variable_id=variable_id,
)
else:
logger.warning(
"Failed to update Langflow global variable",
variable_name=name,
variable_id=variable_id,
status_code=patch_response.status_code,
response_text=patch_response.text,
)
except Exception as e: except Exception as e:
logger.error( logger.error(