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

View file

@ -1,10 +1,12 @@
package font
import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
)
type release struct {
@ -21,7 +23,9 @@ func (a Asset) FilterValue() string { return a.Name }
func Nerds() ([]*Asset, error) {
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 {
return nil, err
}