Skip to content

Instantly share code, notes, and snippets.

@aculich
Created April 1, 2025 18:47
Show Gist options
  • Save aculich/b0453e59139787f634d62c4bff6b6c9b to your computer and use it in GitHub Desktop.
Save aculich/b0453e59139787f634d62c4bff6b6c9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import requests
from datetime import datetime
import json
class LegalServerAPI:
def __init__(self, site_name, api_key):
"""Initialize the API client
Args:
site_name (str): Your LegalServer site name (e.g., 'mysite-demo')
api_key (str): Your API key
"""
self.base_url = f"https://{site_name}.legalserver.org"
self.api_key = api_key
self.headers = {
'Authorization': f'Bearer {api_key}',
'Accept': 'application/json',
'Content-Type': 'application/json'
}
def test_connection(self):
"""Test the API connection by getting the list of available processes"""
url = f"{self.base_url}/api/v2/processes"
try:
response = requests.get(url, headers=self.headers)
print("\nConnection Test Results:")
print(f"Status Code: {response.status_code}")
if response.status_code == 401:
print("\nAuthentication Error:")
print("- Your API key may be invalid or not properly configured")
print("- Check that your Bearer token is correct")
print("- Ensure you have basic API access permissions")
return False
if response.status_code == 403:
print("\nPermission Denied:")
print("- You may not have the required API permissions")
print("- Contact your LegalServer System Administrator to:")
print(" 1. Set up API Service Account Permission Groups")
print(" 2. Create an API Service Account")
print(" 3. Grant necessary API permissions to your user")
print("\nAlternatively, contact LegalServer Support for assistance with API access")
return False
response.raise_for_status()
print(f"Rate Limit Limit: {response.headers.get('x-rate-limit-limit')}")
print(f"Rate Limit Remaining: {response.headers.get('x-rate-limit-remaining')}")
print(f"Rate Limit Reset: {response.headers.get('x-rate-limit-reset')}")
if response.headers.get('x-rate-limit-remaining'):
remaining = int(response.headers.get('x-rate-limit-remaining'))
if remaining < 10:
print(f"\nWarning: Only {remaining} API calls remaining before rate limit!")
print("\nAvailable Processes:")
for process in response.json():
print(f"- {process['name']} (Type: {process['type']}, ID: {process['id']})")
return True
except requests.exceptions.RequestException as e:
print(f"\nError testing connection: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f"Response Status Code: {e.response.status_code}")
print(f"Response Body: {e.response.text}")
if e.response.status_code == 404:
print("\nEndpoint Not Found:")
print("- Verify your site name is correct")
print("- Check that the API endpoint is available")
print("- Ensure you're using the correct API version")
return False
def main():
# Get credentials from environment variables
site_name = os.getenv('LEGALSERVER_SITE')
api_key = os.getenv('LEGALSERVER_API_KEY')
if not site_name or not api_key:
print("Error: Please set LEGALSERVER_SITE and LEGALSERVER_API_KEY environment variables")
print("\nExample:")
print("export LEGALSERVER_SITE='mysite-demo'")
print("export LEGALSERVER_API_KEY='your-api-key-here'")
return
# Initialize API client
api = LegalServerAPI(site_name, api_key)
# Test connection
print(f"\nTesting connection to {api.base_url}...")
api.test_connection()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment