extracted client

This commit is contained in:
Bryce
2025-11-09 15:57:22 -08:00
parent 66e44d42a9
commit 5a61777128
3 changed files with 173 additions and 200 deletions

167
app.py
View File

@@ -6,11 +6,12 @@ import pytz
from flask import Flask, render_template, request, redirect, url_for, session, abort, jsonify
from dotenv import load_dotenv
load_dotenv()
import firebase_admin
from firebase_admin import credentials, auth as fb_auth, firestore
import requests
from filevine_client import FilevineClient
load_dotenv()
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET_KEY", os.urandom(32))
@@ -126,18 +127,19 @@ def fetch_all_projects():
"""Fetch all projects for a user and store them in Firestore"""
print("Fetching projects....")
# Get bearer token
bearer = get_filevine_bearer()
# Initialize Filevine client
client = FilevineClient()
bearer = client.get_bearer_token()
# List projects (all pages)
projects = list_all_projects(bearer)
projects = client.list_all_projects()
projects = projects[:]
# Fetch details for each
detailed_rows = []
import worker_pool
detailed_rows = worker_pool.process_projects_parallel(projects, bearer, 9)
detailed_rows = worker_pool.process_projects_parallel(projects, client, 9)
# Store the results in Firestore
projects_ref = db.collection("projects")
@@ -208,160 +210,7 @@ def welcome():
# --- Filevine API ---
def get_filevine_bearer():
url = "https://identity.filevine.com/connect/token"
data = {
"client_id": FV_CLIENT_ID,
"client_secret": FV_CLIENT_SECRET,
"grant_type": "personal_access_token",
"scope": "fv.api.gateway.access tenant filevine.v2.api.* email openid fv.auth.tenant.read",
"token": FV_PAT,
}
headers = {"Accept": "application/json"}
resp = requests.post(url, data=data, headers=headers, timeout=30)
resp.raise_for_status()
js = resp.json()
token = js.get("access_token")
print(f"Got bearer js", js)
return token
def list_all_projects(bearer: str):
base = "https://api.filevineapp.com/fv-app/v2/Projects?limit=500"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
results = []
last_count = None
tries = 0
offset = 0
# TODO we probably need to sync the data with fierbase
cnt = 0
while True:
cnt = len(results)
print(f"list try {tries}, starting at {offset}, previous count {last_count}, currently at {cnt}")
tries += 1
url = base
params = {}
if last_count is not None:
# Some deployments use LastID/Offset pagination; adapt if needed
offset = offset + last_count
params["offset"] = offset
r = requests.get(url, headers=headers, params=params, timeout=30)
r.raise_for_status()
page = r.json()
from pprint import pprint
print(f"Fetched page. Headers: {r.headers}, Offset: {offset}")
items = page.get("items", [])
results.extend(items)
has_more = page.get("hasMore")
last_count = page.get("count")
if not has_more:
break
# Safety valve
if tries > 200:
break
return results
def fetch_project_detail(bearer: str, project_id_native: int):
url = f"https://api.filevineapp.com/fv-app/v2/Projects/{project_id_native}"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return r.json()
def fetch_project_team(bearer: str, project_id_native: int):
url = f"https://api.filevineapp.com/fv-app/v2/Projects/{project_id_native}/team?limit=1000"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
from pprint import pprint
return r.json().get('items') or []
def fetch_project_tasks(bearer: str, project_id_native: int):
url = f"https://api.filevineapp.com/fv-app/v2/Projects/{project_id_native}/tasks"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return r.json()
def fetch_client(bearer: str, client_id_native: int):
url = f"https://api.filevineapp.com/fv-app/v2/contacts/{client_id_native}"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return r.json()
def fetch_contacts(bearer: str, project_id_native: int):
url = f"https://api.filevineapp.com/fv-app/v2/projects/{project_id_native}/contacts"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return r.json().get("items")
def fetch_form(bearer: str, project_id_native: int, form: str):
try:
url = f"https://api.filevineapp.com/fv-app/v2/Projects/{project_id_native}/Forms/{form}"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return r.json()
except Exception as e:
print(e)
return {}
def fetch_collection(bearer: str, project_id_native: int, collection: str):
try:
url = f"https://api.filevineapp.com/fv-app/v2/Projects/{project_id_native}/Collections/{collection}"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {bearer}",
"x-fv-orgid": str(FV_ORG_ID),
"x-fv-userid": str(FV_USER_ID),
}
r = requests.get(url, headers=headers, timeout=30)
r.raise_for_status()
return [x.get('dataObject') for x in r.json().get("items")]
except Exception as e:
print(e)
return {}
# Filevine client is now in filevine_client.py