chore(grequests): update grequests to v2
This commit is contained in:
parent
a88da88a47
commit
00bf1820f1
5 changed files with 18 additions and 13 deletions
2
go.mod
2
go.mod
|
@ -7,7 +7,7 @@ require (
|
|||
github.com/go-chi/chi/v5 v5.2.2
|
||||
github.com/go-chi/render v1.0.3
|
||||
github.com/hashicorp/go-memdb v1.3.5
|
||||
github.com/levigross/grequests v0.0.0-20250606031859-3f3c12e4e704
|
||||
github.com/levigross/grequests/v2 v2.0.0-20250606223341-05add15eea25
|
||||
github.com/praserx/ipconv v1.2.2
|
||||
github.com/prometheus/client_golang v1.22.0
|
||||
github.com/schollz/progressbar/v3 v3.18.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -39,8 +39,8 @@ github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zt
|
|||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/levigross/grequests v0.0.0-20250606031859-3f3c12e4e704 h1:iH9CoV+Eoc3eeaCTt8qCU8/Qod7HdRKHlytTmOCIutw=
|
||||
github.com/levigross/grequests v0.0.0-20250606031859-3f3c12e4e704/go.mod h1:qnB03x2PLQFGb5PZv0m87zJrVuvrHUuvZMlHTUuZVnM=
|
||||
github.com/levigross/grequests/v2 v2.0.0-20250606223341-05add15eea25 h1:qB9QdTaOOXwvXSA3zIvT0LiFsb1klMgGowVgsaXUtPA=
|
||||
github.com/levigross/grequests/v2 v2.0.0-20250606223341-05add15eea25/go.mod h1:MvmS5LAuNHiqSBKgGAjsayaaBKK5YXWRLSwwTMdTEMw=
|
||||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
package downloader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
req "github.com/levigross/grequests"
|
||||
req "github.com/levigross/grequests/v2"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
|
@ -29,8 +30,8 @@ func NewContext(filename, link string) *Context {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Context) Download() error {
|
||||
resp, err := req.Head(c.Link, nil)
|
||||
func (c *Context) Download(ctx context.Context) error {
|
||||
resp, err := req.Head(ctx, c.Link)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no such host") {
|
||||
return ErrInvalidURL
|
||||
|
@ -54,7 +55,7 @@ func (c *Context) Download() error {
|
|||
return errors.New(resp.RawResponse.Status)
|
||||
}
|
||||
|
||||
resp, err = req.Get(c.Link, nil)
|
||||
resp, err = req.Get(ctx, c.Link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package downloader_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
@ -37,7 +38,7 @@ func TestDownload(t *testing.T) {
|
|||
ctx := downloader.NewContext(filename, server.URL)
|
||||
defer os.Remove(filename)
|
||||
|
||||
err := ctx.Download()
|
||||
err := ctx.Download(context.Background())
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
@ -49,7 +50,7 @@ func TestDownload(t *testing.T) {
|
|||
|
||||
t.Run("InvalidURL", func(t *testing.T) {
|
||||
ctx := downloader.NewContext("test_invalid_url.txt", "http://invalid.url")
|
||||
err := ctx.Download()
|
||||
err := ctx.Download(context.Background())
|
||||
|
||||
if err == nil || err.Error() != "invalid url" {
|
||||
t.Errorf("expected invalid url error, got %v", err)
|
||||
|
@ -63,7 +64,8 @@ func TestDownload(t *testing.T) {
|
|||
"https://self-signed.badssl.com/", // Example URL that can be used
|
||||
)
|
||||
|
||||
if err := ctx.Download(); err == nil || err.Error() != "certificate from unknown authority" {
|
||||
err := ctx.Download(context.Background())
|
||||
if err == nil || err.Error() != "certificate from unknown authority" {
|
||||
t.Errorf("expected certificate from unknown authority error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
@ -77,7 +79,7 @@ func TestDownload(t *testing.T) {
|
|||
|
||||
ctx := downloader.NewContext("test_not_found.txt", server.URL)
|
||||
|
||||
if err := ctx.Download(); err == nil || err.Error() != "invalid url" {
|
||||
if err := ctx.Download(context.Background()); err == nil || err.Error() != "invalid url" {
|
||||
t.Errorf("expected invalid url error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
@ -91,7 +93,8 @@ func TestDownload(t *testing.T) {
|
|||
|
||||
ctx := downloader.NewContext("test_restricted_access.txt", server.URL)
|
||||
|
||||
if err := ctx.Download(); err == nil || err.Error() != "restricted access (credentials required)" {
|
||||
err := ctx.Download(context.Background())
|
||||
if err == nil || err.Error() != "restricted access (credentials required)" {
|
||||
t.Errorf("expected restricted access error, got %v", err)
|
||||
}
|
||||
})
|
||||
|
|
3
main.go
3
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -37,7 +38,7 @@ func main() {
|
|||
|
||||
ctx := downloader.NewContext(config.DataFile, config.DataURL)
|
||||
if !ctx.FileExists() {
|
||||
if downloadErr := ctx.Download(); downloadErr != nil {
|
||||
if downloadErr := ctx.Download(context.Background()); downloadErr != nil {
|
||||
panic(err)
|
||||
}
|
||||
slog.Info("saved file", "path", ctx.Filename)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue