0
0
forked from toolshed/abra
Files
.gitea
cli
cmd
pkg
app
autocomplete
catalogue
client
compose
config
container
context
dns
formatter
git
integration
jsontable
limit
lint
recipe
secret
server
service
ssh
test
upstream
web
client.go
web.go
scripts
tests
.dockerignore
.drone.yml
.envrc.sample
.gitignore
.goreleaser.yml
AUTHORS.md
Dockerfile
LICENSE
Makefile
README.md
go.mod
go.sum
renovate.json
abra/pkg/web/web.go

53 lines
1.0 KiB
Go

// Package web provides functionality for dealing with web operations.
package web
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
// Timeout is the time it takes before a web request bails out waiting for a
// request.
const Timeout = 10 * time.Second
// ReadJSON reads JSON and parses it into your chosen interface pointer
func ReadJSON(url string, target interface{}) error {
httpClient := NewHTTPRetryClient()
res, err := httpClient.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(target)
}
// GetFile downloads a file and saves it to a filepath
func GetFile(filepath string, url string) (err error) {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}