diff --git a/config.go b/config.go index 83ae19d..43120b1 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "gopkg.in/yaml.v2" "io/ioutil" "net/http" ) @@ -15,26 +16,27 @@ type Config struct { BridgeAddrScheme string `yaml:bridgeAddressScheme` } -// createNewUser will create a new user. This should be called only of there's none in the yaml config. -func (c *Config) createNewUser() { - // TODO: read/create the url - url := "http://192.168.178.46/api" - - var reqBody = []byte(`{"devicetype": "go-hue-interface#Philips hue"}`) - req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody)) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - panic(err) +// TODO: Rename if this will be placed in a seperate package +// ReadConfig ... +func ReadConfig(path string) (conf *Config, err error) { + f, err := ioutil.ReadFile(path) + if err != nil { + return + } + + err = yaml.Unmarshal(f, conf) + if err != nil { + return } - defer resp.Body.Close() - fmt.Println("response Status:", resp.Status) - fmt.Println("response Headers:", resp.Header) - body, _ := ioutil.ReadAll(resp.Body) - fmt.Println("response Body:", string(body)) + // TODO: check wether a user is already created and if not create one. - // json.Unmarshal([]byte(str), &resp) - fmt.Println(resp) + return } + +func (c *Config) WriteConfig(path string) (err error) { + b, err := yaml.Marshal(c) + + err = ioutil.WriteFile(path, b, 0644) + return +} \ No newline at end of file diff --git a/user.go b/user.go new file mode 100644 index 0000000..5351502 --- /dev/null +++ b/user.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "bytes" + "fmt" + "net/http" +) + +type User struct { + username string + // TODO: + //key string +} + +// CreateNewUser will create a new user. This should be called only of there's none in the yaml config. +func CreateUser(addr string) (name, key string, succ bool) { + +} + +// TODO: remove these comments +// example application: "go.hue.interface" +// example deviceName: "Philips hue" +func CreateUserExtended(addr, application, deviceName string) (u *User, err error) { + uri := "http://" + addr + "/api" + + var reqBody = []byte(fmt.Sprintf(`{"devicetype": "%s#%s"}`, application, deviceName)) + req, err := http.NewRequest("POST", uri, bytes.NewBuffer(reqBody)) + + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + return + } + defer res.Body.Close() + + // Unmarshal data + err = json.NewDecoder(res.Body).Decode(u) + if err != nil { + return + } + + return +} \ No newline at end of file