collection URL instead of EntriesJSON vi fnRequest
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sebastian Frank
2019-03-11 18:56:29 +01:00
parent 6c3f985d1f
commit 4041868a9f
7 changed files with 85 additions and 35 deletions

View File

@@ -2,7 +2,6 @@ package helper
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path"
@@ -100,7 +99,7 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
if colConfig.Name == nil || *colConfig.Name == "" {
Log.Panicf("missing Name in collection config in '%s'", inPath)
}
if colConfig.EntriesJSON == nil || *colConfig.EntriesJSON == "" {
if colConfig.URL == nil || *colConfig.URL == "" {
Log.Panicf("missing EntriesJSON in collection config in '%s'", inPath)
}
}
@@ -109,37 +108,73 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
node.ColMap = make(config.MapString)
}
ctx := newContext()
jsonStr, err := pongo2.RenderTemplateString(*colConfig.EntriesJSON, ctx)
ctx["This"] = node.Config.This
ctx["Data"] = node.Config.Data
url, err := pongo2.RenderTemplateString(*colConfig.URL, ctx)
if err != nil {
Log.Panicf("invalid template string in '%s': %s", inPath, err)
Log.Panicf("invalid template string for Collection Element.URL in '%s': %s", inPath, err)
}
var colSlice []config.MapString
err = json.Unmarshal([]byte(jsonStr), &colSlice)
if err != nil {
Log.Panicf("invalid JSON in EntriesJSON in '%s': %s", inPath, err)
}
node.ColMap[*colConfig.Name] = colSlice
//node.ColMap[*colConfig.Name] = jsonWebRequest(*colConfig.URL)
colData := jsonWebRequest(url)
node.ColMap[*colConfig.Name] = colData
if navT := colConfig.NavTemplate; navT != nil {
tpl := navT.Template
if tpl == "" {
tpl = *newConfig.Template
var entries []interface{}
var ok bool
if navT.EntriesAttribute != "" {
var colDataMap map[string]interface{}
if colDataMap, ok = colData.(map[string]interface{}); ok {
entries, ok = colDataMap[navT.EntriesAttribute].([]interface{})
if !ok {
Log.Debug(spew.Sdump(colDataMap))
Log.Panicf("invalid json data in [%s] from url '%s' for entries", navT.EntriesAttribute, url)
}
}
} else {
entries, ok = colData.([]interface{})
}
if !ok {
Log.Debug(spew.Sdump(colData))
Log.Panicf("invalid json data from url '%s', need array of objects for entries or object with configured NavTemplate.EntriesAttribute", url)
}
// build navigation with detail sites
for _, colEl := range colSlice {
for idx, colEl := range entries {
ctxE := make(pongo2.Context)
err := config.Merge(&ctxE, ctx)
if err != nil {
Log.Panicf("could not merge context in '%s': %s", inPath, err)
}
err = config.Merge(&ctxE, pongo2.Context(colEl))
var jsonCtx map[string]interface{}
if jsonCtx, ok = colEl.(map[string]interface{}); !ok {
Log.Debug(spew.Sdump(colEl))
Log.Panicf("no json object for entry index %d from url '%s'", idx, url)
}
err = config.Merge(&ctxE, pongo2.Context(jsonCtx))
if err != nil {
Log.Panicf("could not merge context in '%s': %s", inPath, err)
}
tpl := ""
if navT.Template != "" {
tpl, err = pongo2.RenderTemplateString(navT.Template, ctxE)
if err != nil {
Log.Panicf("invalid template string for NavTemplate.Template in '%s': %s", inPath, err)
}
}
if tpl == "" {
tpl = *newConfig.Template
}
dataKey := ""
if navT.DataKey != "" {
dataKey, err = pongo2.RenderTemplateString(navT.DataKey, ctxE)
if err != nil {
Log.Panicf("invalid template string for NavTemplate.DataKey in '%s': %s", inPath, err)
}
}
goTo, err := pongo2.RenderTemplateString(navT.GoTo, ctxE)
if err != nil {
Log.Panicf("invalid template string for NavTemplate.GoTo in '%s': %s", inPath, err)
@@ -172,7 +207,7 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
}
}
add2Nav(node, node.Config, tpl, goTo, navname, colEl, navT.DataKey, body, navT.Hidden)
add2Nav(node, node.Config, tpl, goTo, navname, colEl, dataKey, body, navT.Hidden)
}
}

View File

@@ -10,7 +10,7 @@ import (
"github.com/flosch/pongo2"
)
func jsonWebRequest(url string) config.MapString {
func jsonWebRequest(url string) interface{} {
Log.Noticef("requesting url via GET %s", url)
resp, err := http.Get(url)
@@ -38,13 +38,20 @@ func jsonWebRequest(url string) config.MapString {
Log.Panicf("is not json '%s' from url '%s'", contentType, url)
}
jsonMap := make(config.MapString)
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
Log.Panicf("could not read json from '%s': %s", url, err)
if err == nil {
return jsonMap
}
return jsonMap
jsonArrayMap := make([]map[string]interface{}, 0)
err = json.Unmarshal(body, &jsonArrayMap)
if err == nil {
return jsonArrayMap
}
Log.Panicf("could not read json from '%s': invalid type", url)
return nil
}
// RequestFn will make a web request and returns map[string]interface form pongo2