summaryrefslogtreecommitdiff
path: root/system/addon/api.go
blob: ae4909b2e2c20bb664547754b1e55446736c64e1 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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/ponzu-cms/ponzu/system/db"
)

// QueryOptions is a mirror of the same struct in db package and are re-declared
// here only to make the API simpler for the caller
type QueryOptions db.QueryOptions

// ContentAll retrives all items from the HTTP API within the provided namespace
func ContentAll(namespace string) []byte {
	addr := db.ConfigCache("bind_addr").(string)
	port := db.ConfigCache("http_port").(string)
	endpoint := "http://%s:%s/api/contents?type=%s&count=-1"
	URL := fmt.Sprintf(endpoint, addr, port, namespace)

	j, err := Get(URL)
	if err != nil {
		log.Println("Error in ContentAll for reference HTTP request:", URL)
		return nil
	}

	return j
}

// Query retrieves a set of content from the HTTP API  based on options
// and returns the total number of content in the namespace and the content
func Query(namespace string, opts QueryOptions) []byte {
	addr := db.ConfigCache("bind_addr").(string)
	port := db.ConfigCache("http_port").(string)
	endpoint := "http://%s:%s/api/contents?type=%s&count=%d&offset=%d&order=%s"
	URL := fmt.Sprintf(endpoint, addr, port, namespace, opts.Count, opts.Offset, opts.Order)

	j, err := Get(URL)
	if err != nil {
		log.Println("Error in Query for reference HTTP request:", URL)
		return nil
	}

	return j
}

// Get is a helper function to make a HTTP call from an addon
func Get(endpoint string) ([]byte, error) {
	buf := []byte{}
	r := bytes.NewReader(buf)

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

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

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

	return j, nil
}