### Install pycronofy via pip Source: https://github.com/cronofy/pycronofy/blob/master/README.md Install the pycronofy library using pip. This is the recommended method for most users. ```bash pip install pycronofy ``` -------------------------------- ### Install pycronofy via setup.py Source: https://github.com/cronofy/pycronofy/blob/master/README.md Install pycronofy and its dependencies for testing using setup.py. This method is useful for development or contributing to the library. ```bash pip install -r requirements.txt python setup.py install ``` -------------------------------- ### Get Account and User Info Source: https://github.com/cronofy/pycronofy/blob/master/README.md Retrieve account details and user information using the account() and userinfo() methods. ```python # For account details cronofy.account() # For userinfo cronofy.userinfo() ``` -------------------------------- ### Client Initialization with Data Center Specification Source: https://github.com/cronofy/pycronofy/blob/master/README.md Initialize the pycronofy client, specifying a particular data center. This is useful for regional API endpoints. ```python cronofy = pycronofy.Client( client_id=YOUR_CLIENT_ID, client_secret=YOUR_CLIENT_SECRET, access_token=auth['access_token'], refresh_token=auth['refresh_token'], token_expiration=auth['token_expiration'], data_center='us' ) ``` -------------------------------- ### Create and Manage Notification Channels Source: https://github.com/cronofy/pycronofy/blob/master/README.md Set up notification channels to receive push notifications for calendar or profile changes. This requires an application and OAuth tokens, not a personal access token. ```python channel = cronofy.create_notification_channel('http://example.com', calendar_ids=(cal['calendar_id'],) ) # list channels cronofy.list_notification_channels() cronofy.close_notification_channel(channel['channel_id']) ``` -------------------------------- ### List Profiles Source: https://github.com/cronofy/pycronofy/blob/master/README.md Iterate through and print all available user profiles associated with the authenticated account. ```python for profile in cronofy.list_profiles(): print(profile) ``` -------------------------------- ### Initial OAuth Authorization Source: https://github.com/cronofy/pycronofy/blob/master/README.md Initiate the OAuth authorization flow to obtain user consent and generate authorization tokens. This requires your client ID and secret. ```python import pycronofy # Initial authorization cronofy = pycronofy.Client(client_id=YOUR_CLIENT_ID, client_secret=YOUR_CLIENT_SECRET) url = cronofy.user_auth_link('http://yourwebsite.com') print('Go to this url in your browser, and paste the code below') code = input('Paste Code Here: ') # raw_input() for python 2. auth = cronofy.get_authorization_from_code(code) # get_authorization_from_code updates the state of the cronofy client. It also returns # the authorization tokens (and expiration) in case you need to store them. # If that is the case, you will want to initiate the client as follows: cronofy = pycronofy.Client( client_id=YOUR_CLIENT_ID, client_secret=YOUR_CLIENT_SECRET, access_token=auth['access_token'], refresh_token=auth['refresh_token'], token_expiration=auth['token_expiration'] ) ``` -------------------------------- ### Client Initialization with Personal Access Token Source: https://github.com/cronofy/pycronofy/blob/master/README.md Initialize the pycronofy client using a personal access token. This is suitable for testing or direct API access without a full OAuth flow. ```python cronofy = pycronofy.Client(access_token=YOUR_TOKEN) # Using a personal token for testing. ``` -------------------------------- ### Run Unit Tests Source: https://github.com/cronofy/pycronofy/blob/master/README.md Execute the unit tests for the PyCronofy library using pytest, with code coverage reporting enabled. ```bash py.test pycronofy --cov=pycronofy ``` -------------------------------- ### List Calendars Source: https://github.com/cronofy/pycronofy/blob/master/README.md Iterate through and print all calendars accessible by the authenticated user. ```python for calendar in cronofy.list_calendars(): print(calendar) ``` -------------------------------- ### Create Event with Local Timezone Source: https://github.com/cronofy/pycronofy/blob/master/README.md Create an event with a specified local timezone. Ensure datetime objects or strings are in UTC. A unique event ID is required for retrieval. ```python import datetime import uuid # Example timezone id timezone_id = 'US/Eastern' event_id = 'example-%s' % uuid.uuid4() event = { 'event_id': event_id, 'summary': 'Test Event', # The event title 'description': 'Discuss proactive strategies for a reactive world.', 'start': datetime.datetime.utcnow(), 'end': (datetime.datetime.utcnow() + datetime.timedelta(hours=1)), 'tzid': timezone_id, 'location': { 'description': 'My Desk!', }, } cronofy.upsert_event(calendar_id=cal['calendar_id'], event=event) ``` -------------------------------- ### Read Events with Pagination Source: https://github.com/cronofy/pycronofy/blob/master/README.md Read events from specified calendars within a date range, supporting timezone specification and automatic pagination. Events can be iterated or accessed as a list. ```python import datetime # Example timezone id timezone_id = 'US/Eastern' # Dates/Datetimes must be in UTC # For from_date, to_date, start, end, you can pass in a datetime object # or an ISO 8601 datetime string. # For example: example_datetime_string = '2016-01-06T16:49:37Z' #ISO 8601. # To set to local time, pass in the tzid argument. from_date = (datetime.datetime.utcnow() - datetime.timedelta(days=2)) to_date = datetime.datetime.utcnow() events = cronofy.read_events(calendar_ids=(YOUR_CAL_ID,), from_date=from_date, to_date=to_date, tzid=timezone_id # This argument sets the timezone to local, vs utc. ) # Automatic pagination through an iterator for event in events: print('%s (From %s to %s, %i attending)' % (event['summary'], event['start'], event['end'], len(event['attendees']))) # Treat the events as a list (holding the current page only). print(events[2]) print(len(events)) # Alternatively grab the actual list object for the current page: page = events.current_page() print(page[1]) # Manually move to the next page: events.fetch_next_page() # Access the raw data returned by the request: events.json() # Retrieve all data in a list: # Option 1: all_events = [event for event in cronofy.read_events(calendar_ids=(YOUR_CAL_ID,), from_date=from_date, to_date=to_date, tzid=timezone_id) ] # Option 2: all_events = cronofy.read_events(calendar_ids=(YOUR_CAL_ID,), from_date=from_date, to_date=to_date, tzid=timezone_id ).all() ``` -------------------------------- ### Read Free/Busy Blocks Source: https://github.com/cronofy/pycronofy/blob/master/README.md Retrieve free/busy information for specified calendars within a date range. This method is similar to reading events but only returns availability status. ```python from_date = (datetime.datetime.utcnow() - datetime.timedelta(days=2)) to_date = datetime.datetime.utcnow() free_busy_blocks = cronofy.read_free_busy(calendar_ids=(YOUR_CAL_ID,), from_date=from_date, to_date=to_date ) for block in free_busy_blocks: print(block) ``` -------------------------------- ### Set Request Hook for Debugging Source: https://github.com/cronofy/pycronofy/blob/master/README.md Utilize the 'set_request_hook' argument to integrate with requests' event hooks for debugging. The provided callback function can inspect or modify request/response data. ```python # pycronofy provides a "set_request_hook" argument to make use of requests' event hooks. def on_request(response, *args, **kwargs): """ "If the callback function returns a value, it is assumed that it is to replace the data that was passed in. If the function doesn’t return anything, nothing else is effected." http://docs.python-requests.org/en/latest/user/advanced/#event-hooks """ print('%s %s' % (response.request.method, response.url)) print(kwargs) pycronofy.set_request_hook(on_request) ``` -------------------------------- ### Check Authorization Expiry Source: https://github.com/cronofy/pycronofy/blob/master/README.md Verify if the current OAuth authorization token has expired using the is_authorization_expired method. ```python cronofy.is_authorization_expired() ``` -------------------------------- ### Handle PyCronofy Request Errors Source: https://github.com/cronofy/pycronofy/blob/master/README.md Catch and inspect PyCronofyRequestError exceptions to access details about failed API requests, including response reason, body, request method, headers, URL, and post data. ```python try: cronofy.upsert(event(calendar_id='ABC', event=malformed_event)) except pycronofy.exceptions.PyCronofyRequestError as e: print(e.response.reason) # Error Message print(e.response.text) # Response Body print(e.request.method) # HTTP Method print(e.request.headers) # Headers print(e.request.url) # URL and Get Data print(e.request.body) # Post Data ``` -------------------------------- ### Validate PyCronofy API Calls Source: https://github.com/cronofy/pycronofy/blob/master/README.md Validate API calls for authentication, required arguments, and datetime/date string format. A PyCronofyValidationError is raised on error, providing details about the message, fields, and method. ```python try: cronofy.validate('create_notification_channel', 'http://example.com', calendar_ids=(cal['calendar_id'],) ) except pycronofy.exceptions.PyCronofyValidationError as e: print(e.message) print(e.fields) print(e.method) ``` -------------------------------- ### Refresh OAuth Tokens Source: https://github.com/cronofy/pycronofy/blob/master/README.md Refresh expired OAuth tokens using the refresh_authorization method. This allows continued access to the API without re-authenticating the user. ```python auth = cronofy.refresh_authorization() ``` -------------------------------- ### Revoke OAuth Tokens Source: https://github.com/cronofy/pycronofy/blob/master/README.md Revoke the current OAuth authorization tokens using the revoke_authorization method. This action invalidates the tokens and logs the user out. ```python cronofy.revoke_authorization() ``` -------------------------------- ### Delete Event by ID Source: https://github.com/cronofy/pycronofy/blob/master/README.md Delete a specific event from a calendar using its known event ID. ```python # Delete using known event id cronofy.delete_event(calendar_id=cal['calendar_id'], event_id=test_event_id) ``` -------------------------------- ### Delete All Managed Events Source: https://github.com/cronofy/pycronofy/blob/master/README.md Delete all events managed by Cronofy across all user calendars, or for specified calendars. ```python # Delete all managed events (events inserted via Cronofy) for all user calendars. cronofy.delete_all_events() # Deletes all managed events for the specified user calendars. cronofy.delete_all_events(calendar_ids=(CAL_ID,)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.