@@ -66,12 +66,17 @@ func (c *Cobalt) Post(ctx context.Context, params PostRequest, headers ...string
6666 return nil , CobaltAPIError (* media )
6767 }
6868
69+ for i := range media .Picker {
70+ media .Picker [i ].client = c .client
71+ }
72+
6973 return media , nil
7074}
7175
7276// Stream is a helper utility that will return an [io.ReadCloser] using the [PostResponse.URL] from this media object
7377// The returned [io.ReadCloser] is the Body of [*http.Response] and must be closed when you are done with the stream.
7478// When the [PostResponse.Status] == [ResponseStatusPicker] it will stream the first item from the [PostResponse.Picker] array.
79+ // Use [PickerItem.Stream] instead to stream an specific item from the picker list.
7580func (m * PostResponse ) Stream (ctx context.Context ) (io.ReadCloser , error ) {
7681 if m .Status != ResponseStatusTunnel && m .Status != ResponseStatusRedirect && m .Status != ResponseStatusPicker {
7782 return nil , fmt .Errorf ("unstreamable response type %s" , m .Status )
@@ -81,6 +86,19 @@ func (m *PostResponse) Stream(ctx context.Context) (io.ReadCloser, error) {
8186 if m .Status == ResponseStatusPicker && len (m .Picker ) > 0 {
8287 url = m .Picker [0 ].URL
8388 }
89+ return stream (ctx , m .client , url )
90+ }
91+
92+ // Stream is a helper utility that will return an [io.ReadCloser] using the [PickerItem.URL] from this media object
93+ // The returned [io.ReadCloser] is the Body of [*http.Response] and must be closed when you are done with the stream.
94+ func (m PickerItem ) Stream (ctx context.Context ) (io.ReadCloser , error ) {
95+ return stream (ctx , m .client , m .URL )
96+ }
97+
98+ func stream (ctx context.Context , client * http.Client , url string ) (io.ReadCloser , error ) {
99+ if client == nil {
100+ client = http .DefaultClient
101+ }
84102 if len (url ) == 0 {
85103 return nil , fmt .Errorf ("url is empty, nothing to stream" )
86104 }
@@ -90,7 +108,7 @@ func (m *PostResponse) Stream(ctx context.Context) (io.ReadCloser, error) {
90108 return nil , err
91109 }
92110
93- resp , err := m . client .Do (req )
111+ resp , err := client .Do (req )
94112 if err != nil {
95113 return nil , err
96114 }
0 commit comments