GetSession retrieves a specific agent session by ID.
(ctx context.Context, id string)
| 295 | |
| 296 | // GetSession retrieves a specific agent session by ID. |
| 297 | func (c *CAPIClient) GetSession(ctx context.Context, id string) (*Session, error) { |
| 298 | if id == "" { |
| 299 | return nil, fmt.Errorf("missing session ID") |
| 300 | } |
| 301 | |
| 302 | url := fmt.Sprintf("%s/agents/sessions/%s", c.capiBaseURL, url.PathEscape(id)) |
| 303 | |
| 304 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| 305 | if err != nil { |
| 306 | return nil, err |
| 307 | } |
| 308 | |
| 309 | res, err := c.httpClient.Do(req) |
| 310 | if err != nil { |
| 311 | return nil, err |
| 312 | } |
| 313 | |
| 314 | defer res.Body.Close() |
| 315 | if res.StatusCode != http.StatusOK { |
| 316 | if res.StatusCode == http.StatusNotFound { |
| 317 | return nil, ErrSessionNotFound |
| 318 | } |
| 319 | return nil, fmt.Errorf("failed to get session: %s", res.Status) |
| 320 | } |
| 321 | |
| 322 | var rawSession session |
| 323 | if err := json.NewDecoder(res.Body).Decode(&rawSession); err != nil { |
| 324 | return nil, fmt.Errorf("failed to decode session response: %w", err) |
| 325 | } |
| 326 | |
| 327 | sessions, err := c.hydrateSessionPullRequestsAndUsers([]session{rawSession}) |
| 328 | if err != nil { |
| 329 | return nil, fmt.Errorf("failed to fetch session resources: %w", err) |
| 330 | } |
| 331 | |
| 332 | return sessions[0], nil |
| 333 | } |
| 334 | |
| 335 | // GetSessionLogs retrieves logs of an agent session identified by ID. |
| 336 | func (c *CAPIClient) GetSessionLogs(ctx context.Context, id string) ([]byte, error) { |