summary refs log tree commit diff stats
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bing/bpod.go77
-rw-r--r--pkg/cetus/req.go43
2 files changed, 55 insertions, 65 deletions
diff --git a/pkg/bing/bpod.go b/pkg/bing/bpod.go
index 041eef7..c7ee79d 100644
--- a/pkg/bing/bpod.go
+++ b/pkg/bing/bpod.go
@@ -1,76 +1,23 @@
 package bing
 
 import (
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"net/http"
 	"time"
-)
-
-// Photo holds responses
-type Photo struct {
-	StartDate     string `json:"startdate"`
-	FullStartDate string `json:"fullstartdate"`
-	EndDate       string `json:"enddate"`
-	URL           string `json:"url"`
-	URLBase       string `json:"urlbase"`
-	Copyright     string `json:"copyright"`
-	CopyrightLink string `json:"copyrightlink"`
-	Title         string `json:"title"`
-	Hsh           string `json:"hsh"`
-}
-
-// BPOD  holds list of response
-type BPOD struct {
-	Photos []Photo `json:"images"`
-}
 
-// BPODPath returns Bing Photo of the Day responses
-func BPODPath(bpodInfo map[string]string, timeout time.Duration) (BPOD, error) {
-	var err error
-	bpodRes := BPOD{}
-
-	client := http.Client{
-		Timeout: time.Second * timeout,
-	}
-
-	req, err := http.NewRequest(http.MethodGet, bpodInfo["api"], nil)
-	if err != nil {
-		return bpodRes, err
-	}
-	q := req.URL.Query()
-	q.Add("format", "js")
-
-	// if random flag is passed then fetch 7 photos
-	if bpodInfo["random"] == "true" {
-		q.Add("n", "7")
-	} else {
-		q.Add("n", "1")
-	}
-	req.URL.RawQuery = q.Encode()
-
-	res, err := client.Do(req)
-
-	if err != nil {
-		return bpodRes, err
-	}
-	defer res.Body.Close()
+	"framagit.org/andinus/cetus/pkg/cetus"
+)
 
-	resBody, err := ioutil.ReadAll(res.Body)
-	if err != nil {
-		return bpodRes, err
-	}
+// GetBpodJson returns json response received from the api
+func GetBpodJson(reqInfo map[string]string, t time.Duration) (string, error) {
+	params := make(map[string]string)
+	params["format"] = "js"
+	params["n"] = "1"
 
-	err = json.Unmarshal([]byte(resBody), &bpodRes)
-	if err != nil {
-		return bpodRes, err
-	}
+	// if random is true then fetch 7 photos
+	if reqInfo["random"] == "true" {
+		params["n"] = "7"
 
-	if res.StatusCode != 200 {
-		return bpodRes, fmt.Errorf("Unexpected response status code received: %d %s",
-			res.StatusCode, http.StatusText(res.StatusCode))
 	}
 
-	return bpodRes, err
+	body, err := cetus.GetRes(reqInfo["api"], params, t)
+	return string(body), err
 }
diff --git a/pkg/cetus/req.go b/pkg/cetus/req.go
new file mode 100644
index 0000000..84e52a5
--- /dev/null
+++ b/pkg/cetus/req.go
@@ -0,0 +1,43 @@
+package cetus
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"time"
+)
+
+// GetRes returns api response
+func GetRes(api string, params map[string]string, t time.Duration) (string, error) {
+	c := http.Client{
+		Timeout: time.Second * t,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, api, nil)
+	if err != nil {
+		return "", err
+	}
+
+	q := req.URL.Query()
+	for k, v := range params {
+		q.Add(k, v)
+	}
+	req.URL.RawQuery = q.Encode()
+
+	res, err := c.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+
+	if res.StatusCode != 200 {
+		return "", fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return "", err
+	}
+	return string(body), err
+}