External Service Providers
Relevant source files
The following files were used as context for generating this wiki page:
The External Service Provider system in Analog is designed to abstract interactions with third-party calendar and task services, such as Google and Microsoft. It provides a consistent interface for the application to fetch, create, update, and delete data from these external sources without needing to know the specific implementation details of each service's API. This architecture allows for a unified experience within the Analog application, regardless of the user's connected accounts.
This system is built around a set of interfaces (ICalendarProvider, ITaskProvider) that define a contract for all provider implementations. A central provider registry is used to instantiate and retrieve the correct provider based on the user's account type. This makes the system extensible, allowing new providers to be added in the future with minimal changes to the core application logic.
Sources: packages/api/src/providers/interfaces.ts, packages/api/src/providers/index.ts
Architecture Overview
The provider architecture is based on a factory and interface pattern. A central providers object maps provider types (e.g., google, microsoft) to their respective implementation classes. The getProvider function acts as a factory, taking an account object and returning an initialized instance of the corresponding provider.
This diagram illustrates the relationship between the provider interfaces and their concrete implementations for different services.
Provider Factory
The getProvider function is the entry point for accessing a provider. It takes a user's account, which includes an access token and provider type, and returns an initialized provider instance. This function ensures that each provider is instantiated with the necessary credentials to communicate with its respective external API.
The following flowchart shows the logic of the getProvider function.
The available providers are registered in a map:
| Provider Key | Class |
|---|---|
google | GoogleCalendarProvider |
microsoft | MicrosoftCalendarProvider |
Sources: packages/api/src/providers/index.ts:15-18
Provider Interfaces
The system defines two main interfaces to ensure consistency across different service integrations.
Sources: packages/api/src/providers/interfaces.ts
ICalendarProvider
This interface defines the standard methods for interacting with an external calendar service. All calendar providers, such as GoogleCalendarProvider and MicrosoftCalendarProvider, must implement these methods.
| Method | Description | Return Type |
|---|---|---|
getCalendars() | Retrieves all calendars associated with the account. | Promise<Calendar[]> |
getEvents(calendarId, opts) | Retrieves events from a specific calendar within a given time range. | Promise<Event[]> |
createEvent(calendarId, event) | Creates a new event in a specific calendar. | Promise<Event> |
updateEvent(calendarId, eventId, event) | Updates an existing event. | Promise<Event> |
deleteEvent(calendarId, eventId) | Deletes an event from a calendar. | Promise<void> |
Sources: packages/api/src/providers/interfaces.ts:38-70
ITaskProvider
This interface defines the standard methods for interacting with an external task or to-do list service.
| Method | Description | Return Type |
|---|---|---|
getTaskLists() | Retrieves all task lists associated with the account. | Promise<TaskList[]> |
getTasks(taskListId, opts) | Retrieves tasks from a specific list. | Promise<Task[]> |
createTask(taskListId, task) | Creates a new task in a specific list. | Promise<Task> |
updateTask(taskListId, taskId, task) | Updates an existing task. | Promise<Task> |
deleteTask(taskListId, taskId) | Deletes a task from a list. | Promise<void> |
Sources: packages/api/src/providers/interfaces.ts:72-101
Provider Implementations
Google Calendar Provider
The GoogleCalendarProvider handles all interactions with the Google Calendar API. It uses the @repo/google-calendar client library, which is a wrapper around the official Google Calendar API.
The provider is initialized with an access token, which is then used to instantiate the GoogleCalendar client. This client handles the underlying HTTP requests to Google's servers.
// packages/api/src/providers/calendars/google-calendar.ts:16-20
export class GoogleCalendarProvider implements ICalendarProvider {
private client: GoogleCalendar;
constructor(accessToken: string) {
this.client = new GoogleCalendar({ accessToken });
}
Sources: packages/api/src/providers/calendars/google-calendar.ts:16-20, packages/google-calendar/src/client.ts:182-186
The provider's methods, such as getEvents, call the corresponding methods on the GoogleCalendar client and then map the response data from Google's format to Analog's internal Event format. This mapping logic is crucial for abstracting the provider-specific data structures.
The following sequence diagram shows the flow for fetching calendar events from Google.
Microsoft Calendar Provider
The MicrosoftCalendarProvider integrates with Microsoft Outlook calendars via the Microsoft Graph API. It uses the @microsoft/microsoft-graph-client library for API communication.
Similar to the Google provider, it's initialized with an access token which is used to configure the Graph API client.
// packages/api/src/providers/calendars/microsoft-calendar.ts:15-21
export class MicrosoftCalendarProvider implements ICalendarProvider {
private client: Client;
constructor(accessToken: string) {
this.client = Client.init({
authProvider: (done) => done(null, accessToken),
});
}
Sources: packages/api/src/providers/calendars/microsoft-calendar.ts:15-21
The provider implements the ICalendarProvider interface by making calls to the Microsoft Graph API endpoints (e.g., /me/calendars, /me/calendars/{id}/events). It also contains mapping logic to transform the data from the Microsoft Graph format into Analog's internal Calendar and Event models.
The sequence diagram below illustrates fetching events from a Microsoft calendar.
OAuth Configuration
Integration with these external services requires OAuth 2.0 setup. The application needs client IDs and secrets for each provider, which are configured via environment variables.
| Environment Variable | Provider | Description |
|---|---|---|
GOOGLE_CLIENT_ID | The client ID from the Google Cloud Console. | |
GOOGLE_CLIENT_SECRET | The client secret from the Google Cloud Console. | |
MICROSOFT_CLIENT_ID | Microsoft | The Application (client) ID from the Azure Portal. |
MICROSOFT_CLIENT_SECRET | Microsoft | The client secret from the Azure Portal. |
The necessary API permissions (scopes) must also be enabled in the respective developer consoles. For example, Microsoft requires Calendars.Read, Calendars.ReadWrite, and User.Read.
Sources: README.md:52-82