Hey there, fellow developer! Ready to dive into the world of SafetyCulture API integration? You're in for a treat. We'll be using the safetyculture-sdk-python
package to make our lives easier. This powerful API opens up a world of possibilities for automating safety inspections, managing templates, and more. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get that SDK installed:
pip install safetyculture-sdk-python
Easy peasy, right?
Now, let's authenticate and get that client up and running:
from safetyculture_sdk_python import SafetyCulture api_token = 'your_api_token_here' client = SafetyCulture(api_token)
Boom! You're in.
Let's start with some bread-and-butter operations:
inspections = client.inspections.search() for inspection in inspections: print(inspection.audit_id)
inspection_id = 'your_inspection_id' details = client.inspections.get(inspection_id) print(details)
report = client.inspections.get_report(inspection_id, 'pdf') with open('report.pdf', 'wb') as f: f.write(report)
Ready to level up? Let's tackle some advanced stuff:
template_data = {...} # Your template JSON here new_template = client.templates.create(template_data)
users = client.users.list() groups = client.groups.list()
schedule_data = {...} # Your schedule details here client.schedules.create(schedule_data)
Don't forget to handle those pesky rate limits:
import time def api_call_with_retry(func, max_retries=3): for attempt in range(max_retries): try: return func() except RateLimitError: if attempt < max_retries - 1: time.sleep(2 ** attempt) else: raise
Let's put it all together with a simple dashboard:
def get_recent_inspections(): inspections = client.inspections.search(limit=10) return [{'id': i.audit_id, 'title': i.audit_data['audit_name']} for i in inspections] def main(): recent = get_recent_inspections() print("Recent Inspections:") for inspection in recent: print(f"- {inspection['title']} (ID: {inspection['id']})") if __name__ == '__main__': main()
Always test your code! Here's a quick unit test example:
import unittest from unittest.mock import patch class TestSafetyCultureIntegration(unittest.TestCase): @patch('safetyculture_sdk_python.SafetyCulture') def test_get_recent_inspections(self, mock_client): mock_client.inspections.search.return_value = [...] # Mock data here result = get_recent_inspections() self.assertEqual(len(result), 10)
And there you have it! You're now equipped to build some awesome SafetyCulture integrations. Remember, the API documentation is your best friend for diving deeper. Now go forth and code something amazing!
Happy coding, and stay safe out there! 🚀🛡️