Skip to content

Commit

Permalink
feat: Add interface methods to User object
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzolino committed Mar 18, 2022
1 parent 42681d5 commit eab200d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
16 changes: 8 additions & 8 deletions examples/me/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func main() {
fmt.Printf("Email: %s\nUsername: %s\nName: %s\n", user.Email, user.Username, user.Name)

// for each home: get home info and print name and address
// for _, userHome := range user.Homes {
// home, err := gotado.GetHome(client, &userHome)
// if err != nil {
// fmt.Fprintf(os.Stderr, "Failed to get user home info: %v\n", err)
// os.Exit(1)
// }
// fmt.Printf("Home: %s\nAddress:\n%s\n%s %s\n", home.Name, *home.Address.AddressLine1, *home.Address.ZipCode, *home.Address.City)
// }
for _, userHome := range user.Homes {
home, err := user.GetHome(ctx, userHome.Name)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get user home info: %v\n", err)
os.Exit(1)
}
fmt.Printf("Home: %s\nAddress:\n%s\n%s %s\n", home.Name, *home.Address.AddressLine1, *home.Address.ZipCode, *home.Address.City)
}
}
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (

// Home represents a home equipped with tado°
type Home struct {
client *client
ID int32 `json:"id"`
Name string `json:"name"`
DateTimeZone string `json:"dateTimeZone"`
Expand Down
24 changes: 24 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gotado

import (
"context"
"fmt"
)

func (u *User) GetHome(ctx context.Context, name string) (*Home, error) {
var homeID int32
for _, h := range u.Homes {
if h.Name == name {
homeID = h.ID
}
}
if homeID == 0 {
return nil, fmt.Errorf("unknown home name '%s'", name)
}

home := &Home{client: u.client}
if err := u.client.get(ctx, apiURL("homes/%d", homeID), home); err != nil {
return nil, err
}
return home, nil
}

0 comments on commit eab200d

Please sign in to comment.