Skip to content

Commit

Permalink
Merge pull request #37 from hbagdi/user-statistics
Browse files Browse the repository at this point in the history
Add GET /users/:id/statistics
  • Loading branch information
hbagdi authored Oct 22, 2017
2 parents ea39a37 + 26f2aac commit cd8b3a5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
44 changes: 44 additions & 0 deletions unsplash/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,47 @@ func (u *UserUpdateInfo) String() string {

return buf.String()
}

// UserStatistics represents statistics like downloads, views and likes of an unsplash user
type UserStatistics struct {
Username string `json:"username"`
Downloads struct {
Total int `json:"total"`
Historical struct {
Change int `json:"change"`
Average int `json:"average"`
Resolution string `json:"resolution"`
Quantity int `json:"quantity"`
Values []struct {
Date string `json:"date"`
Value int `json:"value"`
} `json:"values"`
} `json:"historical"`
} `json:"downloads"`
Views struct {
Total int `json:"total"`
Historical struct {
Change int `json:"change"`
Average int `json:"average"`
Resolution string `json:"resolution"`
Quantity int `json:"quantity"`
Values []struct {
Date string `json:"date"`
Value int `json:"value"`
} `json:"values"`
} `json:"historical"`
} `json:"views"`
Likes struct {
Total int `json:"total"`
Historical struct {
Change int `json:"change"`
Average int `json:"average"`
Resolution string `json:"resolution"`
Quantity int `json:"quantity"`
Values []struct {
Date string `json:"date"`
Value int `json:"value"`
} `json:"values"`
} `json:"historical"`
} `json:"likes"`
}
29 changes: 29 additions & 0 deletions unsplash/users_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,32 @@ func (us *UsersService) Collections(username string, opt *ListOpt) (*[]Collectio
endpoint := fmt.Sprintf("%v/%v/%v", getEndpoint(users), username, getEndpoint(collections))
return s.getCollections(opt, endpoint)
}

// Statistics return a stats about a photo with id.
func (us *UsersService) Statistics(username string, opt *StatsOpt) (*UserStatistics, *Response, error) {
if "" == username {
return nil, nil, &IllegalArgumentError{ErrString: "Photo ID cannot be null"}
}
if opt == nil {
opt = defaultStatsOpt
}
if !opt.Valid() {
return nil, nil, &InvalidStatsOptError{ErrString: "opt provided is not valid."}
}
endpoint := fmt.Sprintf("%v/%v/statistics", getEndpoint(users), username)
req, err := newRequest(GET, endpoint, opt, nil)
if err != nil {
return nil, nil, err
}
cli := (service)(*us)
resp, err := cli.do(req)
if err != nil {
return nil, nil, err
}
var stats UserStatistics
err = json.Unmarshal(*resp.body, &stats)
if err != nil {
return nil, nil, err
}
return &stats, resp, nil
}
31 changes: 31 additions & 0 deletions unsplash/users_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ func TestUserPortfolio(T *testing.T) {
assert.Equal(true, ok)
}

func TestUserStatistics(T *testing.T) {
assert := assert.New(T)
log.SetOutput(ioutil.Discard)
unsplash := setup()
stats, resp, err := unsplash.Users.Statistics("lukechesser", nil)
assert.Nil(err)
assert.NotNil(stats)
assert.NotNil(resp)
assert.NotNil(30, stats.Downloads.Historical.Quantity)
log.Println(stats)

stats, resp, err = unsplash.Users.Statistics("lukechesser", &StatsOpt{Quantity: 10})
assert.Nil(err)
assert.NotNil(stats)
assert.NotNil(resp)
assert.NotNil(30, stats.Downloads.Historical.Quantity)
log.Println(stats)

stats, resp, err = unsplash.Users.Statistics("lukechesser", &StatsOpt{Resolution: "sd"})
assert.NotNil(err)
assert.Nil(resp)

stats, resp, err = unsplash.Users.Statistics("lukechesser", &StatsOpt{Quantity: 31})
assert.NotNil(err)
assert.Nil(resp)

stats, resp, err = unsplash.Users.Statistics("", nil)
assert.Nil(stats)
assert.Nil(resp)
assert.NotNil(err)
}
func TestLikedPhotos(T *testing.T) {
assert := assert.New(T)
log.SetOutput(ioutil.Discard)
Expand Down

0 comments on commit cd8b3a5

Please sign in to comment.