Files
rothbard/generate_sample.py
2025-11-05 13:46:43 -08:00

237 lines
8.1 KiB
Python

import os
import json
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def get_bearer_token():
"""Request a bearer token using credentials from environment variables."""
url = "https://identity.filevine.com/connect/token"
data = {
"client_id": os.environ.get("FILEVINE_CLIENT_ID"),
"client_secret": os.environ.get("FILEVINE_CLIENT_SECRET"),
"grant_type": "personal_access_token",
"scope": "fv.api.gateway.access tenant filevine.v2.api.* email openid fv.auth.tenant.read",
"token": os.environ.get("FILEVINE_PERSONAL_ACCESS_TOKEN"),
}
headers = {"Accept": "application/json"}
response = requests.post(url, data=data, headers=headers)
response.raise_for_status()
return response.json().get("access_token")
# Define the collection of request/response pairs
# User can add more tuples following the same pattern
SAMPLE_PROJECT_ID = 15974631
SAMPLE_CLIENT_ID = 43407853
SAMPLE_FOLDER_ID = 138954131
SAMPLE_PROJECT_TYPE = 34111
REQUESTS = [
# (
# {
# "url": "https://api.filevineapp.com/fv-app/v2/projects",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/project_list.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/projects/{SAMPLE_PROJECT_ID}",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/project.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/Projects/{SAMPLE_PROJECT_ID}/contacts",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/project_contacts.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/contacts/{SAMPLE_CLIENT_ID}",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/client.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/folders/{SAMPLE_FOLDER_ID}",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/folder.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/folders/{SAMPLE_FOLDER_ID}/children",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/folder_children.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/DocumentSearch?projectId={SAMPLE_PROJECT_ID}&searchTerm=notice",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/document_search.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/ProjectTypes/{SAMPLE_PROJECT_TYPE}",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/project_type.json"
# ),
# (
# {
# "url": f"https://api.filevineapp.com/fv-app/v2/ProjectTypes/{SAMPLE_PROJECT_TYPE}/sections",
# "headers": {
# "Accept": "application/json",
# "Authorization": f"Bearer {get_bearer_token()}",
# "x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
# "x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
# },
# "params": {},
# "method": "GET"
# },
# "examples/project_type_sections.json"
# ),
(
{
"url": f"https://api.filevineapp.com/fv-app/v2/Projects/{SAMPLE_PROJECT_ID}/Forms/complaintInfo",
"headers": {
"Accept": "application/json",
"Authorization": f"Bearer {get_bearer_token()}",
"x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
"x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
},
"params": {},
"method": "GET"
},
"examples/forms__complaintInfo.json"
),
(
{
"url": f"https://api.filevineapp.com/fv-app/v2/Projects/{SAMPLE_PROJECT_ID}/Forms/newFileReview",
"headers": {
"Accept": "application/json",
"Authorization": f"Bearer {get_bearer_token()}",
"x-fv-orgid": os.environ.get("FILEVINE_ORG_ID"),
"x-fv-userid": os.environ.get("FILEVINE_USER_ID"),
},
"params": {},
"method": "GET"
},
"examples/forms__newFileReview.json"
)
]
def generate_sample_request_response(request_config):
"""Generate a sample request/response pair using the provided request configuration."""
# Make the request
response = requests.request(**request_config)
# Create structured output
result = {
"sample_request": request_config,
"sample_response": {
"status_code": response.status_code,
"json": response.json() if response.content else None,
"xxx": str(response.content),
"headers": dict(response.headers)
}
}
return result
if __name__ == "__main__":
try:
# Ensure examples directory exists
os.makedirs("examples", exist_ok=True)
# Process each request/response pair
for request_config, output_path in REQUESTS:
# Regenerate bearer token for each request (tokens may expire)
request_config["headers"]["Authorization"] = f"Bearer {get_bearer_token()}"
result = generate_sample_request_response(request_config)
# Save pretty printed JSON to file
with open(output_path, 'w') as f:
json.dump(result, f, indent=2)
print(f"Generated {len(REQUESTS)} sample request/response pairs")
except Exception as e:
print(f"Error: {e}")