diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-19 23:00:05 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-19 23:00:05 -0800 |
commit | 68c183ed75352a78434aeeee8e4d5bfd75db8dd7 (patch) | |
tree | d80cb4b785f88f0110d2ebf610efad6b5c611a69 /system/addon/api.go | |
parent | e80e315b1b1fed2bb1c155d8a2c5d9c77f6df53f (diff) |
adding Query func to addon api for more parity with db package, extract Get from ContentAll for reuse
Diffstat (limited to 'system/addon/api.go')
-rw-r--r-- | system/addon/api.go | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/system/addon/api.go b/system/addon/api.go index b79a29e..b75c09c 100644 --- a/system/addon/api.go +++ b/system/addon/api.go @@ -12,19 +12,52 @@ import ( "github.com/ponzu-cms/ponzu/system/db" ) +// QueryOptions is a miror 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 { host := db.ConfigCache("domain") port := db.ConfigCache("http_port") - endpoint := "http://%s:%s/api/contents?type=%s" + endpoint := "http://%s:%s/api/contents?type=%s&count=-1" + URL := fmt.Sprintf(endpoint, host, port, namespace) + + j, err := Get(URL) + if err != nil { + log.Println("Error in ContentAll for reference HTTP request:", endpoint) + 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 { + host := db.ConfigCache("domain") + port := db.ConfigCache("http_port") + endpoint := "http://%s:%s/api/contents?type=%s&count=%d&order=%s&offset=%d" + URL := fmt.Sprintf(endpoint, host, port, namespace, opts.Count, opts.Order, opts.Offset) + + j, err := Get(URL) + if err != nil { + log.Println("Error in Query for reference HTTP request:", endpoint) + 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) - url := fmt.Sprintf(endpoint, host, port, namespace) - req, err := http.NewRequest(http.MethodGet, url, r) + req, err := http.NewRequest(http.MethodGet, endpoint, r) if err != nil { - log.Println("Error creating request for reference of:", namespace) - return nil + log.Println("Error creating reference HTTP request:", endpoint) + return nil, err } c := http.Client{ @@ -32,16 +65,16 @@ func ContentAll(namespace string) []byte { } res, err := c.Do(req) if err != nil { - log.Println("Error making HTTP request for reference of:", namespace) - return 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 request body for reference of:", namespace) - return nil + log.Println("Error reading body for reference HTTP request:", endpoint) + return nil, err } - return j + return j, nil } |