Skip to content

Commit

Permalink
sfeat: add Koios auth and auth profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
mkungla committed Mar 30, 2024
1 parent 17931fb commit 0d24fbf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 32 additions & 3 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log/slog"
"net/url"
"os"
"path/filepath"
"sync"
"time"

Expand Down Expand Up @@ -56,9 +60,10 @@ func Command() *happy.Command {
varflag.BoolFunc("host-preview", false, "Use preview network host"),
varflag.BoolFunc("host-preprod", false, "Use preprod network host"),
varflag.BoolFunc("host-guildnet", false, "Use guildnet network host"),
varflag.BoolFunc("enable-req-stats", false, "Enable request stats"),
varflag.BoolFunc("stats", false, "Enable request stats"),
varflag.BoolFunc("no-format", false, "prints response as machine readable json string"),
varflag.DurationFunc("timeout", time.Duration(time.Minute), "Set timeout for the API server"),

Check failure on line 65 in internal/api/api.go

View workflow job for this annotation

GitHub Actions / lint

undefined: varflag.DurationFunc
varflag.StringFunc("auth", "", "JWT Bearer Auth token generated via https://koios.rest Profile page."),
)

api := &client{}
Expand All @@ -83,7 +88,7 @@ func (c *client) configure(sess *happy.Session, args happy.Args) (err error) {
sess.Log().Debug("configure koios api client")

apiVersion := args.Flag("api-version").String()
enableReqStats, err := args.Flag("enable-req-stats").Var().Value().Bool()
enableReqStats, err := args.Flag("stats").Var().Value().Bool()
if err != nil {
return err
}
Expand All @@ -105,7 +110,7 @@ func (c *client) configure(sess *happy.Session, args happy.Args) (err error) {
sess.Log().Debug(
"configutation",
slog.String("api-version", apiVersion),
slog.Bool("enable-req-stats", enableReqStats),
slog.Bool("stats", enableReqStats),
slog.Bool("no-format", c.noFormat),
slog.Int("rate-limit", ratelimit),
slog.String("sheme", sheme),
Expand All @@ -124,6 +129,30 @@ func (c *client) configure(sess *happy.Session, args happy.Args) (err error) {
koios.RateLimit(ratelimit),
koios.Timeout(duration),
)

if args.Flag("profile").Present() && args.Flag("auth").Present() {
return fmt.Errorf("profile and auth flags cannot be used together")
}

if args.Flag("auth").Present() {
if err := c.kc.SetAuth(args.Flag("auth").String()); err != nil {
return fmt.Errorf("failed to set auth token: %w", err)
}
}

if args.Flag("profile").Present() {
koiosAuthFile := filepath.Join(sess.Get("app.fs.path.config").String(), "koios-auth.jwt")
if _, err := os.Stat(koiosAuthFile); errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("auth token file not found: %w", err)
}

tokenfile, err := os.ReadFile(koiosAuthFile)
if err != nil {
return fmt.Errorf("failed to read auth token file: %w", err)
}
return c.kc.SetAuth(string(tokenfile))
}

return
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"github.com/cardano-community/koios-cli/v2/internal/api"
"github.com/cardano-community/koios-cli/v2/internal/auth"
"github.com/cardano-community/koios-cli/v2/koios"
"github.com/happy-sdk/happy"
"github.com/happy-sdk/happy/sdk/logging"
Expand All @@ -28,7 +29,8 @@ func main() {
}).
WithLogger(logging.Console(logOpts)).
WithBrand(koios.Brand()).
WithCommand(api.Command())
WithCommand(api.Command()).
WithCommand(auth.Command())

app.Run()
}

0 comments on commit 0d24fbf

Please sign in to comment.