From 06fff46e43550fa81949520c909279a3d5defe4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kris=20N=C3=B3va?= Date: Thu, 11 Feb 2021 14:21:44 -0800 Subject: [PATCH] Adding GetAlbums with AlbumOptions and default values, and unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kris NĂ³va --- api/v1/album.go | 50 +++++++++++++++- examples/album.go | 12 ++++ internal/api/album.go | 2 +- .../photoprism/storage/cache/sessions.json | 55 ++++++++++++++++++ sample-app/photoprism/storage/index.db | Bin 425984 -> 425984 bytes .../2021/02/20210204_031706_5B740007.yml | 6 +- test/album_test.go | 44 ++++++++++++++ test/main_test.go | 1 + 8 files changed, 165 insertions(+), 5 deletions(-) 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 eaab8c448b372a3cccdbd3dc4147040a2e286c89..776fabe656ce5aaae95b304f256584296c894b2a 100644 GIT binary patch delta 256 zcmZo@kZNdiWulBTz zxqZq`mV8EGzMIV=j7AK+_V⪚=JaPjjEE2b4+8+=H%c4; delta 260 zcmZo@kZNdiaH5Pe;tqF`uZv_plOiZmzjr2^-jZG~rOe{>>C%1#Wz zxqZq`mV8FRW-&%123~u6UR`lsbIC?c$>|sOvPe$X-_0`7z!+w;nTe^9xq*?1g06vu zfq_CmaEL;#t+t__p{aqXfw`r*S#DxpX=2XwhwoYBr}OS%anpo5+RVbt+|