From ccae147398fb93b01fcd0745994b48368bc808c3 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 16 Mar 2024 11:14:06 -0400 Subject: [PATCH] feat: implement a basic account pins json api without paging --- api/s5/messages.go | 9 +++++++++ api/s5/s5.go | 19 +++++++++++++++++-- api/s5/swagger.yaml | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/api/s5/messages.go b/api/s5/messages.go index 819027b..845772d 100644 --- a/api/s5/messages.go +++ b/api/s5/messages.go @@ -120,3 +120,12 @@ func (a AccountPinBinaryResponse) EncodeMsgpack(enc *msgpack.Encoder) error { return nil } + +type AccountPinResponse struct { + Pins []AccountPin `json:"pins"` +} + +type AccountPin struct { + Hash string `json:"hash"` + MimeType string `json:"mime_type"` +} diff --git a/api/s5/s5.go b/api/s5/s5.go index 10537a6..54fb548 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -189,6 +189,7 @@ func (s *S5API) Routes() (*httprouter.Router, error) { "GET /s5/account": middleware.ApplyMiddlewares(s.accountInfo, authMw), "GET /s5/account/stats": middleware.ApplyMiddlewares(s.accountStats, authMw), "GET /s5/account/pins.bin": middleware.ApplyMiddlewares(s.accountPinsBinary, authMw), + "GET /s5/account/pins": middleware.ApplyMiddlewares(s.accountPins, authMw), // Upload API "POST /s5/upload": middleware.ApplyMiddlewares(s.smallFileUpload, authMw), @@ -824,13 +825,27 @@ func (s *S5API) accountPinsBinary(jc jape.Context) { func (s *S5API) accountPins(jc jape.Context) { userID := middleware.GetUserFromContext(jc.Request.Context()) - pins, err := s.accounts.AccountPins(userID, 0) + pinsRet, err := s.accounts.AccountPins(userID, 0) if err != nil { s.sendErrorResponse(jc, NewS5Error(ErrKeyStorageOperationFailed, err)) return } - jc.Encode(&AccountPinBinaryResponse{Pins: pins}) + pins := make([]AccountPin, len(pinsRet)) + + for i, pin := range pinsRet { + base64Url, err := encoding.NewMultihash(append([]byte{byte(types.HashTypeBlake3)}, pin.Upload.Hash...)).ToBase64Url() + if err != nil { + s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err)) + return + } + pins[i] = AccountPin{ + Hash: base64Url, + MimeType: pin.Upload.MimeType, + } + } + + jc.Encode(&AccountPinResponse{Pins: pins}) } func (s *S5API) accountPinDelete(jc jape.Context) { diff --git a/api/s5/swagger.yaml b/api/s5/swagger.yaml index 47236c5..9a2bf38 100644 --- a/api/s5/swagger.yaml +++ b/api/s5/swagger.yaml @@ -82,6 +82,20 @@ paths: application/json: schema: $ref: "#/components/schemas/AccountStatsResponse" + /s5/account/pins: + get: + summary: Retrieve account pins + tags: + - account + responses: + '200': + description: Account pins + content: + application/json: + schema: + type: array + items: + type: string /s5/account/pins.bin: get: summary: Retrieve account pins @@ -882,6 +896,20 @@ components: properties: stats: $ref: "#/components/schemas/AccountStats" + AccountPinsResponse: + type: object + properties: + pins: + type: array + items: + $ref: '#/components/schemas/AccountPin' + AccountPin: + type: object + properties: + hash: + type: string + mime_type: + type: string RegistryQueryResponse: type: object properties: