Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: daily now uses the fact api can take multiple packages #3

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions cmd/daily.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"

"github.com/devOpifex/cranlogs/internal/color"
"github.com/devOpifex/cranlogs/internal/data"
Expand All @@ -16,22 +17,29 @@ func daily(_ *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("no package specified")
}
// TODO: this still has some issues in how its handled
// as if a specific package fails, it will cause the entire process to
// exit then. A different pattern could be to aggregate errors for
// failed packages, then print out successful ones, then at the end
// the error(s) for the failed ones
for _, pkg := range args {
daily, err := data.GetDaily(dailyPeriod, pkg)
p, err := data.NewPeriod(dailyPeriod)
if err != nil {
log.Fatal(err)
}
dailyDls, err := data.GetDaily(p, args)
if err != nil {
log.Fatalf("error getting daily downloads: %s", err)
}

if printJson {
var out bytes.Buffer
dailyBytes, err := json.Marshal(dailyDls)
if err != nil {
color.PrintError(err.Error())
// given an error should not exit with a 0 exit code
os.Exit(-1)
log.Fatalf("error marshalling daily downloads: %s", err)
}

fmt.Printf("Package: %v %v %v\n", color.Yellow, daily.Package, color.Reset)
for _, v := range daily.Downloads {
fmt.Printf("%v%v%v: %v\n", color.Cyan, v.Day, color.Reset, v.Downloads)
json.Indent(&out, dailyBytes, "", " ")
fmt.Println(out.String())
} else {
for _, d := range dailyDls {
fmt.Printf("Package: %v %v %v\n", color.Yellow, d.Package, color.Reset)
for _, v := range d.Downloads {
fmt.Printf("%v%v%v: %v\n", color.Cyan, v.Day, color.Reset, v.Downloads)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/spf13/cobra"
)

var printJson bool

var rootCmd = &cobra.Command{
Use: "Cranlogs",
Short: "Access the cranlogs API",
Expand All @@ -25,4 +27,5 @@ func Execute() {

func init() {
rootCmd.AddCommand(newDailyCmd())
rootCmd.PersistentFlags().BoolVar(&printJson, "json", false, "output in json format")
}
26 changes: 16 additions & 10 deletions internal/data/daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)

type Daily struct {
Expand All @@ -18,31 +19,36 @@ type DailyDownload struct {
Downloads int `json:"downloads"`
}

func GetDaily(period, pkg string) (Daily, error) {
func GetDaily(period Period, pkgs []string) ([]Daily, error) {
// weirdly the API returns an array of length 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is because of that comma separated packages - keeps the same data structure if 1 package or many..... note: need to update to remove the comment now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know we could pass multiple packages

var daily []Daily

path := URL + "downloads/daily/" + period + "/" + pkg

path := URL + "downloads/daily/" + string(period) + "/" + strings.Join(pkgs, ",")
resp, err := http.Get(path)

if err != nil {
return Daily{}, err
return daily, err
}
// This is currently kind of meaningless until
// https://github.com/r-hub/cranlogs.app/issues/41 is resolved but at least
// its there
if resp.StatusCode != 200 {
return daily, err
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is.... bare minimum ok - ideally would consider more about what alternative responses could come in then handle them. One huge issue is essentially this will currently swallow the result that isn't an error. So for example, the boy json with the error message currently is being swallowed - so this needs to get refactored to likely generate a better, processed error.

}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return Daily{}, err
return daily, err
}

err = json.Unmarshal(body, &daily)

// if this errors given the current 200 status code returning an error response
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now (partially) fixed due to the new deployment that does 404 (at least for some cases)

// such as { "error": "Invalid query", "info": "https://github.com/metacran/cranlogs.app" }
// its still not the best error code
if err != nil {
return Daily{}, err
return daily, err
}

return daily[0], nil
return daily, nil
}