Adding GetAlbums with AlbumOptions and default values, and unit tests

Signed-off-by: Kris Nóva <kris@nivenly.com>
main
Kris Nóva 2021-02-11 14:21:44 -08:00
parent 6b742a1db4
commit 06fff46e43
8 changed files with 165 additions and 5 deletions

View File

@ -1,6 +1,54 @@
package api 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 // 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) { func (v1 *V1Client) GetAlbum(uuid string) (Album, error) {
album := Album{} album := Album{}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
photoprism "github.com/kris-nova/client-go" photoprism "github.com/kris-nova/client-go"
"github.com/kris-nova/logger" "github.com/kris-nova/logger"
) )
@ -27,6 +28,17 @@ func main() {
logger.Always("Logged in...") logger.Always("Logged in...")
album, err := client.V1().GetAlbum(uuid) album, err := client.V1().GetAlbum(uuid)
if err != nil {
halt(3, "Error getting album %s", uuid)
}
fmt.Println(album) 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)
}
} }

View File

@ -40,7 +40,7 @@ func SaveAlbumAsYaml(a entity.Album) {
} }
} }
// GET /api/v1/albums/:uuid // GET /api/v1/albums
func GetAlbums(router *gin.RouterGroup) { func GetAlbums(router *gin.RouterGroup) {
router.GET("/albums", func(c *gin.Context) { router.GET("/albums", func(c *gin.Context) {
s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionSearch) s := Auth(SessionID(c), acl.ResourceAlbums, acl.ActionSearch)

View File

@ -64,6 +64,11 @@
"tokens": null, "tokens": null,
"expiration": 1613522078076464014 "expiration": 1613522078076464014
}, },
"1c2319c0a31690d8c0d9c5bb857ce4b94f7bef2e3289ae4a": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685372068695142
},
"1df4c9db0a9092076596fb78727d069689c4b3083372a1fe": { "1df4c9db0a9092076596fb78727d069689c4b3083372a1fe": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -119,6 +124,11 @@
"tokens": null, "tokens": null,
"expiration": 1613528491291754429 "expiration": 1613528491291754429
}, },
"35ea7856b7147b81ebe396145fbdabb4fa6ce49b7834f4da": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613686777776704897
},
"370c05ea9a50f1e6befe3eba0e59e1a965daaa31da6f028a": { "370c05ea9a50f1e6befe3eba0e59e1a965daaa31da6f028a": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -144,6 +154,11 @@
"tokens": null, "tokens": null,
"expiration": 1613526857034102326 "expiration": 1613526857034102326
}, },
"40e83ce294fc2dce5fe1e17cee5cf9422a83d85d61a6e6ba": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613686103907054752
},
"41a99f15500d1eca9818bc3e0cae7cca319cd6eb2b38bb8d": { "41a99f15500d1eca9818bc3e0cae7cca319cd6eb2b38bb8d": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -174,6 +189,11 @@
"tokens": null, "tokens": null,
"expiration": 1613509579377741749 "expiration": 1613509579377741749
}, },
"48d794607621adc21621196a96fe29b631f18d1f15b0e0e3": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685352396869309
},
"4b390db50d53378cfd6e3e3b08aa2a02b1df8fdcf1bda5c5": { "4b390db50d53378cfd6e3e3b08aa2a02b1df8fdcf1bda5c5": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -209,6 +229,11 @@
"tokens": null, "tokens": null,
"expiration": 1613502564441892628 "expiration": 1613502564441892628
}, },
"5d748cc67c9c4efec35d910f799113b793a9fc47f9d88bd5": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613686778812288997
},
"6196c51bd2db97cc8aeaaac53bef1c6400c50774fedc97b5": { "6196c51bd2db97cc8aeaaac53bef1c6400c50774fedc97b5": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -239,16 +264,31 @@
"tokens": null, "tokens": null,
"expiration": 1613526444602833378 "expiration": 1613526444602833378
}, },
"7bd83ada99d8ef2ddcb389fc9914dc4c9b3bf7596c529b38": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613686726271559883
},
"7d381eaaea551483d8d50aa39007d7fc9fdddd3b6a06359d": { "7d381eaaea551483d8d50aa39007d7fc9fdddd3b6a06359d": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
"expiration": 1613513949594487106 "expiration": 1613513949594487106
}, },
"81000ef1dad0e84ef593d3838fb4ccb28a409d60cfaa7ef7": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685000623682231
},
"8173f5cece02b1a41e9bc937bbbbc80960b838d93cef7f2d": { "8173f5cece02b1a41e9bc937bbbbc80960b838d93cef7f2d": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
"expiration": 1613526315102874135 "expiration": 1613526315102874135
}, },
"8223d8743b700e5c507ff37630f02de3c80dfeb8365b4feb": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685493224019727
},
"82b0a1bdd1ac266855b338513eff6631c000f7187f1e099d": { "82b0a1bdd1ac266855b338513eff6631c000f7187f1e099d": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -269,6 +309,11 @@
"tokens": null, "tokens": null,
"expiration": 1613519263888623481 "expiration": 1613519263888623481
}, },
"88ba10c295a2e1e433d5b846ac5aea7a8fc1b35f90b97992": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685115509915196
},
"8b98cdc875e6a849a9ab77ee44abef39bab4311dc7d19493": { "8b98cdc875e6a849a9ab77ee44abef39bab4311dc7d19493": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -424,6 +469,11 @@
"tokens": null, "tokens": null,
"expiration": 1613513847101926534 "expiration": 1613513847101926534
}, },
"cf5e23f28813fe367d3e1a5de1fc066b76988cae3d14343d": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613686725223640044
},
"d3f79b180ecda15ef16859e1d822e29bb5c801438a8b3ac1": { "d3f79b180ecda15ef16859e1d822e29bb5c801438a8b3ac1": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,
@ -459,6 +509,11 @@
"tokens": null, "tokens": null,
"expiration": 1613526269423740413 "expiration": 1613526269423740413
}, },
"e98af3258ddfead5fc4ed76278bada0fed9839c111a80b86": {
"user": "uqnzie01i1nypnt9",
"tokens": null,
"expiration": 1613685151568885713
},
"eb57053b27b37cdca1d7829972d0af55f329dad18a7e3c96": { "eb57053b27b37cdca1d7829972d0af55f329dad18a7e3c96": {
"user": "uqnzie01i1nypnt9", "user": "uqnzie01i1nypnt9",
"tokens": null, "tokens": null,

View File

@ -3,7 +3,7 @@ UID: pqnzigq351j2fqgn
Type: image Type: image
Title: A really great photo! Title: A really great photo!
TitleSrc: manual 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 DescriptionSrc: manual
OriginalName: IMG_3044 OriginalName: IMG_3044
Year: -1 Year: -1
@ -13,5 +13,5 @@ Details:
Keywords: green, tambourine Keywords: green, tambourine
KeywordsSrc: manual KeywordsSrc: manual
CreatedAt: 2021-02-04T03:17:14.613092062Z CreatedAt: 2021-02-04T03:17:14.613092062Z
UpdatedAt: 2021-02-11T21:45:52.568675691Z UpdatedAt: 2021-02-11T22:19:37.804340087Z
EditedAt: 2021-02-11T21:45:53Z EditedAt: 2021-02-11T22:19:38Z

View File

@ -2,6 +2,8 @@ package test
import ( import (
"testing" "testing"
"github.com/kris-nova/client-go/api/v1"
) )
func TestHappyGetAlbum(t *testing.T) { 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.Errorf("expected error for unknown album: %d", album.ID)
t.FailNow() 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()
}
}

View File

@ -21,6 +21,7 @@ const (
WellKnownAlbumID = "aqnzih81icziiyae" WellKnownAlbumID = "aqnzih81icziiyae"
UnknownAlbumID = "1234567890" UnknownAlbumID = "1234567890"
WellKnownSampleAppConnectionString = "http://localhost:8080" WellKnownSampleAppConnectionString = "http://localhost:8080"
UnknownCategory = "Furries"
) )
// Client is a pre-authenticated client that can be used // Client is a pre-authenticated client that can be used