Skip to content

Commit 362d82b

Browse files
authored
chore: follow immich 1.95.0 API changes (#170)
1 parent 935e0a4 commit 362d82b

File tree

12 files changed

+255
-155
lines changed

12 files changed

+255
-155
lines changed

cmd/duplicate/duplicate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func DuplicateCommand(ctx context.Context, common *cmd.SharedFlags, args []strin
6969

7070
dupCount := 0
7171
app.Jnl.Log.MessageContinue(logger.OK, "Get server's assets...")
72-
err = app.Immich.GetAllAssetsWithFilter(ctx, nil, func(a *immich.Asset) {
72+
err = app.Immich.GetAllAssetsWithFilter(ctx, func(a *immich.Asset) {
7373
if a.IsTrashed {
7474
return
7575
}

cmd/metadata/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func MetadataCommand(ctx context.Context, common *cmd.SharedFlags, args []string
6161
}
6262

6363
app.Jnl.Log.MessageContinue(logger.OK, "Get server's assets...")
64-
list, err := app.Immich.GetAllAssets(ctx, nil)
64+
list, err := app.Immich.GetAllAssets(ctx)
6565
if err != nil {
6666
return err
6767
}

cmd/stack/stack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewStackCommand(ctx context.Context, common *cmd.SharedFlags, args []string
5757
app.Jnl.Log.MessageContinue(logger.OK, "Get server's assets...")
5858
assetCount := 0
5959

60-
err = app.Immich.GetAllAssetsWithFilter(ctx, nil, func(a *immich.Asset) {
60+
err = app.Immich.GetAllAssetsWithFilter(ctx, func(a *immich.Asset) {
6161
if a.IsTrashed {
6262
return
6363
}

cmd/upload/e2e_upload_folder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func resetImmich(ic *immich.ImmichClient, user string) error {
390390
}
391391
}
392392

393-
assets, err := ic.GetAllAssets(context.Background(), nil)
393+
assets, err := ic.GetAllAssets(context.Background())
394394
if err != nil {
395395
return err
396396
}
@@ -405,7 +405,7 @@ func resetImmich(ic *immich.ImmichClient, user string) error {
405405

406406
attempts := 5
407407
for attempts > 0 {
408-
assets, err := ic.GetAllAssets(context.Background(), nil)
408+
assets, err := ic.GetAllAssets(context.Background())
409409
if err != nil {
410410
return err
411411
}

cmd/upload/upload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func NewUpCmd(ctx context.Context, common *cmd.SharedFlags, args []string) (*UpC
164164
}
165165
app.Jnl.Log.OK("Ask for server's assets...")
166166
var list []*immich.Asset
167-
err = app.Immich.GetAllAssetsWithFilter(ctx, nil, func(a *immich.Asset) {
167+
err = app.Immich.GetAllAssetsWithFilter(ctx, func(a *immich.Asset) {
168168
if a.IsTrashed {
169169
return
170170
}

cmd/upload/upload_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
type stubIC struct{}
2121

22-
func (c *stubIC) GetAllAssetsWithFilter(context.Context, *immich.GetAssetOptions, func(*immich.Asset)) error {
22+
func (c *stubIC) GetAllAssetsWithFilter(context.Context, func(*immich.Asset)) error {
2323
return nil
2424
}
2525

@@ -77,7 +77,7 @@ func (c *stubIC) GetAssetAlbums(ctx context.Context, id string) ([]immich.AlbumS
7777
return nil, nil
7878
}
7979

80-
func (c *stubIC) GetAllAssets(ctx context.Context, opt *immich.GetAssetOptions) ([]*immich.Asset, error) {
80+
func (c *stubIC) GetAllAssets(ctx context.Context) ([]*immich.Asset, error) {
8181
return nil, nil
8282
}
8383

immich/asset.go

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -174,57 +174,6 @@ func (o *GetAssetOptions) Values() url.Values {
174174
return v
175175
}
176176

177-
// GetAllAssets get all user's assets using the paged API searchAssets
178-
//
179-
// It calls the server for IMAGE, VIDEO, normal item, trashed Items
180-
181-
func (ic *ImmichClient) GetAllAssets(ctx context.Context, opt *GetAssetOptions) ([]*Asset, error) {
182-
var r []*Asset
183-
184-
for _, t := range []string{"IMAGE", "VIDEO", "AUDIO", "OTHER"} {
185-
values := opt.Values()
186-
values.Set("type", t)
187-
values.Set("withExif", "true")
188-
values.Set("isVisible", "true")
189-
values.Del("trashedBefore")
190-
err := ic.newServerCall(ctx, "GetAllAssets", setPaginator()).do(get("/assets", setURLValues(values), setAcceptJSON()), responseAccumulateJSON(&r))
191-
if err != nil {
192-
return r, err
193-
}
194-
values.Set("trashedBefore", "9999-01-01")
195-
err = ic.newServerCall(ctx, "GetAllAssets", setPaginator()).do(get("/assets", setURLValues(values), setAcceptJSON()), responseAccumulateJSON(&r))
196-
if err != nil {
197-
return r, err
198-
}
199-
}
200-
return r, nil
201-
}
202-
203-
// GetAllAssetsWithFilter get all user's assets using the paged API searchAssets and apply a filter
204-
// TODO: rename this function, it's not a filter, it uses a callback function for each item
205-
//
206-
// It calls the server for IMAGE, VIDEO, normal item, trashed Items
207-
func (ic *ImmichClient) GetAllAssetsWithFilter(ctx context.Context, opt *GetAssetOptions, filter func(*Asset)) error {
208-
for _, t := range []string{"IMAGE", "VIDEO", "AUDIO", "OTHER"} {
209-
values := opt.Values()
210-
values.Set("type", t)
211-
values.Set("withExif", "true")
212-
values.Set("isVisible", "true")
213-
values.Del("trashedBefore")
214-
err := ic.newServerCall(ctx, "GetAllAssets", setPaginator()).do(get("/assets", setURLValues(values), setAcceptJSON()), responseJSONWithFilter(filter))
215-
if err != nil {
216-
return err
217-
}
218-
values.Set("trashedBefore", "9999-01-01")
219-
err = ic.newServerCall(ctx, "GetAllAssets", setPaginator()).do(get("/assets", setURLValues(values), setAcceptJSON()), responseJSONWithFilter(filter))
220-
if err != nil {
221-
return err
222-
}
223-
}
224-
225-
return nil
226-
}
227-
228177
func (ic *ImmichClient) DeleteAssets(ctx context.Context, id []string, forceDelete bool) error {
229178
req := struct {
230179
Force bool `json:"force"`
@@ -238,8 +187,13 @@ func (ic *ImmichClient) DeleteAssets(ctx context.Context, id []string, forceDele
238187
}
239188

240189
func (ic *ImmichClient) GetAssetByID(ctx context.Context, id string) (*Asset, error) {
190+
body := struct {
191+
WithExif bool `json:"withExif,omitempty"`
192+
IsVisible bool `json:"isVisible,omitempty"`
193+
ID string `json:"id"`
194+
}{WithExif: true, IsVisible: true, ID: id}
241195
r := Asset{}
242-
err := ic.newServerCall(ctx, "GetAssetByID").do(get("/asset/assetById/"+id, setAcceptJSON()), responseJSON(&r))
196+
err := ic.newServerCall(ctx, "GetAssetByID").do(post("/search/metadata", "application/json", setAcceptJSON(), setJSONBody(body)), responseJSON(&r))
243197
return &r, err
244198
}
245199

immich/call.go

Lines changed: 15 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11-
"net/url"
12-
"strconv"
1311
"strings"
1412
)
1513

@@ -28,11 +26,8 @@ type serverCall struct {
2826
ic *ImmichClient
2927
err error
3028
ctx context.Context
31-
p *paginator
3229
}
3330

34-
type serverCallOption func(sc *serverCall) error
35-
3631
// callError represents errors returned by the server
3732
type callError struct {
3833
endPoint string
@@ -85,17 +80,12 @@ func (ce callError) Error() string {
8580
return b.String()
8681
}
8782

88-
func (ic *ImmichClient) newServerCall(ctx context.Context, api string, opts ...serverCallOption) *serverCall {
83+
func (ic *ImmichClient) newServerCall(ctx context.Context, api string) *serverCall {
8984
sc := &serverCall{
9085
endPoint: api,
9186
ic: ic,
9287
ctx: ctx,
9388
}
94-
if sc.err == nil {
95-
for _, opt := range opts {
96-
_ = sc.joinError(opt(sc))
97-
}
98-
}
9989
return sc
10090
}
10191

@@ -120,32 +110,6 @@ func (sc *serverCall) joinError(err error) error {
120110
return err
121111
}
122112

123-
// paginator controls the paged API calls
124-
type paginator struct {
125-
pageNumber int // current page
126-
pageParameter string // page parameter name on the URL
127-
EOF bool // true when the last page was empty
128-
}
129-
130-
func (p paginator) setPage(v url.Values) {
131-
v.Set(p.pageParameter, strconv.Itoa(p.pageNumber))
132-
}
133-
134-
func (p *paginator) nextPage() {
135-
p.pageNumber++
136-
}
137-
138-
func setPaginator() serverCallOption {
139-
return func(sc *serverCall) error {
140-
p := paginator{
141-
pageParameter: "page",
142-
pageNumber: 1,
143-
}
144-
sc.p = &p
145-
return nil
146-
}
147-
}
148-
149113
type requestFunction func(sc *serverCall) *http.Request
150114

151115
func (sc *serverCall) request(method string, url string, opts ...serverRequestOption) *http.Request {
@@ -199,25 +163,6 @@ func put(url string, opts ...serverRequestOption) requestFunction {
199163
}
200164

201165
func (sc *serverCall) do(fnRequest requestFunction, opts ...serverResponseOption) error {
202-
if sc.err != nil || fnRequest == nil {
203-
return sc.Err(nil, nil, nil)
204-
}
205-
206-
if sc.p == nil {
207-
return sc._callDo(fnRequest, opts...)
208-
}
209-
210-
for !sc.p.EOF {
211-
err := sc._callDo(fnRequest, opts...)
212-
if err != nil {
213-
return err
214-
}
215-
sc.p.nextPage()
216-
}
217-
return nil
218-
}
219-
220-
func (sc *serverCall) _callDo(fnRequest requestFunction, opts ...serverResponseOption) error {
221166
var (
222167
resp *http.Response
223168
err error
@@ -228,11 +173,6 @@ func (sc *serverCall) _callDo(fnRequest requestFunction, opts ...serverResponseO
228173
return sc.Err(req, nil, nil)
229174
}
230175

231-
if sc.p != nil {
232-
v := req.URL.Query()
233-
sc.p.setPage(v)
234-
req.URL.RawQuery = v.Encode()
235-
}
236176
if sc.ic.APITrace /* && req.Header.Get("Content-Type") == "application/json"*/ {
237177
_ = sc.joinError(setTraceJSONRequest()(sc, req))
238178
}
@@ -316,21 +256,6 @@ func setContentType(cType string) serverRequestOption {
316256
}
317257
}
318258

319-
func setURLValues(values url.Values) serverRequestOption {
320-
return func(sc *serverCall, req *http.Request) error {
321-
if values != nil {
322-
rValues := req.URL.Query()
323-
for k, v := range values {
324-
for _, s := range v {
325-
rValues.Set(k, s)
326-
}
327-
}
328-
req.URL.RawQuery = rValues.Encode()
329-
}
330-
return nil
331-
}
332-
}
333-
334259
type serverResponseOption func(sc *serverCall, resp *http.Response) error
335260

336261
func responseJSON[T any](object *T) serverResponseOption {
@@ -349,11 +274,10 @@ func responseJSON[T any](object *T) serverResponseOption {
349274
}
350275
}
351276

277+
/*
352278
func responseAccumulateJSON[T any](acc *[]T) serverResponseOption {
353279
return func(sc *serverCall, resp *http.Response) error {
354-
if sc.p != nil {
355-
sc.p.EOF = true
356-
}
280+
eof := true
357281
if resp != nil {
358282
if resp.Body != nil {
359283
defer resp.Body.Close()
@@ -366,21 +290,23 @@ func responseAccumulateJSON[T any](acc *[]T) serverResponseOption {
366290
return err
367291
}
368292
if len(arr) > 0 && sc.p != nil {
369-
sc.p.EOF = false
293+
eof = false
370294
}
371295
(*acc) = append((*acc), arr...)
296+
if eof {
297+
sc.p.setEOF()
298+
}
372299
return nil
373300
}
374301
}
375302
return errors.New("can't decode nil response")
376303
}
377304
}
378-
305+
*/
306+
/*
379307
func responseJSONWithFilter[T any](filter func(*T)) serverResponseOption {
380308
return func(sc *serverCall, resp *http.Response) error {
381-
if sc.p != nil {
382-
sc.p.EOF = true
383-
}
309+
eof := true
384310
if resp != nil {
385311
if resp.Body != nil {
386312
defer resp.Body.Close()
@@ -403,15 +329,19 @@ func responseJSONWithFilter[T any](filter func(*T)) serverResponseOption {
403329
return err
404330
}
405331
if sc.p != nil {
406-
sc.p.EOF = false
332+
eof = false
407333
}
408334
filter(&o)
409335
}
410336
// read closing bracket "]"
411337
_, err = dec.Token()
338+
if eof {
339+
sc.p.setEOF()
340+
}
412341
return err
413342
}
414343
}
415344
return errors.New("can't decode nil response")
416345
}
417346
}
347+
*/

0 commit comments

Comments
 (0)