diff --git a/api/v1/album.go b/api/v1/album.go index 74d4cbc..d418ff9 100644 --- a/api/v1/album.go +++ b/api/v1/album.go @@ -1,6 +1,54 @@ package api +type AlbumOptions struct { + ParamType string + Q string + Count int + Offset int + Category string +} + +const ( + DefaultAlbumOptionsParamType = "album" + DefaultAlbumOptionsQ = "" + DefaultAlbumOptionsCount = 24 + DefaultAlbumOptionsOffset = 0 + DefaultAlbumOptionsCategory = "" +) + // GET /api/v1/albums +// +// Example Params: http://localhost:8080/api/v1/albums?count=24&offset=0&q=&category=&type=album +func (v1 *V1Client) GetAlbums(options *AlbumOptions) ([]Album, error) { + albums := []Album{{}} + + if options == nil { + + // Default to sane options for query + options = &AlbumOptions{ + ParamType: "album", + Q: "", + Count: 24, + Offset: 0, + Category: "", + } + } + + // Checks for missing fields + if options.Count == 0 { + return albums, nil + } + if options.ParamType == "" { + options.ParamType = DefaultAlbumOptionsParamType + } + + // NOTE: Even though this method is singular GetAlbum + // if will call the "albums" plural endpoint. + err := v1.GET("/api/v1/albums?count=%d&offset=%d&q=%s&category=%s&type=%s", options.Count, options.Offset, options.Q, options.Category, options.ParamType).JSON(&albums) + return albums, err +} + +// GET /api/v1/albums/:uuid func (v1 *V1Client) GetAlbum(uuid string) (Album, error) { album := Album{} @@ -8,4 +56,4 @@ func (v1 *V1Client) GetAlbum(uuid string) (Album, error) { // if will call the "albums" plural endpoint. err := v1.GET("/api/v1/albums/%s", uuid).JSON(&album) return album, err -} \ No newline at end of file +} diff --git a/examples/album.go b/examples/album.go index 6457136..63cd4bd 100644 --- a/examples/album.go +++ b/examples/album.go @@ -2,6 +2,7 @@ package main import ( "fmt" + photoprism "github.com/kris-nova/client-go" "github.com/kris-nova/logger" ) @@ -27,6 +28,17 @@ func main() { logger.Always("Logged in...") album, err := client.V1().GetAlbum(uuid) + if err != nil { + halt(3, "Error getting album %s", uuid) + } fmt.Println(album) + albums, err := client.V1().GetAlbums(nil) + if err != nil { + halt(2, "Error listing albums: %v", err) + } + for _, album := range albums { + fmt.Println(album) + } + } diff --git a/internal/api/album.go b/internal/api/album.go index 5ae3f86..02abd18 100644 --- a/internal/api/album.go +++ b/internal/api/album.go @@ -40,7 +40,7 @@ func SaveAlbumAsYaml(a entity.Album) { } } -// GET /api/v1/albums/:uuid +// GET /api/v1/albums func GetAlbums(router *gin.RouterGroup) { router.GET("/albums", func(c *gin.Context) { s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionSearch) diff --git a/sample-app/photoprism/storage/cache/sessions.json b/sample-app/photoprism/storage/cache/sessions.json index 3de5ce1..8e19330 100644 --- a/sample-app/photoprism/storage/cache/sessions.json +++ b/sample-app/photoprism/storage/cache/sessions.json @@ -64,6 +64,11 @@ "tokens": null, "expiration": 1613522078076464014 }, + "1c2319c0a31690d8c0d9c5bb857ce4b94f7bef2e3289ae4a": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685372068695142 + }, "1df4c9db0a9092076596fb78727d069689c4b3083372a1fe": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -119,6 +124,11 @@ "tokens": null, "expiration": 1613528491291754429 }, + "35ea7856b7147b81ebe396145fbdabb4fa6ce49b7834f4da": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613686777776704897 + }, "370c05ea9a50f1e6befe3eba0e59e1a965daaa31da6f028a": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -144,6 +154,11 @@ "tokens": null, "expiration": 1613526857034102326 }, + "40e83ce294fc2dce5fe1e17cee5cf9422a83d85d61a6e6ba": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613686103907054752 + }, "41a99f15500d1eca9818bc3e0cae7cca319cd6eb2b38bb8d": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -174,6 +189,11 @@ "tokens": null, "expiration": 1613509579377741749 }, + "48d794607621adc21621196a96fe29b631f18d1f15b0e0e3": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685352396869309 + }, "4b390db50d53378cfd6e3e3b08aa2a02b1df8fdcf1bda5c5": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -209,6 +229,11 @@ "tokens": null, "expiration": 1613502564441892628 }, + "5d748cc67c9c4efec35d910f799113b793a9fc47f9d88bd5": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613686778812288997 + }, "6196c51bd2db97cc8aeaaac53bef1c6400c50774fedc97b5": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -239,16 +264,31 @@ "tokens": null, "expiration": 1613526444602833378 }, + "7bd83ada99d8ef2ddcb389fc9914dc4c9b3bf7596c529b38": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613686726271559883 + }, "7d381eaaea551483d8d50aa39007d7fc9fdddd3b6a06359d": { "user": "uqnzie01i1nypnt9", "tokens": null, "expiration": 1613513949594487106 }, + "81000ef1dad0e84ef593d3838fb4ccb28a409d60cfaa7ef7": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685000623682231 + }, "8173f5cece02b1a41e9bc937bbbbc80960b838d93cef7f2d": { "user": "uqnzie01i1nypnt9", "tokens": null, "expiration": 1613526315102874135 }, + "8223d8743b700e5c507ff37630f02de3c80dfeb8365b4feb": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685493224019727 + }, "82b0a1bdd1ac266855b338513eff6631c000f7187f1e099d": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -269,6 +309,11 @@ "tokens": null, "expiration": 1613519263888623481 }, + "88ba10c295a2e1e433d5b846ac5aea7a8fc1b35f90b97992": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685115509915196 + }, "8b98cdc875e6a849a9ab77ee44abef39bab4311dc7d19493": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -424,6 +469,11 @@ "tokens": null, "expiration": 1613513847101926534 }, + "cf5e23f28813fe367d3e1a5de1fc066b76988cae3d14343d": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613686725223640044 + }, "d3f79b180ecda15ef16859e1d822e29bb5c801438a8b3ac1": { "user": "uqnzie01i1nypnt9", "tokens": null, @@ -459,6 +509,11 @@ "tokens": null, "expiration": 1613526269423740413 }, + "e98af3258ddfead5fc4ed76278bada0fed9839c111a80b86": { + "user": "uqnzie01i1nypnt9", + "tokens": null, + "expiration": 1613685151568885713 + }, "eb57053b27b37cdca1d7829972d0af55f329dad18a7e3c96": { "user": "uqnzie01i1nypnt9", "tokens": null, diff --git a/sample-app/photoprism/storage/index.db b/sample-app/photoprism/storage/index.db index eaab8c4..776fabe 100644 Binary files a/sample-app/photoprism/storage/index.db and b/sample-app/photoprism/storage/index.db differ diff --git a/sample-app/photoprism/storage/sidecar/2021/02/20210204_031706_5B740007.yml b/sample-app/photoprism/storage/sidecar/2021/02/20210204_031706_5B740007.yml index 17d29b5..ec2f150 100755 --- a/sample-app/photoprism/storage/sidecar/2021/02/20210204_031706_5B740007.yml +++ b/sample-app/photoprism/storage/sidecar/2021/02/20210204_031706_5B740007.yml @@ -3,7 +3,7 @@ UID: pqnzigq351j2fqgn Type: image Title: A really great photo! TitleSrc: manual -Description: 'Sample App Description: 2021-02-11 13:45:52.564527024 -0800 PST m=+1.150507976' +Description: 'Sample App Description: 2021-02-11 14:19:37.7961079 -0800 PST m=+6.239338909' DescriptionSrc: manual OriginalName: IMG_3044 Year: -1 @@ -13,5 +13,5 @@ Details: Keywords: green, tambourine KeywordsSrc: manual CreatedAt: 2021-02-04T03:17:14.613092062Z -UpdatedAt: 2021-02-11T21:45:52.568675691Z -EditedAt: 2021-02-11T21:45:53Z +UpdatedAt: 2021-02-11T22:19:37.804340087Z +EditedAt: 2021-02-11T22:19:38Z diff --git a/test/album_test.go b/test/album_test.go index 82d460f..d3b7f81 100644 --- a/test/album_test.go +++ b/test/album_test.go @@ -2,6 +2,8 @@ package test import ( "testing" + + "github.com/kris-nova/client-go/api/v1" ) func TestHappyGetAlbum(t *testing.T) { @@ -21,3 +23,45 @@ func TestSadGetAlbum(t *testing.T) { t.Errorf("expected error for unknown album: %d", album.ID) t.FailNow() } + +func TestHappyGetAlbumsOptionsCount1(t *testing.T) { + options := api.AlbumOptions{ + Count: 1, + } + albums, err := Client.V1().GetAlbums(&options) + if err != nil { + t.Errorf("expected success listing 1 album: %v", err) + t.FailNow() + } + if len(albums) != 1 { + t.Errorf("expected 1 album length, got: %d", len(albums)) + t.FailNow() + } +} + +func TestHappyGetAlbumsNil(t *testing.T) { + albums, err := Client.V1().GetAlbums(nil) + if err != nil { + t.Errorf("expected success listing albums: %v", err) + t.FailNow() + } + t.Logf("Listed %d albums", len(albums)) +} + +func TestSadGetAlbums(t *testing.T) { + options := api.AlbumOptions{ + Category: UnknownCategory, + } + albums, err := Client.V1().GetAlbums(&options) + if err != nil { + t.Errorf("error listing albums: %v", err) + t.FailNow() + return + } + + // Note: by defualt we return "{}" which counts as 1 album + if len(albums) != 1 { + t.Errorf("Non zero length of albums") + t.FailNow() + } +} diff --git a/test/main_test.go b/test/main_test.go index c8ca89f..6bcc7b4 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -21,6 +21,7 @@ const ( WellKnownAlbumID = "aqnzih81icziiyae" UnknownAlbumID = "1234567890" WellKnownSampleAppConnectionString = "http://localhost:8080" + UnknownCategory = "Furries" ) // Client is a pre-authenticated client that can be used