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

Method OpenStream

pkg/cmd/event/client/client.go:225–253  ·  view source on GitHub ↗
(ctx context.Context, id string)

Source from the content-addressed store, hash-verified

223}
224
225func (c *eventClient) OpenStream(ctx context.Context, id string) (io.ReadCloser, error) {
226 if !SupportedHost(c.host) {
227 return nil, ErrUnsupportedHost
228 }
229
230 path, err := safeurl.JoinPath("event-subscriptions", id, "events")
231 if err != nil {
232 return nil, err
233 }
234
235 resp, err := c.api.RequestWithContext(ctx, c.host, http.MethodGet, path.String(), nil, api.WithHeader("Accept", "text/event-stream"))
236 if err != nil {
237 if httpErr, ok := errors.AsType[api.HTTPError](err); ok {
238 switch {
239 case httpErr.StatusCode == http.StatusNotFound:
240 return nil, fmt.Errorf("%w: %w", ErrSubscriptionNotFound, err)
241 case httpErr.StatusCode == http.StatusForbidden:
242 return nil, forbiddenError(httpErr, err)
243 }
244 }
245 return nil, err
246 }
247 mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
248 if mediaType != "text/event-stream" {
249 resp.Body.Close()
250 return nil, fmt.Errorf("unexpected event stream content type %q", resp.Header.Get("Content-Type"))
251 }
252 return resp.Body, nil
253}
254
255// forbiddenError tells a 403 response for an account without preview access apart from other denials. It keeps the
256// HTTP error wrapped, so callers treat it as final rather than retrying it.

Callers

nothing calls this directly

Calls 9

JoinPathFunction · 0.92
WithHeaderFunction · 0.92
SupportedHostFunction · 0.85
forbiddenErrorFunction · 0.85
RequestWithContextMethod · 0.80
StringMethod · 0.65
ErrorfMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected