Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add appType in ctx for client pkg header

Neeraj Gupta69a4f4848866
diff --git a/internal/api/client.go b/internal/api/client.go
index 5c73866c33..ed82b1c061 100644
--- a/internal/api/client.go
+++ b/internal/api/client.go
@@ -1,6 +1,7 @@
package api
import (
+ "context"
"github.com/go-resty/resty/v2"
)
@@ -8,6 +9,7 @@ const (
EnteAPIEndpoint = "https://api.ente.io"
TokenHeader = "X-Auth-Token"
TokenQuery = "token"
+ ClientPkgHeader = "X-Client-Package"
)
var (
@@ -25,28 +27,40 @@ type Params struct {
Host string
}
+func readValueFromContext(ctx context.Context, key string) interface{} {
+ value := ctx.Value(key)
+ return value
+}
func NewClient(p Params) *Client {
- c := resty.New()
+ enteAPI := resty.New()
if p.Trace {
- c.EnableTrace()
+ enteAPI.EnableTrace()
}
+ enteAPI.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
+ app := readValueFromContext(req.Context(), "app")
+ if app == nil {
+ panic("app not set in context")
+ }
+ req.Header.Set(ClientPkgHeader, StringToApp(app.(string)).ClientPkg())
+ return nil
+ })
if p.Debug {
- c.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
+ enteAPI.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
logRequest(req)
return nil
})
- c.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
+ enteAPI.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
logResponse(resp)
return nil
})
}
if p.Host != "" {
- c.SetBaseURL(p.Host)
+ enteAPI.SetBaseURL(p.Host)
} else {
- c.SetBaseURL(EnteAPIEndpoint)
+ enteAPI.SetBaseURL(EnteAPIEndpoint)
}
return &Client{
- restClient: c,
+ restClient: enteAPI,
}
}
diff --git a/internal/api/enums.go b/internal/api/enums.go
index 42cddc2874..eaaaf010da 100644
--- a/internal/api/enums.go
+++ b/internal/api/enums.go
@@ -1,5 +1,7 @@
package api
+import "fmt"
+
type App string
const (
@@ -7,3 +9,27 @@ const (
AppAuth App = "auth"
AppLocker App = "locker"
)
+
+func StringToApp(s string) App {
+ switch s {
+ case "photos":
+ return AppPhotos
+ case "auth":
+ return AppAuth
+ case "locker":
+ return AppLocker
+ default:
+ panic(fmt.Sprintf("invalid app: %s", s))
+ }
+}
+func (a App) ClientPkg() string {
+ switch a {
+ case AppPhotos:
+ return "io.ente.photos"
+ case AppAuth:
+ return "io.ente.auth"
+ case AppLocker:
+ return "io.ente.locker"
+ }
+ return ""
+}
diff --git a/pkg/account.go b/pkg/account.go
index 243d482555..2809edf5d7 100644
--- a/pkg/account.go
+++ b/pkg/account.go
@@ -2,33 +2,34 @@ package pkg
import (
"cli-go/internal/api"
+ "cli-go/pkg/model"
"context"
"encoding/json"
"fmt"
"log"
+ "runtime"
bolt "go.etcd.io/bbolt"
)
const AccBucket = "accounts"
-type AccountInfo struct {
- Email string `json:"email" binding:"required"`
- UserID int64 `json:"userID" binding:"required"`
- App api.App `json:"app" binding:"required"`
-}
-
func (c *ClICtrl) AddAccount(cxt context.Context) {
var flowErr error
defer func() {
if r := recover(); r != nil {
- log.Println("recovered from panic", r)
+ fmt.Println("Panic occurred:", r)
+ // Print the stack trace
+ stackTrace := make([]byte, 1024*8)
+ stackTrace = stackTrace[:runtime.Stack(stackTrace, false)]
+ fmt.Printf("Stack Trace:\n%s", stackTrace)
}
if flowErr != nil {
log.Fatal(flowErr)
}
}()
-
+ app := GetAppType()
+ cxt = context.WithValue(cxt, "app", string(app))
email, flowErr := GetUserInput("Enter email address")
if flowErr != nil {
return
diff --git a/pkg/promt.go b/pkg/promt.go
index 32936f469d..65abb0b09f 100644
--- a/pkg/promt.go
+++ b/pkg/promt.go
@@ -1,6 +1,7 @@
package pkg
import (
+ "cli-go/internal/api"
"errors"
"fmt"
"log"
@@ -31,6 +32,29 @@ func GetUserInput(label string) (string, error) {
return input, nil
}
+func GetAppType() api.App {
+ for {
+ app, err := GetUserInput("Enter app type (default: photos)")
+ if err != nil {
+ fmt.Printf("Use default app type: %s\n", api.AppPhotos)
+ return api.AppPhotos
+ }
+ switch app {
+ case "photos":
+ return api.AppPhotos
+ case "auth":
+ return api.AppAuth
+ case "locker":
+ return api.AppLocker
+ case "":
+ return api.AppPhotos
+ default:
+ fmt.Println("invalid app type")
+ continue
+ }
+ }
+}
+
func GetCode(promptText string, length int) (string, error) {
for {
ott, err := GetUserInput(promptText)