github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/api.go (about) 1 package main 2 3 import ( 4 "io" 5 "os" 6 "strings" 7 ) 8 9 var cmdAPI = &Command{ 10 Run: runAPI, 11 Usage: "api <method> <path>", 12 Category: "hk", 13 Short: "make a single API request" + extra, 14 Long: ` 15 The api command is a convenient but low-level way to send requests 16 to the Heroku API. It sends an HTTP request to the Heroku API 17 using the given method on the given path. For methods PUT, PATCH, 18 and POST, it uses stdin unmodified as the request body. It prints 19 the response unmodified on stdout. 20 21 Method name input will be upcased, so both 'hk api GET /apps' and 22 'hk api get /apps' are valid commands. 23 24 As with any hk command, the behavior of hk api is controlled by 25 various environment variables. See 'hk help environ' for details. 26 27 Examples: 28 29 $ hk api GET /apps/myapp | jq . 30 { 31 "name": "myapp", 32 "id": "app123@heroku.com", 33 "created_at": "2011-11-11T04:17:13-00:00", 34 … 35 } 36 37 $ export HKHEADER 38 $ HKHEADER=' 39 Content-Type: application/x-www-form-urlencoded 40 Accept: application/json 41 ' 42 $ printf 'type=web&qty=2' | hk api POST /apps/myapp/ps/scale 43 2 44 `, 45 } 46 47 func runAPI(cmd *Command, args []string) { 48 if len(args) != 2 { 49 cmd.PrintUsage() 50 os.Exit(2) 51 } 52 method := strings.ToUpper(args[0]) 53 var body io.Reader 54 if method == "PATCH" || method == "PUT" || method == "POST" { 55 body = os.Stdin 56 } 57 if err := client.APIReq(os.Stdout, method, args[1], body); err != nil { 58 printFatal(err.Error()) 59 } 60 }