diff options
author | Andinus <andinus@inventati.org> | 2020-03-16 01:20:19 +0530 |
---|---|---|
committer | Andinus <andinus@inventati.org> | 2020-03-16 01:20:19 +0530 |
commit | 03c467b836efcfcacca499518a2ba284bca299f6 (patch) | |
tree | 415b227c5d8daab8997b9de49e469c52e7fb86c0 | |
parent | 53ecbbc03b8cd8e54479c2d90cbdae9921eaa127 (diff) | |
download | cetus-03c467b836efcfcacca499518a2ba284bca299f6.tar.gz |
Rewrite apod response retrival
-rw-r--r-- | cmd/cetus-bing/cetus-bing.go | 12 | ||||
-rw-r--r-- | cmd/cetus-nasa/cetus-nasa.go | 106 | ||||
-rw-r--r-- | pkg/cetus/cetus.go | 11 | ||||
-rw-r--r-- | pkg/nasa/apod.go | 73 |
4 files changed, 82 insertions, 120 deletions
diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go index 871c96a..b8c397c 100644 --- a/cmd/cetus-bing/cetus-bing.go +++ b/cmd/cetus-bing/cetus-bing.go @@ -4,7 +4,6 @@ import ( "encoding/json" "flag" "fmt" - "log" "math/rand" "time" @@ -60,7 +59,7 @@ func main() { bpod := bpod{} err = json.Unmarshal([]byte(body), &bpod) - errChk("body unmarshal failed", err) + cetus.ErrChk("body unmarshal failed", err) // if random was set then bpodRes holds list of multiple // responses, choose a random response from the list @@ -72,7 +71,7 @@ func main() { // correct date format dt, err := time.Parse("20060102", bpodPhoto.startDate) - errChk("bpodPhoto.startDate parse failed", err) + cetus.ErrChk("bpodPhoto.startDate parse failed", err) bpodPhoto.startDate = dt.Format("2006-01-02") printDetails(bpodPhoto) @@ -83,7 +82,7 @@ func main() { } err = background.Set(bpodPhoto.url) - errChk("setting background failed", err) + cetus.ErrChk("setting background failed", err) } func parseFlags() { @@ -126,8 +125,3 @@ func bpodBody() (string, error) { body, err := bing.GetBpodJson(reqInfo, t) return body, err } - -func errChk(ctx string, err error) { - log.Println(ctx) - log.Fatal(err) -} diff --git a/cmd/cetus-nasa/cetus-nasa.go b/cmd/cetus-nasa/cetus-nasa.go index 495a308..fafd921 100644 --- a/cmd/cetus-nasa/cetus-nasa.go +++ b/cmd/cetus-nasa/cetus-nasa.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "fmt" "log" @@ -11,22 +12,31 @@ import ( "framagit.org/andinus/cetus/pkg/nasa" ) +type apod struct { + copyright string `json:"copyright"` + date string `json:"date"` + explanation string `json:"explanation"` + hdURL string `json:"hdurl"` + mediaType string `json:"media_type"` + serviceVersion string `json:"service_version"` + title string `json:"title"` + url string `json:"url"` + + code int `json:"code"` + msg string `json:"msg"` +} + var ( + t time.Duration + api string + date string + dump bool quiet bool + random bool + apiKey string version bool fetchOnly bool pathOnly bool - - api string - apiKey string - date string - random bool - dateHelp string - dateDefault string - timeout time.Duration - - err error - apodRes nasa.APOD ) func main() { @@ -38,22 +48,26 @@ func main() { } // Convert timeout to seconds - timeout = timeout * time.Second + t = t * time.Second if random { date = nasa.RandDate() } - // get response from api - apodRes, err = getAPODRes() - if err != nil { - if len(apodRes.Msg) != 0 { - log.Println("Message: ", apodRes.Msg) - } - log.Fatal(err) + body, err := apodBody() + if dump { + fmt.Println(body) + return } - printDetails(apodRes) + apod := apod{} + err = json.Unmarshal([]byte(body), &apod) + cetus.ErrChk("body unmarshal failed", err) + if len(apod.msg) != 0 { + log.Println("Message: ", apod.msg) + } + + printDetails(apod) // if fetchOnly is true then don't set background if fetchOnly { @@ -61,11 +75,9 @@ func main() { } // if media type is an image then set background - if apodRes.MediaType == "image" { - err = background.Set(apodRes.HDURL) - if err != nil { - log.Fatal(err) - } + if apod.mediaType == "image" { + err = background.Set(apod.hdURL) + cetus.ErrChk("setting background failed", err) } } @@ -74,8 +86,8 @@ func parseFlags() { flag.BoolVar(&quiet, "quiet", false, "No output") flag.BoolVar(&version, "version", false, "Cetus version") flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info") - - dateHelp = fmt.Sprintf("Choose a random date between 1995-06-16 & %s", + flag.BoolVar(&dump, "dump", false, "Only dump received response") + dateHelp := fmt.Sprintf("Choose a random date between 1995-06-16 & %s", time.Now().UTC().Format("2006-01-02")) flag.BoolVar(&random, "random", false, dateHelp) flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image") @@ -83,42 +95,40 @@ func parseFlags() { flag.StringVar(&api, "api", "https://api.nasa.gov/planetary/apod", "APOD API URL") flag.StringVar(&apiKey, "api-key", "DEMO_KEY", "api.nasa.gov key for expanded usage") - dateDefault = time.Now().UTC().Format("2006-01-02") + dateDefault := time.Now().UTC().Format("2006-01-02") flag.StringVar(&date, "date", dateDefault, "Date of the APOD image to retrieve") - flag.DurationVar(&timeout, "timeout", 32*time.Second, "Timeout for http client in seconds") + flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds") flag.Parse() } -func printDetails(apodRes nasa.APOD) { +func printDetails(apod apod) { if quiet { return } if pathOnly { - cetus.PrintPath(apodRes.HDURL) + cetus.PrintPath(apod.hdURL) return } - fmt.Printf("Title: %s\n\n", apodRes.Title) - fmt.Printf("Copyright: %s\n", apodRes.Copyright) - fmt.Printf("Date: %s\n\n", apodRes.Date) - fmt.Printf("Media Type: %s\n", apodRes.MediaType) - if apodRes.MediaType == "image" { - fmt.Printf("URL: %s\n\n", apodRes.HDURL) + fmt.Printf("Title: %s\n\n", apod.title) + fmt.Printf("Copyright: %s\n", apod.copyright) + fmt.Printf("Date: %s\n\n", apod.date) + fmt.Printf("Media Type: %s\n", apod.mediaType) + if apod.mediaType == "image" { + fmt.Printf("URL: %s\n\n", apod.hdURL) } else { - fmt.Printf("URL: %s\n\n", apodRes.URL) + fmt.Printf("URL: %s\n\n", apod.url) } - fmt.Printf("Explanation: %s\n", apodRes.Explanation) + fmt.Printf("Explanation: %s\n", apod.explanation) } -func getAPODRes() (nasa.APOD, error) { - var apodInfo map[string]string - apodInfo = make(map[string]string) - apodInfo["api"] = api - apodInfo["apiKey"] = apiKey - apodInfo["date"] = date - - apodRes, err = nasa.APODPath(apodInfo, timeout) +func apodBody() (string, error) { + reqInfo := make(map[string]string) + reqInfo["api"] = api + reqInfo["apiKey"] = apiKey + reqInfo["date"] = date - return apodRes, err + body, err := nasa.GetApodJson(reqInfo, t) + return body, err } diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go index c22c742..ec7fc4c 100644 --- a/pkg/cetus/cetus.go +++ b/pkg/cetus/cetus.go @@ -1,6 +1,9 @@ package cetus -import "fmt" +import ( + "fmt" + "log" +) var version string = "v0.4.6" @@ -13,3 +16,9 @@ func Version() { func PrintPath(path string) { fmt.Println(path) } + +// ErrChk logs the context & error +func ErrChk(ctx string, err error) { + log.Println(ctx) + log.Fatal(err) +} diff --git a/pkg/nasa/apod.go b/pkg/nasa/apod.go index 6f89bcc..606bbdf 100644 --- a/pkg/nasa/apod.go +++ b/pkg/nasa/apod.go @@ -1,29 +1,13 @@ package nasa import ( - "encoding/json" "fmt" - "io/ioutil" "math/rand" - "net/http" "regexp" "time" -) - -// APOD holds responses -type APOD struct { - Copyright string `json:"copyright"` - Date string `json:"date"` - Explanation string `json:"explanation"` - HDURL string `json:"hdurl"` - MediaType string `json:"media_type"` - ServiceVersion string `json:"service_version"` - Title string `json:"title"` - URL string `json:"url"` - Code int `json:"code"` - Msg string `json:"msg"` -} + "framagit.org/andinus/cetus/pkg/cetus" +) // RandDate returns a random date between 1995-06-16 & today func RandDate() string { @@ -44,52 +28,17 @@ func RandDate() string { return date } -// APODPath returns Astronomy Picture of the Day path -func APODPath(apodInfo map[string]string, timeout time.Duration) (APOD, error) { - var err error - apodRes := APOD{} - - // validate date +// GetApodJson returns json response received from the api +func GetApodJson(reqInfo map[string]string, t time.Duration) (string, error) { re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])") - if !re.MatchString(apodInfo["date"]) { - return apodRes, fmt.Errorf("%s does not match format 'YYYY-MM-DD'", apodInfo["date"]) - } - - client := http.Client{ - Timeout: time.Second * timeout, - } - - req, err := http.NewRequest(http.MethodGet, apodInfo["api"], nil) - if err != nil { - return apodRes, err + if !re.MatchString(reqInfo["date"]) { + return "", fmt.Errorf("%s does not match format 'YYYY-MM-DD'", reqInfo["date"]) } - q := req.URL.Query() - q.Add("api_key", apodInfo["apiKey"]) - q.Add("date", apodInfo["date"]) - req.URL.RawQuery = q.Encode() - res, err := client.Do(req) - - if err != nil { - fmt.Printf("Error: GET %s\n", apodInfo["api"]) - return apodRes, err - } - defer res.Body.Close() - - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - return apodRes, err - } - - err = json.Unmarshal([]byte(resBody), &apodRes) - if err != nil { - return apodRes, err - } - - if res.StatusCode != 200 { - return apodRes, fmt.Errorf("Unexpected response status code received: %d %s", - res.StatusCode, http.StatusText(res.StatusCode)) - } + params := make(map[string]string) + params["api_key"] = reqInfo["apiKey"] + params["date"] = reqInfo["date"] - return apodRes, err + body, err := cetus.GetRes(reqInfo["api"], params, t) + return string(body), err } |