GetCodespace returns the user codespace based on the provided name. If the codespace is not found, an error is returned. If includeConnection is true, it will return the connection information for the codespace.
(ctx context.Context, codespaceName string, includeConnection bool)
| 495 | // If the codespace is not found, an error is returned. |
| 496 | // If includeConnection is true, it will return the connection information for the codespace. |
| 497 | func (a *API) GetCodespace(ctx context.Context, codespaceName string, includeConnection bool) (*Codespace, error) { |
| 498 | resp, err := a.withRetry(func() (*http.Response, error) { |
| 499 | req, err := http.NewRequest( |
| 500 | http.MethodGet, |
| 501 | a.githubAPI+"/user/codespaces/"+codespaceName, |
| 502 | nil, |
| 503 | ) |
| 504 | if err != nil { |
| 505 | return nil, fmt.Errorf("error creating request: %w", err) |
| 506 | } |
| 507 | if includeConnection { |
| 508 | q := req.URL.Query() |
| 509 | q.Add("internal", "true") |
| 510 | q.Add("refresh", "true") |
| 511 | req.URL.RawQuery = q.Encode() |
| 512 | } |
| 513 | a.setHeaders(req) |
| 514 | return a.do(ctx, req, "/user/codespaces/*") |
| 515 | }) |
| 516 | if err != nil { |
| 517 | return nil, fmt.Errorf("error making request: %w", err) |
| 518 | } |
| 519 | defer resp.Body.Close() |
| 520 | |
| 521 | if resp.StatusCode != http.StatusOK { |
| 522 | return nil, api.HandleHTTPError(resp) |
| 523 | } |
| 524 | |
| 525 | b, err := io.ReadAll(resp.Body) |
| 526 | if err != nil { |
| 527 | return nil, fmt.Errorf("error reading response body: %w", err) |
| 528 | } |
| 529 | |
| 530 | var response Codespace |
| 531 | if err := json.Unmarshal(b, &response); err != nil { |
| 532 | return nil, fmt.Errorf("error unmarshalling response: %w", err) |
| 533 | } |
| 534 | |
| 535 | return &response, nil |
| 536 | } |
| 537 | |
| 538 | // StartCodespace starts a codespace for the user. |
| 539 | // If the codespace is already running, the returned error from the API is ignored. |