Hey there, fellow developer! Ready to dive into the world of TeamUp API integration? You're in for a treat. TeamUp's API is a powerful tool that lets you tap into their calendar functionality, and we're going to build something cool with it using Python. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get our environment ready:
pip install requests
Now, let's keep our API key safe:
import os API_KEY = os.environ.get('TEAMUP_API_KEY')
Pro tip: Use environment variables to keep your secrets... well, secret!
Let's start with a simple GET request to test the waters:
import requests BASE_URL = 'https://api.teamup.com' headers = { 'Teamup-Token': API_KEY } response = requests.get(f'{BASE_URL}/calendars', headers=headers) print(response.json())
If you see some calendar data, you're golden!
Now for the fun part. Let's play with some events:
def get_events(calendar_key): response = requests.get(f'{BASE_URL}/{calendar_key}/events', headers=headers) return response.json()
def create_event(calendar_key, event_data): response = requests.post(f'{BASE_URL}/{calendar_key}/events', headers=headers, json=event_data) return response.json()
def update_event(calendar_key, event_id, event_data): response = requests.put(f'{BASE_URL}/{calendar_key}/events/{event_id}', headers=headers, json=event_data) return response.json()
def delete_event(calendar_key, event_id): response = requests.delete(f'{BASE_URL}/{calendar_key}/events/{event_id}', headers=headers) return response.status_code == 204
TeamUp uses cursor-based pagination. Here's how to handle it:
def get_all_events(calendar_key): events = [] next_cursor = None while True: params = {'startCursor': next_cursor} if next_cursor else {} response = requests.get(f'{BASE_URL}/{calendar_key}/events', headers=headers, params=params) data = response.json() events.extend(data['events']) next_cursor = data.get('next_cursor') if not next_cursor: break return events
These are just special types of events. You can use the same endpoints, just with different data structures.
Always expect the unexpected:
def api_request(method, endpoint, **kwargs): try: response = requests.request(method, f'{BASE_URL}/{endpoint}', headers=headers, **kwargs) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"API request failed: {e}") return None
Implement exponential backoff for rate limits:
import time def retry_with_backoff(func, max_retries=5): for attempt in range(max_retries): try: return func() except requests.exceptions.RequestException: if attempt == max_retries - 1: raise time.sleep(2 ** attempt)
Cache frequently accessed data:
from functools import lru_cache @lru_cache(maxsize=100) def get_calendar_info(calendar_key): return api_request('GET', f'{calendar_key}')
Use batch operations when possible:
def batch_create_events(calendar_key, events): return api_request('POST', f'{calendar_key}/events', json={'events': events})
Always test your code! Here's a simple unit test example:
import unittest from unittest.mock import patch class TestTeamUpIntegration(unittest.TestCase): @patch('requests.get') def test_get_events(self, mock_get): mock_get.return_value.json.return_value = {'events': []} events = get_events('test_calendar') self.assertEqual(events, {'events': []})
And there you have it! You've just built a solid TeamUp API integration in Python. Remember, this is just the beginning. The API has a lot more to offer, so don't be afraid to explore and experiment.
Keep coding, keep learning, and most importantly, have fun with it!
Here's a quick example of how you might use this integration:
import schedule import time def check_upcoming_events(): events = get_events('your_calendar_key') # Filter for events in the next 24 hours upcoming = [e for e in events if is_within_24_hours(e['start_dt'])] if upcoming: send_notification(upcoming) schedule.every().hour.do(check_upcoming_events) while True: schedule.run_pending() time.sleep(1)
This script checks for upcoming events every hour and sends a notification. Pretty neat, huh?
Now go forth and build something awesome with TeamUp's API!