Skip to content

Commit

Permalink
Merge pull request #237 from jovandeginste/api-generic-name
Browse files Browse the repository at this point in the history
feat: Accept API query parameters for workout import
  • Loading branch information
jovandeginste authored Aug 6, 2024
2 parents dd27f53 + 56db0e0 commit f3d4878
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion pkg/app/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ func (a *App) apiWorkoutHandler(c echo.Context) error {
// apiImportHandler imports a workout
// @Summary Import a workout
// @Param program path string true "Program that generates the workout file"
// @Param name query string "no-name" "Name of the imported workout"
// @Param type query string "auto" "Type of the imported workout"
// @Produce json
// @Success 200 {object} APIResponse{result=database.Workout}
// @Failure 400 {object} APIResponse
Expand All @@ -300,7 +302,7 @@ func (a *App) apiImportHandler(c echo.Context) error {
program := c.Param("program")
a.logger.Info("Importing with program: " + program)

file, err := importers.Import(program, c.Request().Header, c.Request().Body)
file, err := importers.Import(program, c, c.Request().Body)
if err != nil {
return a.renderAPIError(c, resp, err)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/importers/fitotrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ package importers
import (
"fmt"
"io"
"net/http"

"github.com/labstack/echo/v4"
)

func importFitotrack(headers http.Header, body io.ReadCloser) (*Content, error) {
func importFitotrack(c echo.Context, body io.ReadCloser) (*Content, error) {
headers := c.Request().Header

if t := headers.Get("FitoTrack-Type"); t != "workout-gpx" {
return nil, fmt.Errorf("unsupported FitoTrack-Type: %s", t)
}

wt := headers.Get("FitoTrack-Workout-Type")
wn := headers.Get("FitoTrack-Comment")

b, err := importGeneric(headers, body)
b, err := importGeneric(c, body)
if err != nil {
return nil, err
}

b.Type = wt
b.Notes = wn
b.Filename = ""

return b, nil
}
14 changes: 10 additions & 4 deletions pkg/importers/generic.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package importers

import (
"cmp"
"io"
"net/http"

"github.com/labstack/echo/v4"
)

func importGeneric(_ http.Header, body io.ReadCloser) (*Content, error) {
func importGeneric(c echo.Context, body io.ReadCloser) (*Content, error) {
name := cmp.Or(c.QueryParam("name"), "no-name")
t := cmp.Or(c.QueryParam("type"), "auto")

b, err := io.ReadAll(body)
if err != nil {
return nil, err
}

g := &Content{
Content: b,
Type: "auto",
Filename: name,
Content: b,
Type: t,
}

return g, nil
Expand Down
9 changes: 5 additions & 4 deletions pkg/importers/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"errors"
"fmt"
"io"
"net/http"

"github.com/labstack/echo/v4"
)

var ErrUnsupportedProgram = errors.New("unsupported program")
Expand All @@ -16,14 +17,14 @@ type Content struct {
Type string
}

func Import(program string, headers http.Header, body io.ReadCloser) (*Content, error) {
func Import(program string, c echo.Context, body io.ReadCloser) (*Content, error) {
defer body.Close()

switch program {
case "generic":
return importGeneric(headers, body)
return importGeneric(c, body)
case "fitotrack":
return importFitotrack(headers, body)
return importFitotrack(c, body)
default:
return nil, fmt.Errorf("%w: %s", ErrUnsupportedProgram, program)
}
Expand Down

0 comments on commit f3d4878

Please sign in to comment.