MCPcopy Create free account
hub / github.com/cli/cli / GetCodespace

Method GetCodespace

internal/codespaces/api/api.go:539–582  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

537// If the codespace is not found, an error is returned.
538// If includeConnection is true, it will return the connection information for the codespace.
539func (a *API) GetCodespace(ctx context.Context, codespaceName string, includeConnection bool) (*Codespace, error) {
540 resp, err := a.withRetry(func() (*http.Response, error) {
541 u, err := safeurl.JoinPathWithHostPrefix(a.githubAPI, "user", "codespaces", codespaceName)
542 if err != nil {
543 return nil, err
544 }
545 req, err := http.NewRequest(
546 http.MethodGet,
547 u.String(),
548 nil,
549 )
550 if err != nil {
551 return nil, fmt.Errorf("error creating request: %w", err)
552 }
553 if includeConnection {
554 q := req.URL.Query()
555 q.Add("internal", "true")
556 q.Add("refresh", "true")
557 req.URL.RawQuery = q.Encode()
558 }
559 a.setHeaders(req)
560 return a.do(ctx, req, "/user/codespaces/*")
561 })
562 if err != nil {
563 return nil, fmt.Errorf("error making request: %w", err)
564 }
565 defer resp.Body.Close()
566
567 if resp.StatusCode != http.StatusOK {
568 return nil, api.HandleHTTPError(resp)
569 }
570
571 b, err := io.ReadAll(resp.Body)
572 if err != nil {
573 return nil, fmt.Errorf("error reading response body: %w", err)
574 }
575
576 var response Codespace
577 if err := json.Unmarshal(b, &response); err != nil {
578 return nil, fmt.Errorf("error unmarshalling response: %w", err)
579 }
580
581 return &response, nil
582}
583
584// StartCodespace starts a codespace for the user.
585// If the codespace is already running, the returned error from the API is ignored.

Callers 3

CreateCodespaceMethod · 0.95
TestRetriesFunction · 0.95

Calls 10

withRetryMethod · 0.95
setHeadersMethod · 0.95
doMethod · 0.95
JoinPathWithHostPrefixFunction · 0.92
HandleHTTPErrorFunction · 0.92
StringMethod · 0.65
ErrorfMethod · 0.65
QueryMethod · 0.65
AddMethod · 0.65
CloseMethod · 0.65

Tested by 1

TestRetriesFunction · 0.76