-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
58 lines (46 loc) · 1.05 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"neopixel"
"net/http"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %v <endpoint> <arg>", os.Args[0])
os.Exit(1)
}
endpoint := os.Args[1]
var hostport string
hostport = "localhost:8080"
url := "http://" + hostport + "/" + endpoint
log.Println("URL:>", url)
var jsonStr []byte
switch endpoint {
case "setcolor":
command := new(neopixel.Color)
command.ColorName = os.Args[2]
jsonStr, _ = json.Marshal(command)
case "action":
command := new(neopixel.Action)
command.Do = os.Args[2]
jsonStr, _ = json.Marshal(command)
default:
command := new(neopixel.Color)
command.ColorName = "off"
jsonStr, _ = json.Marshal(command)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("X-Custom-Header", "lampcontrol")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
}