Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go/golang example code #399

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
+ [C# example code](#c-example-code)
+ [Java 11+ example code](#java-11-example-code)
+ [PHP example code](#php-example-code)
+ [Swift example code](#swift-example-code)
+ [Go example code](#go-example-code)
- [Glossary](#glossary)
- [API Usage](#api-usage)
* [Host Domain](#host-domain)
Expand Down Expand Up @@ -590,6 +592,69 @@ func getDevices() async throws -> Any {
}
```

#### Go example code

```go
package main

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"

"github.com/google/uuid"
)

func main() {
token := "XXXXXXXXXXXXXXXXXXX" // copy and paste from the SwitchBot app V6.14 or later
secret := "YYYYYYYYYYY" // copy and paste from the SwitchBot app V6.14 or later
nonce := uuid.NewString() // generate random UUID v4
timestamp := time.Now().UnixMilli() // 13-digit milliseconds Unix timestamp

data := fmt.Sprintf("%s%d%s", token, timestamp, nonce)

mac := hmac.New(sha256.New, []byte(secret))
if _, err := mac.Write([]byte(data)); err != nil {
log.Fatalf("Error generating signature: %v", err)
}

signature := mac.Sum(nil)
signatureB64 := strings.ToUpper(base64.StdEncoding.EncodeToString(signature))

url := "https://api.switch-bot.com/v1.1/devices"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("Error creating request: %v", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", token)
req.Header.Set("sign", signatureB64)
req.Header.Set("nonce", nonce)
req.Header.Set("t", fmt.Sprintf("%d", timestamp))

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response: %v", err)
}

fmt.Println(string(body)) // Response in JSON format
}
```

## Glossary

The following table provides definitions to the terms to be frequently mentioned in the subsequent sections.
Expand Down