Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"io"
"mime"
"net"
"net/http"
"net/url"
"path/filepath"
"reflect"
"strconv"
"time"
Expand Down Expand Up @@ -202,7 +204,7 @@ func (s Server) toEchoHandler(handlerFunc any) echo.HandlerFunc {

var j int
// Get path parameters
for i := 0; i < handlerFuncType.NumIn(); i++ {
for i := range handlerFuncType.NumIn() {
paramType := handlerFuncType.In(i)
if paramType.String() == "context.Context" {
inputParams = append(inputParams, reflect.ValueOf(c.Request().Context()))
Expand Down Expand Up @@ -347,6 +349,7 @@ func (s Server) setupRoutes(e *echo.Echo) {
e.POST("/api/preparation/:id/piece", s.toEchoHandler(s.dataprepHandler.AddPieceHandler))

// Wallet
e.POST("/api/wallet/create", s.toEchoHandler(s.walletHandler.CreateHandler))
e.POST("/api/wallet", s.toEchoHandler(s.walletHandler.ImportHandler))
e.GET("/api/wallet", s.toEchoHandler(s.walletHandler.ListHandler))
e.DELETE("/api/wallet/:address", s.toEchoHandler(s.walletHandler.RemoveHandler))
Expand Down Expand Up @@ -527,3 +530,34 @@ func httpResponseFromError(c echo.Context, e error) error {
logger.Errorf("%+v", e)
return c.JSON(httpStatusCode, HTTPError{Err: e.Error()})
}

// retrieveFile handles GET /api/file/:id/retrieve
func (s Server) retrieveFile(c echo.Context) error {
idParam := c.Param("id")
id, err := strconv.ParseUint(idParam, 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, HTTPError{Err: "invalid file ID"})
}

data, name, modTime, err := s.fileHandler.RetrieveFileHandler(
c.Request().Context(),
s.db,
s.retriever,
id,
)
if err != nil {
return httpResponseFromError(c, err)
}

c.Response().Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", name))

ext := filepath.Ext(name)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "application/octet-stream"
}
c.Response().Header().Set("Content-Type", mimeType)

http.ServeContent(c.Response(), c.Request(), name, modTime, data)
return nil
}
11 changes: 11 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ func setupMockWallet() wallet.Handler {
m := new(wallet.MockWallet)
m.On("AttachHandler", mock.Anything, mock.Anything, "id", "wallet").
Return(&model.Preparation{}, nil)
m.On("CreateHandler", mock.Anything, mock.Anything, mock.Anything).
Return(&model.Wallet{}, nil)
m.On("DetachHandler", mock.Anything, mock.Anything, "id", "wallet").
Return(&model.Preparation{}, nil)
m.On("ImportHandler", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Expand Down Expand Up @@ -301,6 +303,15 @@ func TestAllAPIs(t *testing.T) {
})

t.Run("wallet", func(t *testing.T) {
t.Run("CreateWallet", func(t *testing.T) {
resp, err := client.Wallet.CreateWallet(&wallet2.CreateWalletParams{
Request: &models.WalletCreateRequest{},
Context: ctx,
})
require.NoError(t, err)
require.True(t, resp.IsSuccess())
require.NotNil(t, resp.Payload)
})
t.Run("ImportWallet", func(t *testing.T) {
resp, err := client.Wallet.ImportWallet(&wallet2.ImportWalletParams{
Request: &models.WalletImportRequest{},
Expand Down
153 changes: 153 additions & 0 deletions client/swagger/http/wallet/create_wallet_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading