Skip to content

Commit

Permalink
Merge pull request #185 from jovandeginste/add-generic-import
Browse files Browse the repository at this point in the history
Add generic "import" endpoint to API
  • Loading branch information
jovandeginste authored Jul 29, 2024
2 parents 5a51f7f + 407b6a6 commit fe16061
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion assets/output.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
! tailwindcss v3.4.3 | MIT License | https://tailwindcss.com
! tailwindcss v3.4.4 | MIT License | https://tailwindcss.com
*/

/*
Expand Down
13 changes: 4 additions & 9 deletions pkg/importers/fitotrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ func importFitotrack(headers http.Header, body io.ReadCloser) (*Content, error)
wt := headers.Get("FitoTrack-Workout-Type")
wn := headers.Get("FitoTrack-Comment")

defer body.Close()

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

g := &Content{
Content: b,
Type: wt,
Notes: wn,
}
b.Type = wt
b.Notes = wn

return g, nil
return b, nil
}
20 changes: 20 additions & 0 deletions pkg/importers/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package importers

import (
"io"
"net/http"
)

func importGeneric(_ http.Header, body io.ReadCloser) (*Content, error) {
b, err := io.ReadAll(body)
if err != nil {
return nil, err
}

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

return g, nil
}
11 changes: 8 additions & 3 deletions pkg/importers/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ type Content struct {
}

func Import(program string, headers http.Header, body io.ReadCloser) (*Content, error) {
if program == "fitotrack" {
defer body.Close()

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

return nil, fmt.Errorf("%w: %s", ErrUnsupportedProgram, program)
}

0 comments on commit fe16061

Please sign in to comment.