111 lines
3.7 KiB
Python
111 lines
3.7 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 = 15905506
|
|
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}/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/43125866",
|
|
"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"
|
|
)
|
|
]
|
|
|
|
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,
|
|
"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}") |