Skip to content

Commit

Permalink
add /api/user call
Browse files Browse the repository at this point in the history
  • Loading branch information
Strubbl committed Feb 7, 2024
1 parent c45993f commit f56cf04
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Go wrapper library for the [Wallabag](https://github.com/wallabag/wallabag/) API
- [ ] `GET /api/search.{_format}`
- [ ] `GET /api/taggingrule/export.{_format}`
- [x] `GET /api/tags.{_format}`
- [ ] `GET /api/user.{_format}`
- [x] `GET /api/version.{_format}` DEPRECATED by wallabag server 2.4
- [x] `GET /api/user.{_format}`
- [x] `GET /api/version.{_format}` DEPRECATED since wallabag version 2.4

#### POST
- [ ] `POST /api/annotations/{entry}.{_format}`
Expand Down
8 changes: 8 additions & 0 deletions entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (t *WallabagTime) MarshalJSON() ([]byte, error) {
return _json, err
}

// Equal compares the year, month, day, hours, minutes and seconds of the given WallabagTimes
func (t1 *WallabagTime) Equal(t2 *WallabagTime) bool {
if t1.Year() != t2.Year() || t1.Month() != t2.Month() || t1.Day() != t2.Day() || t1.Hour() != t2.Hour() || t1.Minute() != t2.Minute() || t1.Second() != t2.Second() {
return false
}
return true
}

// Links contains four links (self, first, last, next), being part of the Entries object
type Links struct {
Self *Link
Expand Down
26 changes: 26 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package wallabago

import "encoding/json"

// LoggedInUser represents the object being returned from the API request /user
type LoggedInUser struct {
ID int `json:"id"`
UserName string `json:"username"`
Email string `json:"email"`
CreatedAt *WallabagTime `json:"created_at"`
UpdatedAt *WallabagTime `json:"updated_at"`
}

// User returns the user info of the logged in user of the configured wallabag instance
func User(bodyByteGetterFunc BodyByteGetter) (LoggedInUser, error) {
var u LoggedInUser
userJSONByte, err := bodyByteGetterFunc(LibConfig.WallabagURL+"/api/user", "GET", nil)
if err != nil {
return u, err
}
err = json.Unmarshal(userJSONByte, &u)
if err != nil {
return u, err
}
return u, err
}
39 changes: 39 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wallabago

import (
"testing"
"time"
)

func TestUser(t *testing.T) {
expected := LoggedInUser{
ID: 1,
UserName: "john",
Email: "no-reply@example.com",
CreatedAt: &WallabagTime{time.Date(2011, 1, 1, 21, 31, 5, 0, time.FixedZone("+0000", 0))},
UpdatedAt: &WallabagTime{time.Date(2024, 2, 1, 16, 16, 17, 0, time.FixedZone("+0000", 0))},
}
got, _ := User(mockGetBodyOfUserApiCall)
if expected.ID != got.ID {
t.Errorf("expected ID %v but got %v", expected.ID, got.ID)
}
if expected.UserName != got.UserName {
t.Errorf("expected UserName %v but got %v", expected.UserName, got.UserName)
}
if expected.Email != got.Email {
t.Errorf("expected Email %v but got %v", expected.Email, got.Email)
}
if !expected.CreatedAt.Equal(got.CreatedAt) {
t.Errorf("expected CreatedAt:\n%v\nbut got:\n%v", expected.CreatedAt, got.CreatedAt)
}
if !expected.UpdatedAt.Equal(got.UpdatedAt) {
t.Errorf("expected UpdatedAt:\n%v\nbut got:\n%v", expected.UpdatedAt, got.UpdatedAt)
}
if expected.CreatedAt.Equal(got.UpdatedAt) {
t.Errorf("expected CreatedAt:\n%v\nto be different than UpdatedAt but it's equal:\n%v", expected.CreatedAt, got.UpdatedAt)
}
}

func mockGetBodyOfUserApiCall(url string, httpMethod string, postData []byte) ([]byte, error) {
return []byte(`{"id":1,"username":"john","email":"no-reply@example.com","created_at":"2011-01-01T21:31:05+0000","updated_at":"2024-02-01T16:16:17+0000"}`), nil
}

0 comments on commit f56cf04

Please sign in to comment.