mark2web/pkg/helper/webrequest.go

108 lines
2.3 KiB
Go
Raw Normal View History

2019-03-18 13:34:52 +01:00
package helper
import (
"encoding/json"
2019-03-25 15:07:02 +01:00
"fmt"
"image"
2019-03-18 13:34:52 +01:00
"io/ioutil"
"net/http"
"strings"
2019-03-25 09:28:58 +01:00
"gitbase.de/apairon/mark2web/pkg/logger"
2019-03-18 13:34:52 +01:00
)
2019-03-25 15:07:02 +01:00
type wrImageEntry struct {
img image.Image
format string
}
type wrJSONEntry struct {
data interface{}
}
var wrImageCache = make(map[string]*wrImageEntry)
var wrJSONCache = make(map[string]*wrJSONEntry)
// WebRequest will fetch an url and returns reponse
func WebRequest(url string, opts interface{}) (*http.Response, error) {
logger.Log.Noticef("requesting url via GET %s", url)
return http.Get(url)
}
2019-03-19 12:46:32 +01:00
// JSONWebRequest will GET a json object/array from a given URL
2019-03-18 13:34:52 +01:00
func JSONWebRequest(url string) interface{} {
2019-03-25 15:07:02 +01:00
cached := wrJSONCache[url]
if cached == nil {
resp, err := WebRequest(url, nil)
if err != nil {
logger.Log.Panicf("could not get url '%s': %s", url, err)
}
defer resp.Body.Close()
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Log.Panicf("could not read body from url '%s': %s", url, err)
}
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
logger.Log.Debugf("output from url '%s':\n%s", url, string(body))
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
if resp.StatusCode >= 400 {
logger.Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, url)
}
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
contentType := resp.Header.Get("Content-Type")
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
if strings.Contains(contentType, "json") {
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
} else {
logger.Log.Panicf("is not json '%s' from url '%s'", contentType, url)
}
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
cached = new(wrJSONEntry)
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err == nil {
cached.data = jsonMap
} else {
jsonArrayMap := make([]map[string]interface{}, 0)
err = json.Unmarshal(body, &jsonArrayMap)
if err == nil {
cached.data = jsonArrayMap
} else {
logger.P("could not read json from '%s': invalid type", url)
}
}
wrJSONCache[url] = cached
2019-03-18 13:34:52 +01:00
}
2019-03-25 15:07:02 +01:00
return cached.data
}
// ImageWebRequest gets an image from an url
func ImageWebRequest(url string) (image.Image, string, error) {
cached := wrImageCache[url]
if cached == nil {
resp, err := WebRequest(url, nil)
if err != nil {
return nil, "", fmt.Errorf("could not get url '%s': %s", url, err)
}
img, format, err := image.Decode(resp.Body)
if err != nil {
return nil, "", fmt.Errorf("could read body from url '%s': %s", url, err)
}
cached = &wrImageEntry{
img: img,
format: format,
}
2019-03-18 13:34:52 +01:00
2019-03-25 15:07:02 +01:00
wrImageCache[url] = cached
2019-03-18 13:34:52 +01:00
}
2019-03-25 15:07:02 +01:00
return cached.img, cached.format, nil
2019-03-18 13:34:52 +01:00
}