nlink/nomad/source.go

115 lines
1.8 KiB
Go

package nomad
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"net/http"
"github.com/anaskhan96/soup"
)
// Source provides data for the various parsing functions.
type Source interface {
Fetch() (string, error)
FetchBytes() ([]byte, error)
ID() string
String() string
}
// URLSource fetches data from an HTTP resource.
type URLSource struct {
url string
}
func (u URLSource) FetchBytes() ([]byte, error) {
resp, err := http.Get(u.url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func (u URLSource) Fetch() (string, error) {
body, err := u.FetchBytes()
if err != nil {
return "", err
}
return string(body), nil
}
func (u URLSource) ID() string {
return u.url
}
func (u URLSource) String() string {
return u.url
}
func NewURLSource(url string) URLSource {
return URLSource{url: url}
}
type StringSource struct {
s string
}
func (s StringSource) Fetch() (string, error) {
return s.String(), nil
}
func (s StringSource) FetchBytes() ([]byte, error) {
return []byte(s.s), nil
}
func NewStringSource(s string) *StringSource {
return &StringSource{s: s}
}
func (s StringSource) ID() string {
h := sha256.Sum256([]byte(s.s))
return fmt.Sprintf("%s", h[:])
}
func (s StringSource) String() string {
return s.s
}
// SoupSource uses the soup package to fetch data.
type SoupSource struct {
url string
}
func (s SoupSource) Fetch() (string, error) {
return soup.Get(s.url)
}
func (s SoupSource) FetchBytes() ([]byte, error) {
body, err := s.Fetch()
if err != nil {
return nil, err
}
return []byte(body), nil
}
func (s SoupSource) ID() string {
return s.url
}
func (s SoupSource) String() string {
return s.url
}
func NewSoupSource(url string) SoupSource {
return SoupSource{url: url}
}