diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-01-12 09:27:16 -1000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-01-12 09:27:16 -1000 |
| commit | 9fe0998436488537a8a2e8ffeefb0c4424b41c60 (patch) | |
| tree | ce877f04e60a187c2bd0e481e80298ec5e7cdf80 /internal/api/trello.go | |
Initial commit: Personal Consolidation Dashboard (Phase 1 Complete)
Implemented a unified web dashboard aggregating tasks, notes, and meal planning:
Core Features:
- Trello integration (PRIMARY feature - boards, cards, lists)
- Todoist integration (tasks and projects)
- Obsidian integration (20 most recent notes)
- PlanToEat integration (optional - 7-day meal planning)
- Mobile-responsive web UI with auto-refresh (5 min)
- SQLite caching with 5-minute TTL
- AI agent endpoint with Bearer token authentication
Technical Implementation:
- Go 1.21+ backend with chi router
- Interface-based API client design for testability
- Parallel data fetching with goroutines
- Graceful degradation (partial data on API failures)
- .env file loading with godotenv
- Comprehensive test coverage (9/9 tests passing)
Bug Fixes:
- Fixed .env file not being loaded at startup
- Fixed nil pointer dereference with optional API clients (typed nil interface gotcha)
Documentation:
- START_HERE.md - Quick 5-minute setup guide
- QUICKSTART.md - Fast track setup
- SETUP_GUIDE.md - Detailed step-by-step instructions
- PROJECT_SUMMARY.md - Complete project overview
- CLAUDE.md - Guide for Claude Code instances
- AI_AGENT_ACCESS.md - AI agent design document
- AI_AGENT_SETUP.md - Claude.ai integration guide
- TRELLO_AUTH_UPDATE.md - New Power-Up auth process
Statistics:
- Binary: 17MB
- Code: 2,667 lines
- Tests: 5 unit + 4 acceptance tests (all passing)
- Dependencies: chi, sqlite3, godotenv
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/api/trello.go')
| -rw-r--r-- | internal/api/trello.go | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/internal/api/trello.go b/internal/api/trello.go new file mode 100644 index 0000000..899f6df --- /dev/null +++ b/internal/api/trello.go @@ -0,0 +1,219 @@ +package api + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "task-dashboard/internal/models" +) + +const ( + trelloBaseURL = "https://api.trello.com/1" +) + +// TrelloClient handles interactions with the Trello API +type TrelloClient struct { + apiKey string + token string + httpClient *http.Client +} + +// NewTrelloClient creates a new Trello API client +func NewTrelloClient(apiKey, token string) *TrelloClient { + return &TrelloClient{ + apiKey: apiKey, + token: token, + httpClient: &http.Client{ + Timeout: 30 * time.Second, + }, + } +} + +// trelloBoardResponse represents a board from the API +type trelloBoardResponse struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// trelloCardResponse represents a card from the API +type trelloCardResponse struct { + ID string `json:"id"` + Name string `json:"name"` + IDList string `json:"idList"` + Due *string `json:"due"` + URL string `json:"url"` + Desc string `json:"desc"` + IDBoard string `json:"idBoard"` +} + +// trelloListResponse represents a list from the API +type trelloListResponse struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// GetBoards fetches all boards for the authenticated user +func (c *TrelloClient) GetBoards(ctx context.Context) ([]models.Board, error) { + url := fmt.Sprintf("%s/members/me/boards?key=%s&token=%s", trelloBaseURL, c.apiKey, c.token) + + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch boards: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("trello API error (status %d): %s", resp.StatusCode, string(body)) + } + + var apiBoards []trelloBoardResponse + if err := json.NewDecoder(resp.Body).Decode(&apiBoards); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + // Convert to our model + boards := make([]models.Board, 0, len(apiBoards)) + for _, apiBoard := range apiBoards { + board := models.Board{ + ID: apiBoard.ID, + Name: apiBoard.Name, + Cards: []models.Card{}, // Will be populated by GetCards + } + boards = append(boards, board) + } + + return boards, nil +} + +// GetCards fetches all cards for a specific board +func (c *TrelloClient) GetCards(ctx context.Context, boardID string) ([]models.Card, error) { + url := fmt.Sprintf("%s/boards/%s/cards?key=%s&token=%s", trelloBaseURL, boardID, c.apiKey, c.token) + + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch cards: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("trello API error (status %d): %s", resp.StatusCode, string(body)) + } + + var apiCards []trelloCardResponse + if err := json.NewDecoder(resp.Body).Decode(&apiCards); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + // Fetch lists to get list names + lists, err := c.getLists(ctx, boardID) + if err != nil { + // If we can't get lists, continue with empty list names + lists = make(map[string]string) + } + + // Convert to our model + cards := make([]models.Card, 0, len(apiCards)) + for _, apiCard := range apiCards { + card := models.Card{ + ID: apiCard.ID, + Name: apiCard.Name, + ListID: apiCard.IDList, + ListName: lists[apiCard.IDList], + URL: apiCard.URL, + } + + // Parse due date if present + if apiCard.Due != nil && *apiCard.Due != "" { + dueDate, err := time.Parse(time.RFC3339, *apiCard.Due) + if err == nil { + card.DueDate = &dueDate + } + } + + cards = append(cards, card) + } + + return cards, nil +} + +// getLists fetches lists for a board and returns a map of list ID to name +func (c *TrelloClient) getLists(ctx context.Context, boardID string) (map[string]string, error) { + url := fmt.Sprintf("%s/boards/%s/lists?key=%s&token=%s", trelloBaseURL, boardID, c.apiKey, c.token) + + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch lists: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("trello API error (status %d): %s", resp.StatusCode, string(body)) + } + + var apiLists []trelloListResponse + if err := json.NewDecoder(resp.Body).Decode(&apiLists); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + // Convert to map + lists := make(map[string]string, len(apiLists)) + for _, list := range apiLists { + lists[list.ID] = list.Name + } + + return lists, nil +} + +// GetBoardsWithCards fetches all boards and their cards in one call +func (c *TrelloClient) GetBoardsWithCards(ctx context.Context) ([]models.Board, error) { + boards, err := c.GetBoards(ctx) + if err != nil { + return nil, err + } + + // Fetch cards for each board + for i := range boards { + cards, err := c.GetCards(ctx, boards[i].ID) + if err != nil { + // Log error but continue with other boards + continue + } + boards[i].Cards = cards + } + + return boards, nil +} + +// CreateCard creates a new card (for Phase 2) +func (c *TrelloClient) CreateCard(ctx context.Context, listID, name, description string, dueDate *time.Time) (*models.Card, error) { + // This will be implemented in Phase 2 + return nil, fmt.Errorf("not implemented yet") +} + +// UpdateCard updates a card (for Phase 2) +func (c *TrelloClient) UpdateCard(ctx context.Context, cardID string, updates map[string]interface{}) error { + // This will be implemented in Phase 2 + return fmt.Errorf("not implemented yet") +} |
