diff --git a/cmd/apis/crtapi/crtapi.go b/cmd/apis/crtapi/crtapi.go index e6603e7b..3f10282e 100644 --- a/cmd/apis/crtapi/crtapi.go +++ b/cmd/apis/crtapi/crtapi.go @@ -30,7 +30,7 @@ var Cmd = &cobra.Command{ } } else { proxyName := "{\"name\":\"" + name + "\"}" - shared.PostHttpClient(u.String(), proxyName) + shared.HttpClient(u.String(), proxyName) } }, } diff --git a/cmd/apis/depapi/depapi.go b/cmd/apis/depapi/depapi.go index f2c09fca..c5bca4a2 100644 --- a/cmd/apis/depapi/depapi.go +++ b/cmd/apis/depapi/depapi.go @@ -19,7 +19,7 @@ var Cmd = &cobra.Command{ u.RawQuery = q.Encode() } u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Org, "apis", name, "revisions", revision,"deployments") - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/apis/listapis/listapis.go b/cmd/apis/listapis/listapis.go index dc2762ac..7ec465dd 100644 --- a/cmd/apis/listapis/listapis.go +++ b/cmd/apis/listapis/listapis.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "apis") - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/apis/listdeploy/listdeploy.go b/cmd/apis/listdeploy/listdeploy.go index ab1059c4..e276735c 100644 --- a/cmd/apis/listdeploy/listdeploy.go +++ b/cmd/apis/listdeploy/listdeploy.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "apis", name, "deployments") - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/apps/getapp/getapp.go b/cmd/apps/getapp/getapp.go index 68360150..68457e66 100644 --- a/cmd/apps/getapp/getapp.go +++ b/cmd/apps/getapp/getapp.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "apps", name) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/apps/listapp/listapp.go b/cmd/apps/listapp/listapp.go index 371c798e..319df718 100644 --- a/cmd/apps/listapp/listapp.go +++ b/cmd/apps/listapp/listapp.go @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{ q.Set("row", count) } u.RawQuery = q.Encode() - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/developers/getdev/getdev.go b/cmd/developers/getdev/getdev.go index b1a960ec..b3779413 100644 --- a/cmd/developers/getdev/getdev.go +++ b/cmd/developers/getdev/getdev.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "developers", name) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/developers/listdev/listdev.go b/cmd/developers/listdev/listdev.go index 2e265f3d..1a8691ae 100644 --- a/cmd/developers/listdev/listdev.go +++ b/cmd/developers/listdev/listdev.go @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{ q.Set("count", count) } u.RawQuery = q.Encode() - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/env/getenv/getenv.go b/cmd/env/getenv/getenv.go index 649f7228..75fe48b6 100644 --- a/cmd/env/getenv/getenv.go +++ b/cmd/env/getenv/getenv.go @@ -19,7 +19,7 @@ var Cmd = &cobra.Command{ } else { u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments", shared.RootArgs.Env) } - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/env/listenv/listenv.go b/cmd/env/listenv/listenv.go index 67d2e502..8ec12f83 100644 --- a/cmd/env/listenv/listenv.go +++ b/cmd/env/listenv/listenv.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "environments") - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/org/getorg/getorg.go b/cmd/org/getorg/getorg.go index 734afb87..9f7803ac 100644 --- a/cmd/org/getorg/getorg.go +++ b/cmd/org/getorg/getorg.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/org/listorgs/listorgs.go b/cmd/org/listorgs/listorgs.go index eea12cd7..9dad868d 100644 --- a/cmd/org/listorgs/listorgs.go +++ b/cmd/org/listorgs/listorgs.go @@ -12,7 +12,7 @@ var Cmd = &cobra.Command{ Long: "List the Apigee organizations, and the related projects that a user has permissions for", Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/products/getprod/getprod.go b/cmd/products/getprod/getprod.go index 3c4847d0..1a294d46 100644 --- a/cmd/products/getprod/getprod.go +++ b/cmd/products/getprod/getprod.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "apiproducts", name) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/products/listproducts/listproducts.go b/cmd/products/listproducts/listproducts.go index d0bc799e..c6621267 100644 --- a/cmd/products/listproducts/listproducts.go +++ b/cmd/products/listproducts/listproducts.go @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{ q.Set("count", count) } u.RawQuery = q.Encode() - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/shared/shared.go b/cmd/shared/shared.go index bc9db72c..923ccb8d 100644 --- a/cmd/shared/shared.go +++ b/cmd/shared/shared.go @@ -82,42 +82,6 @@ func Init() { log.Ldate|log.Ltime|log.Lshortfile) } -func GetHttpClient(url string) error { - client := &http.Client{} - - Info.Println("Connecting to : ", url) - req, err := http.NewRequest("GET", url, nil) - - Info.Println("Setting token : ", RootArgs.Token) - req.Header.Add("Authorization", "Bearer "+ RootArgs.Token) - - resp, err := client.Do(req) - - if err != nil { - Error.Fatalln("Error connecting:\n", err) - return err - } else { - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - Error.Fatalln("Error in response:\n", err) - return err - } else if resp.StatusCode != 200 { - Error.Fatalln("Error in response:\n", string(body)) - return errors.New("Error in response") - } else { - var prettyJSON bytes.Buffer - err = json.Indent(&prettyJSON, body, "", "\t") - if err != nil { - Error.Fatalln("Error parsing response:\n", err) - return err - } - fmt.Println(string(prettyJSON.Bytes())) - return nil - } - } -} - func PostHttpOctet(url string, proxyName string) error { file, _ := os.Open(proxyName) @@ -176,15 +140,25 @@ func PostHttpOctet(url string, proxyName string) error { } } -func PostHttpClient(url string, payload string) error { +func HttpClient(params ...string) error { + + var req *http.Request + var err error + client := &http.Client{} + Info.Println("Connecting to : ", params[0]) - Info.Println("Connecting to : ", url) - req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(payload))) + if len(params) == 2 { + req, err = http.NewRequest("POST", params[0], bytes.NewBuffer([]byte(params[1]))) + } else if len(params) == 1 { + req, err = http.NewRequest("GET", params[0], nil) + } else { + return errors.New("Incorrect parameters to invoke the method") + } Info.Println("Setting token : ", RootArgs.Token) req.Header.Add("Authorization", "Bearer "+ RootArgs.Token) - req.Header.Add("Content-Type", "application/json") + resp, err := client.Do(req) if err != nil { @@ -209,7 +183,7 @@ func PostHttpClient(url string, payload string) error { fmt.Println(string(prettyJSON.Bytes())) return nil } - } + } } func getPrivateKey() (interface{}, error) { diff --git a/cmd/sharedflows/getsf/getsf.go b/cmd/sharedflows/getsf/getsf.go index d6f9235b..e2c398e5 100644 --- a/cmd/sharedflows/getsf/getsf.go +++ b/cmd/sharedflows/getsf/getsf.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "sharedflows", name) - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/sharedflows/listsf/listsf.go b/cmd/sharedflows/listsf/listsf.go index d4e2aad2..25e3ec43 100644 --- a/cmd/sharedflows/listsf/listsf.go +++ b/cmd/sharedflows/listsf/listsf.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org, "sharedflows") - shared.GetHttpClient(u.String()) + shared.HttpClient(u.String()) }, } diff --git a/cmd/sync/getsync/getsync.go b/cmd/sync/getsync/getsync.go index b8d2cfa5..9a28309a 100644 --- a/cmd/sync/getsync/getsync.go +++ b/cmd/sync/getsync/getsync.go @@ -14,7 +14,7 @@ var Cmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { u, _ := url.Parse(shared.BaseURL) u.Path = path.Join(u.Path, shared.RootArgs.Org+":getSyncAuthorization") - shared.PostHttpClient(u.String(), "") + shared.HttpClient(u.String(), "") }, } diff --git a/cmd/sync/setsync/setsync.go b/cmd/sync/setsync/setsync.go index 0aa5b2d3..66b6f38a 100644 --- a/cmd/sync/setsync/setsync.go +++ b/cmd/sync/setsync/setsync.go @@ -37,7 +37,7 @@ var Cmd = &cobra.Command{ identities.Identities = append(identities.Identities, identity) payload, _ := json.Marshal(&identities) fmt.Println(string(payload)) - shared.PostHttpClient(u.String(), string(payload)) + shared.HttpClient(u.String(), string(payload)) }, }