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