From c9bfd9758a9772eaa29d82338db9e7c80e34c9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean=20Fran=C3=A7ois=20CASSAN?= Date: Sat, 17 Aug 2024 16:34:44 -0300 Subject: [PATCH] remove "GetJobs" call from API traces (#442) * remove "GetJobs" call from API traces Fixes #440 * "Renamed hardcoded endpoint names to constants in ImmichClient and serverCall" --- immich/albums.go | 14 +++++++------- immich/call.go | 24 ++++++++++++++++++++---- immich/client.go | 10 +++++----- immich/job.go | 2 +- immich/metadata.go | 2 +- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/immich/albums.go b/immich/albums.go index 79776c35..e4d0a7e9 100644 --- a/immich/albums.go +++ b/immich/albums.go @@ -23,7 +23,7 @@ type AlbumSimplified struct { func (ic *ImmichClient) GetAllAlbums(ctx context.Context) ([]AlbumSimplified, error) { var albums []AlbumSimplified - err := ic.newServerCall(ctx, "GetAllAlbums").do(getRequest("/albums", setAcceptJSON()), responseJSON(&albums)) + err := ic.newServerCall(ctx, EndPointGetAllAlbums).do(getRequest("/albums", setAcceptJSON()), responseJSON(&albums)) if err != nil { return nil, err } @@ -77,13 +77,13 @@ func (ic *ImmichClient) GetAlbumInfo(ctx context.Context, id string, withoutAsse if withoutAssets { query += "?withoutAssets=true" } - err := ic.newServerCall(ctx, "GetAlbumInfo").do(getRequest("/albums/"+query, setAcceptJSON()), responseJSON(&album)) + err := ic.newServerCall(ctx, EndPointGetAlbumInfo).do(getRequest("/albums/"+query, setAcceptJSON()), responseJSON(&album)) return album, err } func (ic *ImmichClient) GetAssetsAlbums(ctx context.Context, id string) ([]AlbumSimplified, error) { var albums []AlbumSimplified - err := ic.newServerCall(ctx, "GetAllAlbums").do(getRequest("/albums", setAcceptJSON()), responseJSON(&albums)) + err := ic.newServerCall(ctx, EndPointGetAlbumInfo).do(getRequest("/albums", setAcceptJSON()), responseJSON(&albums)) if err != nil { return nil, err } @@ -105,7 +105,7 @@ func (ic *ImmichClient) AddAssetToAlbum(ctx context.Context, albumID string, ass body := UpdateAlbum{ IDS: assets, } - err := ic.newServerCall(ctx, "AddAssetToAlbum").do( + err := ic.newServerCall(ctx, EndPointAddAsstToAlbum).do( putRequest(fmt.Sprintf("/albums/%s/assets", albumID), setAcceptJSON(), setJSONBody(body)), responseJSON(&r)) @@ -122,7 +122,7 @@ func (ic *ImmichClient) CreateAlbum(ctx context.Context, name string, descriptio AssetIDs: assetsIDs, } var r AlbumSimplified - err := ic.newServerCall(ctx, "CreateAlbum").do( + err := ic.newServerCall(ctx, EndPointCreateAlbum).do( postRequest("/albums", "application/json", setAcceptJSON(), setJSONBody(body)), responseJSON(&r)) if err != nil { @@ -133,12 +133,12 @@ func (ic *ImmichClient) CreateAlbum(ctx context.Context, name string, descriptio func (ic *ImmichClient) GetAssetAlbums(ctx context.Context, id string) ([]AlbumSimplified, error) { var r []AlbumSimplified - err := ic.newServerCall(ctx, "GetAssetAlbums").do( + err := ic.newServerCall(ctx, EndPointGetAssetAlbums).do( getRequest("/albums?assetId="+id, setAcceptJSON()), responseJSON(&r)) return r, err } func (ic *ImmichClient) DeleteAlbum(ctx context.Context, id string) error { - return ic.newServerCall(ctx, "DeleteAlbum").do(deleteRequest("/albums/" + id)) + return ic.newServerCall(ctx, EndPointDeleteAlbum).do(deleteRequest("/albums/" + id)) } diff --git a/immich/call.go b/immich/call.go index ac0ce6b2..a6800fde 100644 --- a/immich/call.go +++ b/immich/call.go @@ -15,6 +15,22 @@ import ( "github.com/simulot/immich-go/helpers/fshelper" ) +const ( + EndPointGetJobs = "GetJobs" + EndPointGetAllAlbums = "GetAllAlbums" + EndPointGetAlbumInfo = "GetAlbumInfo" + EndPointAddAsstToAlbum = "AddAssetToAlbum" + EndPointCreateAlbum = "CreateAlbum" + EndPointGetAssetAlbums = "GetAssetAlbums" + EndPointDeleteAlbum = "DeleteAlbum" + EndPointPingServer = "PingServer" + EndPointValidateConnection = "ValidateConnection" + EndPointGetServerStatistics = "GetServerStatistics" + EndPointGetAssetStatistics = "GetAssetStatistics" + EndPointGetSupportedMediaTypes = "GetSupportedMediaTypes" + EndPointGetAllAssets = "GetAllAssets" +) + type TooManyInternalError struct { error } @@ -121,7 +137,7 @@ var callSequence atomic.Int64 const ctxCallSequenceID = "api-call-sequence" func (sc *serverCall) request(method string, url string, opts ...serverRequestOption) *http.Request { - if sc.ic.apiTraceWriter != nil { + if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs { seq := callSequence.Add(1) sc.ctx = context.WithValue(sc.ctx, ctxCallSequenceID, seq) } @@ -185,7 +201,7 @@ func (sc *serverCall) do(fnRequest requestFunction, opts ...serverResponseOption return sc.Err(req, nil, nil) } - if sc.ic.apiTraceWriter != nil /* && req.Header.Get("Content-Type") == "application/json"*/ { + if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs { _ = sc.joinError(setTraceRequest()(sc, req)) } @@ -247,7 +263,7 @@ func setJSONBody(object any) serverRequestOption { return func(sc *serverCall, req *http.Request) error { b := bytes.NewBuffer(nil) enc := json.NewEncoder(b) - if sc.ic.apiTraceWriter != nil { + if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs { enc.SetIndent("", " ") } err := enc.Encode(object) @@ -278,7 +294,7 @@ func responseJSON[T any](object *T) serverResponseOption { return nil } err := json.NewDecoder(resp.Body).Decode(object) - if sc.ic.apiTraceWriter != nil { + if sc.ic.apiTraceWriter != nil && sc.endPoint != EndPointGetJobs { seq := sc.ctx.Value(ctxCallSequenceID) fmt.Fprintln(sc.ic.apiTraceWriter, time.Now().Format(time.RFC3339), "RESPONSE", seq, sc.endPoint, resp.Request.Method, resp.Request.URL.String()) fmt.Fprintln(sc.ic.apiTraceWriter, " Status:", resp.Status) diff --git a/immich/client.go b/immich/client.go index e4ee8417..6ab1478d 100644 --- a/immich/client.go +++ b/immich/client.go @@ -113,7 +113,7 @@ func NewImmichClient(endPoint string, key string, options ...clientOption) (*Imm func (ic *ImmichClient) PingServer(ctx context.Context) error { r := PingResponse{} b := bytes.NewBuffer(nil) - err := ic.newServerCall(ctx, "PingServer").do(getRequest("/server-info/ping", setAcceptJSON()), responseCopy(b), responseJSON(&r)) + err := ic.newServerCall(ctx, EndPointPingServer).do(getRequest("/server-info/ping", setAcceptJSON()), responseCopy(b), responseJSON(&r)) if err != nil { return fmt.Errorf("unexpected response to the immich's ping API at this address: %s:\n%s", ic.endPoint+"/server-info/ping", b.String()) } @@ -129,7 +129,7 @@ func (ic *ImmichClient) PingServer(ctx context.Context) error { func (ic *ImmichClient) ValidateConnection(ctx context.Context) (User, error) { var user User - err := ic.newServerCall(ctx, "ValidateConnection"). + err := ic.newServerCall(ctx, EndPointValidateConnection). do(getRequest("/users/me", setAcceptJSON()), responseJSON(&user)) if err != nil { return user, err @@ -163,7 +163,7 @@ type ServerStatistics struct { func (ic *ImmichClient) GetServerStatistics(ctx context.Context) (ServerStatistics, error) { var s ServerStatistics - err := ic.newServerCall(ctx, "GetServerStatistics").do(getRequest("/server-info/statistics", setAcceptJSON()), responseJSON(&s)) + err := ic.newServerCall(ctx, EndPointGetServerStatistics).do(getRequest("/server-info/statistics", setAcceptJSON()), responseJSON(&s)) return s, err } @@ -178,7 +178,7 @@ type UserStatistics struct { func (ic *ImmichClient) GetAssetStatistics(ctx context.Context) (UserStatistics, error) { var s UserStatistics - err := ic.newServerCall(ctx, "GetAssetStatistics").do(getRequest("/assets/statistics", setAcceptJSON()), responseJSON(&s)) + err := ic.newServerCall(ctx, EndPointGetAssetStatistics).do(getRequest("/assets/statistics", setAcceptJSON()), responseJSON(&s)) return s, err } @@ -204,7 +204,7 @@ var DefaultSupportedMedia = SupportedMedia{ func (ic *ImmichClient) GetSupportedMediaTypes(ctx context.Context) (SupportedMedia, error) { var s map[string][]string - err := ic.newServerCall(ctx, "GetSupportedMediaTypes").do(getRequest("/server-info/media-types", setAcceptJSON()), responseJSON(&s)) + err := ic.newServerCall(ctx, EndPointGetSupportedMediaTypes).do(getRequest("/server-info/media-types", setAcceptJSON()), responseJSON(&s)) if err != nil { return nil, err } diff --git a/immich/job.go b/immich/job.go index 426f086c..a6297d06 100644 --- a/immich/job.go +++ b/immich/job.go @@ -19,6 +19,6 @@ type Job struct { func (ic *ImmichClient) GetJobs(ctx context.Context) (map[string]Job, error) { var resp map[string]Job - err := ic.newServerCall(ctx, "GetJobs").do(getRequest("/jobs", setAcceptJSON()), responseJSON(&resp)) + err := ic.newServerCall(ctx, EndPointGetJobs).do(getRequest("/jobs", setAcceptJSON()), responseJSON(&resp)) return resp, err } diff --git a/immich/metadata.go b/immich/metadata.go index a8867122..d4bd696c 100644 --- a/immich/metadata.go +++ b/immich/metadata.go @@ -30,7 +30,7 @@ func (ic *ImmichClient) callSearchMetadata(ctx context.Context, req *searchMetad return ctx.Err() default: resp := searchMetadataResponse{} - err := ic.newServerCall(ctx, "GetAllAssets").do(postRequest("/search/metadata", "application/json", setJSONBody(&req), setAcceptJSON()), responseJSON(&resp)) + err := ic.newServerCall(ctx, EndPointGetAllAssets).do(postRequest("/search/metadata", "application/json", setJSONBody(&req), setAcceptJSON()), responseJSON(&resp)) if err != nil { return err }