-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ww24/fix/api-error-handling
fix error handling
- Loading branch information
Showing
3 changed files
with
128 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/ww24/lirc-web-api/lirc" | ||
) | ||
|
||
type signal struct { | ||
Remote string `json:"remote"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func apiv1(g *echo.Group) { | ||
g.GET("/", func(c echo.Context) (err error) { | ||
signals, err := fetchSignals() | ||
if err != nil { | ||
return wrapError(err) | ||
} | ||
|
||
return &response{ | ||
code: http.StatusOK, | ||
Status: "ok", | ||
Signals: signals, | ||
} | ||
}) | ||
|
||
g.POST("/", func(c echo.Context) (err error) { | ||
sig := new(signal) | ||
if err = c.Bind(sig); err != nil { | ||
return wrapError(err) | ||
} | ||
|
||
client, err := lirc.New() | ||
if err != nil { | ||
return wrapError(err) | ||
} | ||
defer client.Close() | ||
|
||
replies, err := client.List(sig.Remote, sig.Name) | ||
if err != nil { | ||
return wrapError(err) | ||
} | ||
if len(replies) == 0 { | ||
return &response{ | ||
code: http.StatusBadRequest, | ||
Status: "ng", | ||
Message: "invalid signal", | ||
} | ||
} | ||
|
||
err = client.SendOnce(sig.Remote, sig.Name) | ||
if err != nil { | ||
return wrapError(err) | ||
} | ||
|
||
return &response{ | ||
code: http.StatusOK, | ||
Status: "ok", | ||
} | ||
}) | ||
} | ||
|
||
func fetchSignals() (signals []signal, err error) { | ||
client, err := lirc.New() | ||
if err != nil { | ||
return | ||
} | ||
defer client.Close() | ||
|
||
remotes, err := client.List("") | ||
if err != nil { | ||
return | ||
} | ||
|
||
signals = make([]signal, 0, len(remotes)*2) | ||
for _, remote := range remotes { | ||
var replies []string | ||
replies, err = client.List(remote) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, reply := range replies { | ||
signals = append(signals, signal{ | ||
Remote: remote, | ||
Name: reply, | ||
}) | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters