chore(font): execute HTTP requests with context

This commit is contained in:
Jan De Dobbeleer 2022-08-03 08:58:37 +02:00 committed by Jan De Dobbeleer
parent 47265b1bc1
commit d678265607
2 changed files with 15 additions and 4 deletions

View file

@ -4,11 +4,13 @@ package font
// component library. // component library.
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"time"
) )
func Download(fontPath string) ([]byte, error) { func Download(fontPath string) ([]byte, error) {
@ -35,9 +37,14 @@ func isZipFile(data []byte) bool {
} }
func getRemoteFile(location string) (data []byte, err error) { func getRemoteFile(location string) (data []byte, err error) {
var client = http.Client{} client := &http.Client{}
ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(60))
resp, err := client.Get(location) defer cancelF()
req, err := http.NewRequestWithContext(ctx, "GET", location, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil { if err != nil {
return return
} }

View file

@ -1,10 +1,12 @@
package font package font
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"net/http" "net/http"
"strings" "strings"
"time"
) )
type release struct { type release struct {
@ -21,7 +23,9 @@ func (a Asset) FilterValue() string { return a.Name }
func Nerds() ([]*Asset, error) { func Nerds() ([]*Asset, error) {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil) ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(20))
defer cancelF()
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }