summaryrefslogtreecommitdiff
path: root/system/addon/api.go
blob: 4c3152b302f1dbcc7de5c345b208c1bc27853402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package addon provides an API for Ponzu addons to interface with the system
package addon

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/bosssauce/ponzu/system/db"
)

// ContentAll retrives all items from the HTTP API within the provided namespace
func ContentAll(namespace string) []byte {
	host := db.ConfigCache("domain")
	port := db.ConfigCache("http_port")
	endpoint := "http://%s:%s/api/contents?type=%s"
	buf := []byte{}
	r := bytes.NewReader(buf)
	url := fmt.Sprintf(endpoint, host, port, namespace)

	req, err := http.NewRequest(http.MethodGet, url, r)
	if err != nil {
		log.Println("Error creating request for reference of:", namespace)
		return nil
	}

	c := http.Client{
		Timeout: time.Duration(time.Second * 5),
	}
	res, err := c.Do(req)
	if err != nil {
		log.Println("Error making HTTP request for reference of:", namespace)
		return nil
	}
	defer res.Body.Close()

	j, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Println("Error reading request body for reference of:", namespace)
		return nil
	}

	return j
}