Skip to content

Commit

Permalink
fix: api may not return query
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Jan 23, 2019
1 parent e6009a4 commit 70b3f54
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,30 @@ func httpGet(url string) *jason.Object {
resp, err := http.Get(url)
if err != nil {
// handle error
log.Fatalf("Fail to request api: %#v", err)
return nil
}

defer resp.Body.Close()

if resp.StatusCode == 200 {
obj, _ := jason.NewObjectFromReader(resp.Body)
obj, err := jason.NewObjectFromReader(resp.Body)
if err != nil {
log.Fatalf("Fail to parse json: %#v", err)
return nil
}
return obj
}
log.Fatalf("HTTP Non 200: %v", resp)
log.Fatalf("HTTP Non 200: %#v", resp)
return nil
}

func printExplain(v *jason.Object) {
func printExplain(q string, v *jason.Object) {

query, _ := v.GetString("query")
query, err := v.GetString("query")
if err != nil {
query = q
}
fmt.Fprintf(color.Output, color.HiWhiteString("%s ", query))

if basic, err := v.GetObject("basic"); err == nil {
Expand Down Expand Up @@ -128,8 +137,7 @@ func interativeMode(from string) {
break
}

printExplain(httpGet(ydAPI(query, from)))

printExplain(query, httpGet(ydAPI(query, from)))
fmt.Print("> ")
}
if err := scanner.Err(); err != nil {
Expand Down Expand Up @@ -169,5 +177,5 @@ func main() {
}

query := strings.Join(os.Args[1:], " ")
printExplain(httpGet(ydAPI(query, from)))
printExplain(query, httpGet(ydAPI(query, from)))
}

0 comments on commit 70b3f54

Please sign in to comment.