From 50900c5e18bd5aa1a2329ca3d53ca3713ec5d137 Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 19 Mar 2020 00:13:54 +0530 Subject: Remove package cetus Moved req.go to request package & version to main.go --- cmd/cetus/main.go | 5 +++-- pkg/apod/json.go | 4 ++-- pkg/bpod/json.go | 4 ++-- pkg/cetus/cetus.go | 12 ------------ pkg/cetus/req.go | 44 -------------------------------------------- pkg/request/req.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 62 deletions(-) delete mode 100644 pkg/cetus/cetus.go delete mode 100644 pkg/cetus/req.go create mode 100644 pkg/request/req.go diff --git a/cmd/cetus/main.go b/cmd/cetus/main.go index 7629cd2..f73e391 100644 --- a/cmd/cetus/main.go +++ b/cmd/cetus/main.go @@ -10,7 +10,6 @@ import ( "framagit.org/andinus/cetus/pkg/apod" "framagit.org/andinus/cetus/pkg/background" "framagit.org/andinus/cetus/pkg/bpod" - "framagit.org/andinus/cetus/pkg/cetus" ) func main() { @@ -24,8 +23,10 @@ func main() { os.Exit(1) } + version := "v0.5.0" + if os.Args[1] == "version" { - cetus.Version() + fmt.Printf("Cetus %s\n", version) os.Exit(0) } diff --git a/pkg/apod/json.go b/pkg/apod/json.go index 4589925..d484209 100644 --- a/pkg/apod/json.go +++ b/pkg/apod/json.go @@ -5,7 +5,7 @@ import ( "fmt" "regexp" - "framagit.org/andinus/cetus/pkg/cetus" + "framagit.org/andinus/cetus/pkg/request" ) // Res holds the response from the api. @@ -43,6 +43,6 @@ func GetJson(reqInfo map[string]string) (string, error) { params["api_key"] = reqInfo["apiKey"] params["date"] = reqInfo["date"] - body, err := cetus.GetRes(reqInfo["api"], params) + body, err := request.GetRes(reqInfo["api"], params) return string(body), err } diff --git a/pkg/bpod/json.go b/pkg/bpod/json.go index 4d74668..d895bcf 100644 --- a/pkg/bpod/json.go +++ b/pkg/bpod/json.go @@ -5,7 +5,7 @@ import ( "fmt" "math/rand" - "framagit.org/andinus/cetus/pkg/cetus" + "framagit.org/andinus/cetus/pkg/request" ) type Res struct { @@ -50,6 +50,6 @@ func GetJson(reqInfo map[string]string) (string, error) { } - body, err := cetus.GetRes(reqInfo["api"], params) + body, err := request.GetRes(reqInfo["api"], params) return string(body), err } diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go deleted file mode 100644 index 0e9b7c2..0000000 --- a/pkg/cetus/cetus.go +++ /dev/null @@ -1,12 +0,0 @@ -package cetus - -import ( - "fmt" -) - -var version string = "v0.5.0" - -// Version prints cetus version -func Version() { - fmt.Printf("Cetus %s\n", version) -} diff --git a/pkg/cetus/req.go b/pkg/cetus/req.go deleted file mode 100644 index ed81374..0000000 --- a/pkg/cetus/req.go +++ /dev/null @@ -1,44 +0,0 @@ -package cetus - -import ( - "fmt" - "io/ioutil" - "net/http" - "time" -) - -// GetRes returns api response -func GetRes(api string, params map[string]string) (string, error) { - c := http.Client{ - // TODO: timeout should be configurable by the user - Timeout: time.Second * 64, - } - - 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 -} diff --git a/pkg/request/req.go b/pkg/request/req.go new file mode 100644 index 0000000..350f5f6 --- /dev/null +++ b/pkg/request/req.go @@ -0,0 +1,44 @@ +package request + +import ( + "fmt" + "io/ioutil" + "net/http" + "time" +) + +// GetRes returns api response +func GetRes(api string, params map[string]string) (string, error) { + c := http.Client{ + // TODO: timeout should be configurable by the user + Timeout: time.Second * 64, + } + + 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 +} -- cgit 1.4.1-2-gfad0