Skip to content

Commit e75e709

Browse files
committed
feat: add PickerItem.Stream function
1 parent 87d2663 commit e75e709

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

client.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
7580
func (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
}

examples/basic/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Basic Example
2+
3+
This example is a simple program that will fetch a media URL, and in case of multiple options,
4+
it will fetch the first element that is a video.
5+
6+
## Run
7+
8+
```bash
9+
go run main.go <your_url_here> > my_file.mp4
10+
```
11+

examples/basic/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"log"
7+
"os"
8+
9+
"github.com/andresperezl/gobalt/v2"
10+
)
11+
12+
func main() {
13+
if len(os.Args) < 2 {
14+
log.Fatal("Need to provide an URL as argument")
15+
}
16+
17+
client := gobalt.NewCobaltWithAPI("http://localhost:9000")
18+
resp, err := client.Post(context.Background(), gobalt.PostRequest{
19+
URL: os.Args[1],
20+
})
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
var stream io.ReadCloser
25+
26+
if resp.Status == gobalt.ResponseStatusPicker {
27+
for _, pi := range resp.Picker {
28+
if pi.Type == gobalt.PickerItemTypeVideo {
29+
stream, err = pi.Stream(context.Background())
30+
}
31+
}
32+
} else {
33+
stream, err = resp.Stream(context.Background())
34+
}
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
defer stream.Close()
39+
40+
if _, err := os.Stdout.ReadFrom(stream); err != nil {
41+
log.Fatal(err)
42+
}
43+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/andresperezl/gobalt/v2
22

3-
go 1.23.0
3+
go 1.24.3

types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ type PostResponse struct {
206206
}
207207

208208
type PickerItem struct {
209+
client *http.Client
210+
209211
Type PickerItemType `json:"type"`
210212
URL string `json:"url"`
211213
Thumb string `json:"thumb"`

0 commit comments

Comments
 (0)