-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
388 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cloud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/libdyson-wg/libdyson-go/internal/generated/oapi" | ||
"net/http" | ||
) | ||
|
||
type ServerRegion int | ||
|
||
const ( | ||
RegionGlobal ServerRegion = iota | ||
RegionChina | ||
) | ||
|
||
var ( | ||
region ServerRegion = RegionGlobal | ||
client oapi.ClientWithResponsesInterface | ||
|
||
provisioned bool | ||
|
||
token string | ||
|
||
servers = []string{ | ||
"https://appapi.cp.dyson.com", | ||
"https://appapi.cp.dyson.cn", | ||
} | ||
) | ||
|
||
func init() { | ||
setServer() | ||
} | ||
|
||
func addAuthToken(_ context.Context, r *http.Request) error { | ||
if token != "" { | ||
r.Header.Set("Authorization", "Bearer "+token) | ||
} | ||
return nil | ||
} | ||
|
||
func addUserAgent(_ context.Context, r *http.Request) error { | ||
r.Header.Set("User-Agent", "android client") | ||
return nil | ||
} | ||
|
||
func setServer() { | ||
var err error | ||
client, err = oapi.NewClientWithResponses( | ||
servers[region], | ||
oapi.WithRequestEditorFn(addAuthToken), | ||
oapi.WithRequestEditorFn(addUserAgent), | ||
) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to initialize client: %w", err)) | ||
} | ||
|
||
_, err = client.ProvisionWithResponse(context.Background()) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to provision api client: %w", err)) | ||
} | ||
} | ||
|
||
func SetToken(t string) { | ||
token = t | ||
} | ||
|
||
func SetServerRegion(r ServerRegion) { | ||
region = r | ||
setServer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/libdyson-wg/libdyson-go/cloud" | ||
"github.com/libdyson-wg/libdyson-go/config" | ||
"github.com/libdyson-wg/libdyson-go/internal/account" | ||
"github.com/libdyson-wg/libdyson-go/internal/shell" | ||
) | ||
|
||
type functions struct { | ||
Login func() error | ||
GetDevices func() error | ||
} | ||
|
||
var funcs functions | ||
|
||
func init() { | ||
funcs = functions{ | ||
Login: account.Login( | ||
shell.PromptForInput, | ||
shell.PromptForPassword, | ||
cloud.BeginLogin, | ||
cloud.CompleteLogin, | ||
config.SetToken, | ||
cloud.SetToken, | ||
cloud.SetServerRegion, | ||
), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/libdyson-wg/libdyson-go/config" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var loginCmd = &cobra.Command{ | ||
Use: "login", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return funcs.Login() | ||
}, | ||
PostRun: func(cmd *cobra.Command, args []string) { | ||
fmt.Println( | ||
fmt.Sprintf( | ||
"You are logged in. Please note that your API Token has been saved to %s.\n\n"+ | ||
"This API Token is sensitive and should not be shared with anyone you don't trust. "+ | ||
"It could possibly be used to control your Dyson devices or learn sensitive private "+ | ||
"information about you through your Dyson account.", | ||
config.GetFilePath(), | ||
), | ||
) | ||
}, | ||
} | ||
|
||
func init() { | ||
loginCmd.Run = | ||
rootCmd.AddCommand(loginCmd) | ||
rootCmd.AddCommand(loginCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
package main | ||
|
||
import "libdyson-go/cmd/dson/cmd" | ||
import ( | ||
"github.com/libdyson-wg/libdyson-go/cloud" | ||
"github.com/libdyson-wg/libdyson-go/cmd/dson/cmd" | ||
"github.com/libdyson-wg/libdyson-go/config" | ||
) | ||
|
||
func main() { | ||
tok, err := config.GetToken() | ||
if err != nil { | ||
panic(err) | ||
} | ||
cloud.SetToken(tok) | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
func SetToken(t string) error { | ||
conf, err := readConfig() | ||
if err != nil { | ||
return fmt.Errorf("could not read config: %v", err) | ||
} | ||
|
||
conf.Token = t | ||
err = writeConfig(conf) | ||
if err != nil { | ||
err = fmt.Errorf("could not save config: %v", err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func GetToken() (string, error) { | ||
conf, err := readConfig() | ||
if err != nil { | ||
return "", fmt.Errorf("could not read token from config: %v", err) | ||
} | ||
|
||
return conf.Token, nil | ||
} |
Oops, something went wrong.