Skip to content

Commit

Permalink
feat: updated API path due to change from EONET
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed Nov 24, 2021
1 parent 7b1800f commit 2bc0265
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Expand Up @@ -15,7 +15,7 @@ var versionCmd = &cobra.Command{
Short: "Print the current version number of disaster-cli",
Long: `Prints the current version number of disaster-cli`,
Run: func(cmd *cobra.Command, args []string) {
version := fmt.Sprintf("disaster %s", VersionString)
version := fmt.Sprintf("disaster %s%s", "v", VersionString)
fmt.Println(version)
},
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -13,6 +13,6 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
60 changes: 2 additions & 58 deletions go.sum

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions library/client.go
Expand Up @@ -12,10 +12,30 @@ const (
ISSUE_MSG = " Please open up a Github issue to report this error! https://github.com/karl-cardenas-coding/disaster-cli"
)

var disasterClient = &http.Client{Timeout: 10 * time.Second}
var disasterClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 5 * time.Second,
DisableCompression: true,
ForceAttemptHTTP2: true,
},
}

func getJson(url string, target interface{}) error {
r, err := disasterClient.Get(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("unable to create HTTP request")
}

req.Header = http.Header{
"Content-Type": []string{`application/json; charset=utf-8`},
"Accept-Encoding": []string{"gzip, deflate, br"},
"Accept": []string{"application/json"},
}

r, err := disasterClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"package": "cmd",
Expand Down
Binary file removed library/disaster
Binary file not shown.
71 changes: 61 additions & 10 deletions library/query.go
Expand Up @@ -4,21 +4,50 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

log "github.com/sirupsen/logrus"
)

func QueryEventAPI(apikey string) EventResponse {
url := fmt.Sprintf("https://eonet.sci.gsfc.nasa.gov/api/v3/events?api_key=%s", apikey)

resp, err := http.Get(url)
var (
url string
records EventResponse
)
if apikey != "" {
url = fmt.Sprintf("https://eonet.gsfc.nasa.gov/api/v3/events?api_key=%s", apikey)
} else {
url = "https://eonet.gsfc.nasa.gov/api/v3/events"
}

client := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 5 * time.Second,
DisableCompression: true,
ForceAttemptHTTP2: true,
},
}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("unable to create HTTP request")
}

req.Header = http.Header{
"Content-Type": []string{`application/json; charset=utf-8`},
"Accept-Encoding": []string{"gzip, deflate, br"},
"Accept": []string{"application/json"},
}

resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
}
defer resp.Body.Close()

var records EventResponse

if err := json.NewDecoder(resp.Body).Decode(&records); err != nil {
log.Println(err)
}
Expand All @@ -28,16 +57,40 @@ func QueryEventAPI(apikey string) EventResponse {
}

func QueryCategoriesAPI(apikey string, category string) CategoriesResponse {
var url string
var (
url string
records CategoriesResponse
)

if category == "" {
url = fmt.Sprintf("https://eonet.sci.gsfc.nasa.gov/api/v3/categories/%s?api_key=%s", category, apikey)
url = fmt.Sprintf("https://eonet.gsfc.nasa.gov/api/v3/categories/%s?api_key=%s", category, apikey)
} else {
url = fmt.Sprintf("https://eonet.sci.gsfc.nasa.gov/api/v3/categories?api_key=%s", apikey)
url = fmt.Sprintf("https://eonet.gsfc.nasa.gov/api/v3/categories?api_key=%s", apikey)

}

client := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 5 * time.Second,
DisableCompression: true,
ForceAttemptHTTP2: true,
},
}

resp, err := http.Get(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("unable to create HTTP request")
}

req.Header = http.Header{
"Content-Type": []string{`application/json; charset=utf-8`},
"Accept-Encoding": []string{"gzip, deflate, br"},
"Accept": []string{"application/json"},
}

resp, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{
"package": "cmd",
Expand All @@ -50,8 +103,6 @@ func QueryCategoriesAPI(apikey string, category string) CategoriesResponse {
}
defer resp.Body.Close()

var records CategoriesResponse

if err := json.NewDecoder(resp.Body).Decode(&records); err != nil {
log.WithFields(log.Fields{
"package": "cmd",
Expand Down
4 changes: 2 additions & 2 deletions library/structs_test.go
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestEventStrcuts(t *testing.T) {
urlEvents := "https://eonet.sci.gsfc.nasa.gov/api/v3/events"
urlEvents := "https://eonet.gsfc.nasa.gov/api/v3/events"
event := new(EventResponse)

err := getJson(urlEvents, event)
Expand All @@ -30,7 +30,7 @@ func TestEventStrcuts(t *testing.T) {

func TestCategoriestrcuts(t *testing.T) {

urlCategories := "https://eonet.sci.gsfc.nasa.gov/api/v3/categories/"
urlCategories := "https://eonet.gsfc.nasa.gov/api/v3/categories/"
CategoriesRes := new(CategoriesResponse)

err := getJson(urlCategories, CategoriesRes)
Expand Down

0 comments on commit 2bc0265

Please sign in to comment.