Back

Step by Step Guide to Building a SafetyCulture API Integration in Python

Aug 17, 20245 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • A Python environment set up (I know you've got this!)
  • A SafetyCulture account with an API token (if you don't have one, go grab it real quick)

Installation

First things first, let's get that SDK installed:

pip install safetyculture-sdk-python

Easy peasy, right?

Authentication

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.

Basic API Operations

Let's start with some bread-and-butter operations:

Fetching Inspections

inspections = client.inspections.search() for inspection in inspections: print(inspection.audit_id)

Retrieving Inspection Details

inspection_id = 'your_inspection_id' details = client.inspections.get(inspection_id) print(details)

Downloading Inspection Reports

report = client.inspections.get_report(inspection_id, 'pdf') with open('report.pdf', 'wb') as f: f.write(report)

Advanced Operations

Ready to level up? Let's tackle some advanced stuff:

Creating and Updating Templates

template_data = {...} # Your template JSON here new_template = client.templates.create(template_data)

Managing Users and Groups

users = client.users.list() groups = client.groups.list()

Scheduling Inspections

schedule_data = {...} # Your schedule details here client.schedules.create(schedule_data)

Error Handling and Best Practices

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

Example Use Case: Inspection Dashboard

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()

Testing and Debugging

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)

Conclusion

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! 🚀🛡️