feat: Implement company querying functionality with natural language processing and logging
This commit is contained in:
+10
-68
@@ -1,14 +1,24 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
import requests
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
handlers=[logging.StreamHandler()],
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FolkAPI:
|
||||
BASE_URL = "https://api.folk.app/v1"
|
||||
|
||||
def __init__(self, api_key: str):
|
||||
api_key = os.environ.get("FOLK_API_KEY", api_key)
|
||||
self.headers = {"Authorization": f"Bearer {api_key}"}
|
||||
logger.info(f"FolkAPI initialized with API key: {api_key[:4]}***")
|
||||
|
||||
def get_groups(self):
|
||||
"""Fetch all groups from Folk."""
|
||||
@@ -190,71 +200,3 @@ class FolkAPI:
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
|
||||
# Prefer getting the API key from the environment. If not set, fall back to the
|
||||
# existing (hard-coded) key so behavior is unchanged for now.
|
||||
DEFAULT_API_KEY = "FOLKfIGXuv74ML9EAajxyiUR39ePaNrZ"
|
||||
api_key = os.environ.get("FOLK_API_KEY", DEFAULT_API_KEY)
|
||||
|
||||
folk = FolkAPI(api_key=api_key)
|
||||
|
||||
|
||||
def example_flow():
|
||||
# Step 1: Get groups
|
||||
groups = folk.get_groups()
|
||||
print(groups)
|
||||
|
||||
# Safely dig into the returned structure. The API returns groups under
|
||||
# groups['data']['items'] (not groups['data'][0]). Handle missing/empty.
|
||||
items = groups.get("data", {}).get("items", [])
|
||||
if not items:
|
||||
print("No groups returned by Folk API.")
|
||||
sys.exit(1)
|
||||
|
||||
# Choose the first group as an example
|
||||
group_id = items[0].get("id")
|
||||
if not group_id:
|
||||
print("No id found for the first group item.")
|
||||
sys.exit(1)
|
||||
|
||||
# Step 2: Choose a group_id and create a company
|
||||
company = folk.create_company(
|
||||
name="2050 Investment Partners",
|
||||
group_id=group_id,
|
||||
website="https://2050.com",
|
||||
linkedin_url="https://linkedin.com/company/2050-investments",
|
||||
)
|
||||
|
||||
# Step 3: Add a person to the same group or company
|
||||
person = folk.create_person(
|
||||
first_name="John",
|
||||
last_name="Doe",
|
||||
email="john@2050.com",
|
||||
company_id=company.get("data", {}).get("id"),
|
||||
group_id=group_id,
|
||||
)
|
||||
|
||||
print("Created company:", company)
|
||||
print("Created person:", person)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
example_flow()
|
||||
except requests.HTTPError as e:
|
||||
# Try to include response body for easier debugging if available
|
||||
resp = getattr(e, "response", None)
|
||||
if resp is not None:
|
||||
try:
|
||||
body = resp.text
|
||||
except Exception:
|
||||
body = "<unreadable response body>"
|
||||
print("HTTP error while talking to Folk API:", e)
|
||||
print("Response status:", resp.status_code)
|
||||
print("Response body:", body)
|
||||
else:
|
||||
print("HTTP error while talking to Folk API:", e)
|
||||
sys.exit(1)
|
||||
except Exception as e: # pragma: no cover - top-level safety
|
||||
print("Unexpected error:", e)
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user