Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
single http method
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Jul 15, 2019
1 parent 160497b commit 375b72b
Show file tree
Hide file tree
Showing 19 changed files with 33 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmd/apis/crtapi/crtapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var Cmd = &cobra.Command{
}
} else {
proxyName := "{\"name\":\"" + name + "\"}"
shared.PostHttpClient(u.String(), proxyName)
shared.HttpClient(u.String(), proxyName)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/apis/depapi/depapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/apis/listapis/listapis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/apis/listdeploy/listdeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/apps/getapp/getapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/apps/listapp/listapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{
q.Set("row", count)
}
u.RawQuery = q.Encode()
shared.GetHttpClient(u.String())
shared.HttpClient(u.String())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/developers/getdev/getdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/developers/listdev/listdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{
q.Set("count", count)
}
u.RawQuery = q.Encode()
shared.GetHttpClient(u.String())
shared.HttpClient(u.String())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/env/getenv/getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/env/listenv/listenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/org/getorg/getorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/org/listorgs/listorgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/products/getprod/getprod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/products/listproducts/listproducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var Cmd = &cobra.Command{
q.Set("count", count)
}
u.RawQuery = q.Encode()
shared.GetHttpClient(u.String())
shared.HttpClient(u.String())
},
}

Expand Down
56 changes: 15 additions & 41 deletions cmd/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -209,7 +183,7 @@ func PostHttpClient(url string, payload string) error {
fmt.Println(string(prettyJSON.Bytes()))
return nil
}
}
}
}

func getPrivateKey() (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/sharedflows/getsf/getsf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sharedflows/listsf/listsf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sync/getsync/getsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sync/setsync/setsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
}

Expand Down

0 comments on commit 375b72b

Please sign in to comment.