From 3bbf82a175f40873a9e152429ccd41b3d31b02ff Mon Sep 17 00:00:00 2001 From: Mika Tuominen Date: Tue, 3 Mar 2020 10:04:55 +0200 Subject: [PATCH] Adding UserByEmail feature --- main.go | 18 ++++++++++++++++-- structs/structs.go | 5 +++++ user.go | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 6825702..3cfe255 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ const ( type Harvest struct { API *structs.API User *User + Users *Users Project string TimeEntries *TimeEntries } @@ -41,6 +42,16 @@ type Entries []structs.Entries // User ... type User structs.User +// type Users structs.UserList +// Users ... +type Users struct { + Users []User `json:"users"` +} + +// type Users []User + +// type Users []structs.User + // Links ... type Links structs.Links @@ -56,6 +67,7 @@ type GetTimeEntriesParams struct { PerPage int8 `url:"per_page"` } +// API is something... type API structs.API // Init methot initializes the data structure needed for Harvest @@ -66,10 +78,12 @@ func Init(conf *config.Config) *Harvest { } e := &TimeEntries{} + // u := &Users{} H := &Harvest{ - API: a, - User: &User{}, + API: a, + User: &User{}, + // Users: u, Project: "", TimeEntries: e, } diff --git a/structs/structs.go b/structs/structs.go index 3b1c4f5..3053732 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -9,6 +9,11 @@ type API struct { BaseURL string } +// UserList is list of all users +type UserList struct { + Users []User `json:"users"` +} + // User is a object of a user fetched from REST API type User struct { ID int64 `json:"id"` diff --git a/user.go b/user.go index b99380f..852e277 100644 --- a/user.go +++ b/user.go @@ -52,3 +52,27 @@ func (h *Harvest) GetUser() (*User, error) { return &user, nil } + +// GetUserByEmail is ... +func (h *Harvest) GetUserByEmail(email string) (User, error) { + var user User + + body, err := h.getURL("GET", usersURL) + if err != nil { + log.Fatalf("[ERROR] Could not get users.") + return user, err + } + + var usersList Users + json.Unmarshal(body, &usersList) + + for _, v := range usersList.Users { + if v.Email == email { + // user = v + return v, nil + } + } + + return user, fmt.Errorf("could not found user") + +}