Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MickMake committed Feb 17, 2022
1 parent bebe911 commit d6a95ce
Show file tree
Hide file tree
Showing 38 changed files with 2,562 additions and 2,223 deletions.
152 changes: 122 additions & 30 deletions .idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions cmd/cmd_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ func cmdApiGetFunc(cmd *cobra.Command, args []string) {
UserAccount: "",
UserPassword: "",
})
a, _ := SunGro.Web.Get(hey1)
a := SunGro.ApiRoot.Get(hey1)
fmt.Printf("resp: %v\n", a)

hey := SunGro.GetEndpoint(args[1], args[0])
_ = hey.SetRequest(getPowerDevicePointNames.Request{
_ = hey.SetRequest(getPowerDevicePointNames.Request {
RequestCommon: api.RequestCommon {
Appkey: Cmd.ApiAppKey,
Lang: "_en_US",
Expand All @@ -115,7 +115,7 @@ func cmdApiGetFunc(cmd *cobra.Command, args []string) {
},
DeviceType: "",
})
r, _ := SunGro.Web.Get(hey1)
r := SunGro.ApiRoot.Get(hey1)
fmt.Printf("resp: %v\n", r)

fmt.Printf("HEY:%v\n", hey)
Expand Down
5 changes: 3 additions & 2 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
}

Cmd.ConfigFile = filepath.Join(Cmd.ConfigDir, defaultConfigFile)
Cmd.ApiTokenFile = filepath.Join(Cmd.ConfigDir, defaultTokenFile)

rootCmd.PersistentFlags().StringVarP(&Cmd.ApiUsername, flagApiUsername, "u", "", fmt.Sprintf("SunGro: api username."))
rootViper.SetDefault(flagApiUsername, "")
Expand Down Expand Up @@ -282,8 +283,8 @@ func readConfig() error {

_, _ = fmt.Fprintf(os.Stderr, "Api Url: %v\n", rootViper.Get(flagApiUrl))
_, _ = fmt.Fprintf(os.Stderr, "Api AppKey: %v\n", rootViper.Get(flagApiAppKey))
_, _ = fmt.Fprintf(os.Stderr, "Api Username: %v\n", rootViper.Get(flagApiUsername))
_, _ = fmt.Fprintf(os.Stderr, "Api Password: %v\n", rootViper.Get(flagApiPassword))
_, _ = fmt.Fprintf(os.Stderr, "Api UserAccount: %v\n", rootViper.Get(flagApiUsername))
_, _ = fmt.Fprintf(os.Stderr, "Api UserPassword: %v\n", rootViper.Get(flagApiPassword))
_, _ = fmt.Fprintln(os.Stderr)

_, _ = fmt.Fprintf(os.Stderr, "Git Repo URL: %v\n", rootViper.Get(flagGitRepo))
Expand Down
47 changes: 30 additions & 17 deletions cmd/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"GoSungro/Only"
"GoSungro/iSolarCloud/sungro"
"GoSungro/iSolarCloud/sungro/AppService"
"GoSungro/iSolarCloud/sungro/AppService/login"
"GoSungro/lsgo"
"GoSungro/mmGit"
Expand All @@ -21,6 +20,7 @@ const (
DefaultBinaryName = "GoSungro"
EnvPrefix = "SunGro"
defaultConfigFile = "config.json"
defaultTokenFile = "AuthToken.json"

flagConfigFile = "config"
flagDebug = "debug"
Expand Down Expand Up @@ -70,6 +70,8 @@ type CommandArgs struct {
ApiPassword string
ApiAppKey string
ApiTokenExpiry string
ApiToken string
ApiTokenFile string

// Google sheets
GoogleSheet string
Expand Down Expand Up @@ -117,29 +119,40 @@ func (ca *CommandArgs) ProcessArgs(cmd *cobra.Command, args []string) error {
break
}

auth := login.Request {
Appkey: Cmd.ApiAppKey,
SysCode: "600",
Cmd.Error = SunGro.Login(login.SunGroAuth {
AppKey: ca.ApiAppKey,
UserAccount: ca.ApiUsername,
UserPassword: ca.ApiPassword,
}
// auth := login.SunGroAuth {
// TokenExpiry: ca.ApiTokenExpiry,
// AppKey: ca.ApiAppKey,
// Username: ca.ApiUsername,
// Password: ca.ApiPassword,
// }
hey1 := SunGro.GetEndpoint(AppService.GetAreaName(), "login")
hey1 = hey1.SetRequest(auth)
ca.Error = hey1.GetError()
if ca.Error != nil {
TokenFile: ca.ApiTokenFile,
})
if Cmd.Error != nil {
break
}
r, _ := SunGro.Web.Get(hey1)
fmt.Printf("resp: %v\n", r)

// auth := login.Request {
// Appkey: Cmd.ApiAppKey,
// SysCode: "600",
// UserAccount: ca.ApiUsername,
// UserPassword: ca.ApiPassword,
// }
// // auth := login.SunGroAuth {
// // Expiry: ca.ApiTokenExpiry,
// // AppKey: ca.ApiAppKey,
// // UserAccount: ca.ApiUsername,
// // UserPassword: ca.ApiPassword,
// // }
// hey1 := SunGro.GetEndpoint(AppService.GetAreaName(), "login")
// hey1 = hey1.SetRequest(auth)
// ca.Error = hey1.GetError()
// if ca.Error != nil {
// break
// }
// r, _ := SunGro.ApiRoot.Get(hey1)
// fmt.Printf("resp: %v\n", r)

if SunGro.HasTokenChanged() {
ca.ApiTokenExpiry = SunGro.GetTokenExpiry()
ca.ApiToken = SunGro.GetToken()
ca.Error = writeConfig()
}

Expand Down
2 changes: 1 addition & 1 deletion iSolarCloud/OLD/site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (t *Site) Count(domain string) Count {
Domain: domain,
}

// t.Error = t.Web.Get(t.read, t.read.Request, &t.read.Response)
// t.Error = t.ApiRoot.Get(t.read, t.read.Request, &t.read.Response)
t.count.Error = t.Web.Get(t.count, t.count.Request, &t.count.Response)
if t.count.Error != nil {
break
Expand Down
129 changes: 129 additions & 0 deletions iSolarCloud/api/apiReflect/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package apiReflect


// Old Get method

// func (w *ApiRoot) Get(endpoint api.EndPoint) (api.EndPoint, error) {
// // func (w *ApiRoot) Get(u *url.URL, request interface{}) (interface{}, error) {
// // func (w *ApiRoot) Get(action interface{}) error {
// for range Only.Once {
// if w.Url == nil {
// w.Error = errors.New("SUNGRO API URL is invalid")
// break
// }
//
// request := endpoint.RequestRef()
// w.Error = reflect.VerifyOptionsRequired(request)
// if w.Error != nil {
// break
// }
//
// // objectName, actionName := GetNameOld(action)
// // httpRequest := FindInStruct(action, "Request")
// // httpResponse := FindInStruct(action, "Response")
// //
// // objectName := GetStructName(object)
// // if objectName == "" {
// // w.Error = errors.New("invalid object name to structure")
// // break
// // }
// //
// // actionName := GetStructName(action)
// // if objectName == "" {
// // w.Error = errors.New("invalid action name to structure")
// // break
// // }
// //
// // requestString := Query(httpRequest)
// // if objectName == "" {
// // w.Error = errors.New("invalid httpRequest string for structure")
// // break
// // }
// //
// // responseString := Query(httpResponse)
// // if objectName == "" {
// // w.Error = errors.New("invalid httpResponse string for structure")
// // break
// // }
// //
// // u := fmt.Sprintf("%s?format=json&object=%s&action=%s%s",
// // w.Url.String(),
// // objectName,
// // actionName,
// // queryString,
// // )
//
// p, _ := json.Marshal(request)
//
// // w.httpRequest, w.Error = http.NewRequest("GET", u, nil)
// // if w.Error != nil {
// // break
// // }
// //
// // w.httpRequest.Header.Set("Authorization", w.Auth.GetAuthHeader())
//
// postUrl := fmt.Sprintf("%s%s", w.Url.String(), endpoint.GetUrl())
//
// for range Only.Twice {
// w.httpResponse, w.Error = http.Post(postUrl, "application/json", bytes.NewBuffer(p))
// if w.Error != nil {
// break
// }
//
// if strings.Contains(w.httpResponse.Status, "The access token provided is invalid") {
// // 401 Unauthorized The access token provided is invalid.
// // Will first attempt a refresh of the token OR re-login.
// w.Error = w.Auth.Login(&login.SunGroAuth {
// Expiry: "",
// AppKey: "",
// UserAccount: "",
// UserPassword: "",
// })
// if w.Error != nil {
// w.Error = errors.New(w.httpResponse.Status)
// break
// }
// //w.Error = errors.New(fmt.Sprintf("API httpResponse is %s", w.httpResponse.Status))
// continue
// }
//
// if w.httpResponse.StatusCode == 401 {
// w.Error = errors.New(w.httpResponse.Status)
// break
// }
//
// // All OK.
// break
// }
// //goland:noinspection GoUnhandledErrorResult
// defer w.httpResponse.Body.Close()
// if w.Error != nil {
// break
// }
//
// if w.httpResponse.StatusCode != 200 {
// w.Error = errors.New(fmt.Sprintf("API httpResponse is %s", w.httpResponse.Status))
// break
// }
//
// w.Body, w.Error = ioutil.ReadAll(w.httpResponse.Body)
// if w.Error != nil {
// break
// }
//
// if len(w.Body) == 0 {
// w.Error = errors.New("empty httpResponse")
// break
// }
//
// endpoint = endpoint.SetResponse(w.Body)
// w.Error = endpoint.IsResponseValid()
//
// if w.Error != nil {
// fmt.Printf("ERROR: Body is:\n%s\n", w.Body)
// break
// }
// }
//
// return endpoint, w.Error
// }
Loading

0 comments on commit d6a95ce

Please sign in to comment.