Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API endpoints #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions lobbyui/lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (m Model) listJams() tea.Cmd {
return func() tea.Msg {
// Create an HTTP client and make a GET request.
c := &http.Client{Timeout: 10 * time.Second}
res, err := c.Get(m.apiURL + "/jam")
res, err := c.Get(m.apiURL + "/jams")
if err != nil {
// There was an error making our request. Wrap the error we received
// in a message and return it.
Expand All @@ -49,7 +49,7 @@ func (m Model) listJams() tea.Cmd {
// Return the HTTP status code
// as a message.
if res.StatusCode >= 400 {
return rmxerr.ErrMsg{Err: fmt.Errorf("could not get sessions: %d", res.StatusCode)}
return rmxerr.ErrMsg{Err: fmt.Errorf("could not get sessions: %s %d", res.Request.URL.Path, res.StatusCode)}
}
decoder := json.NewDecoder(res.Body)
var resp jamsResp
Expand Down Expand Up @@ -202,7 +202,7 @@ func jamCreate(baseURL string) tea.Cmd {
// Next step would be to show inputs for Jam details
// (name, bpm, etc) before creating the Jam.
return func() tea.Msg {
resp, err := http.Post(baseURL+"/jam", "application/json", strings.NewReader("{}"))
resp, err := http.Post(baseURL+"/jams", "application/json", strings.NewReader("{}"))
if err != nil {
return rmxerr.ErrMsg{Err: fmt.Errorf("jamCreate: %v", err)}
}
Expand Down
21 changes: 8 additions & 13 deletions tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,17 @@ var (
)

func NewModel(serverHostURL string, debugMode bool) (mainModel, error) {
wsHostURL, err := url.Parse(serverHostURL)
if err != nil {
return mainModel{}, err
}

wsHostURL.Scheme = "ws" + strings.TrimPrefix(wsHostURL.Scheme, "http")
jamModel, err := jamui.New()
if err != nil {
return mainModel{}, err
}
restEndpoint := serverHostURL + "/v0"
return mainModel{
curView: lobbyView,
lobby: lobbyui.New(serverHostURL + "/api/v1"),
lobby: lobbyui.New(serverHostURL + "/v0"),
jam: jamModel,
RESTendpoint: serverHostURL + "/api/v1",
WSendpoint: wsHostURL.String() + "/ws",
RESTendpoint: restEndpoint,
WSendpoint: strings.Replace(restEndpoint, "http", "ws", 1),
log: *log.Default(),
}, nil
}
Expand Down Expand Up @@ -135,8 +130,8 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m mainModel) View() string {
physicalWidth, _, _ := term.GetSize(int(os.Stdout.Fd()))
doc := strings.Builder{}

status := fmt.Sprintf("server: %s", formatHost(m.RESTendpoint))
serverLine := fmt.Sprintf("server: %s", formatHost(m.RESTendpoint))
status := serverLine
statusKeyText := "STATUS"

rttStats := "--"
Expand All @@ -156,7 +151,7 @@ func (m mainModel) View() string {
}

if m.curError != nil {
status = styles.RenderError(fmt.Sprint(m.curError))
status = styles.RenderError(lipgloss.JoinVertical(lipgloss.Top, fmt.Sprint(m.curError), serverLine))
statusKeyText = "ERROR"
}

Expand Down Expand Up @@ -212,7 +207,7 @@ func bail(err error) {

func (m mainModel) jamConnect(jamID string) tea.Cmd {
return func() tea.Msg {
jURL := m.WSendpoint + "/jam/" + jamID
jURL := fmt.Sprintf("%s/jams/%s/ws", m.WSendpoint, jamID)
ws, _, err := websocket.DefaultDialer.Dial(jURL, nil)
if err != nil {
return rmxerr.ErrMsg{Err: fmt.Errorf("jamConnect: %v\n%v", jURL, err)}
Expand Down
33 changes: 33 additions & 0 deletions wsmsg/wsmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wsmsg

import (
"encoding/json"
"fmt"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -64,3 +65,35 @@ func (e *Envelope) SetPayload(payload any) error {
func (e *Envelope) Unwrap(msg any) error {
return json.Unmarshal(e.Payload, msg)
}

func (t *MsgType) UnmarshalJSON(data []byte) error {
var rawType string
err := json.Unmarshal(data, &rawType)
if err != nil {
return err
}

switch rawType {
case "connect":
*t = CONNECT
case "midi":
*t = MIDI
case "text":
*t = TEXT
default:
return fmt.Errorf("unknown type: %s", rawType)
}
return nil
}

func (t *MsgType) MarshalJSON() ([]byte, error) {
switch *t {
case CONNECT:
return []byte(`"connect"`), nil
case MIDI:
return []byte(`"midi"`), nil
case TEXT:
return []byte(`"text"`), nil
}
return []byte{}, fmt.Errorf("unknown MsgTyp value: %d", *t)
}
41 changes: 41 additions & 0 deletions wsmsg/wsmsg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package wsmsg_test

import (
"encoding/json"
"testing"

"github.com/rapidmidiex/rmxtui/wsmsg"
"github.com/stretchr/testify/require"
)

func TestMsgTypeMarshaling(t *testing.T) {
t.Run("unmarshals type from JSON", func(t *testing.T) {
message := []byte(`{
"id": "7b0f33ba-8a50-446d-aaa4-4de4aa96fc6c",
"type": "midi",
"payload": {
"state": 1,
"number": 60,
"velocity": 120
},
"userId": null
}`)

var got wsmsg.Envelope
err := json.Unmarshal(message, &got)
require.NoError(t, err)

require.Equal(t, got.Typ, wsmsg.MIDI)
})

t.Run("marshals type to JSON", func(t *testing.T) {
message := wsmsg.Envelope{
Typ: wsmsg.MIDI,
}

got, err := json.Marshal(&message)
require.NoError(t, err)
want := `"type":"midi"`
require.Containsf(t, string(got), want, "JSON does not contain [ %s ]\n%s", want, string(got))
})
}