authing the app works now, but the string needs to be trimmed

This commit is contained in:
christian 2024-07-19 20:07:07 +02:00
parent ae11219aaf
commit fe6e3d231b

View File

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -74,42 +75,63 @@ func discoverHueBridge() ([]Bridge, error) {
func registerApplication(bridge Bridge) RegisteredApplication { func registerApplication(bridge Bridge) RegisteredApplication {
var applicationName string var applicationName string
var deviceName string var deviceName string
var token string
fmt.Printf("Enter name of your application: ") fmt.Printf("Enter name of your application: ")
fmt.Scan(&applicationName) fmt.Scan(&applicationName)
fmt.Printf("Enter name of your device: ") fmt.Printf("Enter name of your device: ")
fmt.Scan(&deviceName) fmt.Scan(&deviceName)
resp, err := http.Post(
"http://"+bridge.IP.String()+"/api", for {
"application/json", resp, err := http.Post(
bytes.NewBuffer([]byte(`{"devicetype":"`+applicationName+`#`+deviceName+`"}`)))
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
// if body has link button not pressed, tell user to press it then send the request again to register the application
if strings.Contains(string(body), "link button not pressed") {
fmt.Println("Link button not pressed. Press link button on bridge and press anter to continue.")
bufio.NewReader(os.Stdin).ReadBytes('\n')
resp.Body.Close()
http.Post(
"http://"+bridge.IP.String()+"/api", "http://"+bridge.IP.String()+"/api",
"application/json", "application/json",
bytes.NewBuffer([]byte(`{"devicetype":"`+applicationName+`#`+deviceName+`"}`))) bytes.NewBuffer([]byte(`{"devicetype":"`+applicationName+`#`+deviceName+`"}`)))
body, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
type Success struct {
Username string `json:"username"`
}
if !strings.Contains(string(body), "link button not pressed") {
// Define a struct matching the expected JSON response structure
type SuccessResponse struct {
Success struct {
Username string `json:"username"`
} `json:"success"`
}
// Unmarshal the response body into the struct
var successResponse []SuccessResponse
if err := json.Unmarshal(body, &successResponse); err != nil {
log.Fatalf("Error parsing response: %v", err)
}
// Assign the username to the token
if len(successResponse) > 0 {
token = successResponse[0].Success.Username
}
break
}
fmt.Println("Link button not pressed. Press link button on bridge and press Enter to continue.")
bufio.NewReader(os.Stdin).ReadBytes('\n')
} }
registeredApplication := RegisteredApplication{ registeredApplication := RegisteredApplication{
bridge: bridge, bridge: bridge,
applicationName: applicationName, applicationName: applicationName,
deviceName: deviceName, deviceName: deviceName,
token: string(body)} token: token,
resp.Body.Close() }
return registeredApplication return registeredApplication
} }