-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
13 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 |
---|---|---|
@@ -1,19 +1,37 @@ | ||
package ipc | ||
|
||
const ( | ||
PlayPath = "/transport/play" | ||
// GET | ||
PingPath = "/ping" | ||
// POST | ||
PlayPath = "/transport/play" | ||
// POST | ||
PlayPausePath = "/transport/playpause" | ||
PausePath = "/transport/pause" | ||
StopPath = "/transport/stop" | ||
PreviousPath = "/transport/previous" | ||
NextPath = "/transport/next" | ||
TimePosPath = "/transport/timepos" | ||
|
||
// POST | ||
PausePath = "/transport/pause" | ||
// POST | ||
StopPath = "/transport/stop" | ||
// POST | ||
PreviousPath = "/transport/previous" | ||
// POST | ||
NextPath = "/transport/next" | ||
// POST(TimePos) | ||
TimePosPath = "/transport/timepos" | ||
// POST to seek | ||
PlayTrackPath = "/queue/playtrack" | ||
|
||
// GET -> Volume | ||
// POST(Volume) | ||
VolumePath = "/volume" | ||
) | ||
|
||
type TimePos struct { | ||
Seconds float64 `json:"seconds"` | ||
} | ||
|
||
type Volume struct { | ||
Volume int `json:"volume"` | ||
} | ||
|
||
type Response struct { | ||
Error string `json:"error"` | ||
} |
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,83 @@ | ||
package ipc | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
var ErrPingFail = errors.New("ping failed") | ||
|
||
type Client struct { | ||
httpC http.Client | ||
} | ||
|
||
// Connect attempts to connect to the IPC socket as client. | ||
func Connect() (*Client, error) { | ||
conn, err := Dial() | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := &Client{httpC: http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return conn, nil | ||
}, | ||
}, | ||
}} | ||
if err := client.Ping(); err != nil { | ||
return nil, err | ||
} | ||
return client, nil | ||
} | ||
|
||
func (c *Client) Ping() error { | ||
if c.makeSimpleRequest(http.MethodGet, PingPath) != nil { | ||
return ErrPingFail | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) Play() error { | ||
return c.makeSimpleRequest(http.MethodPost, PlayPath) | ||
} | ||
|
||
func (c *Client) Pause() error { | ||
return c.makeSimpleRequest(http.MethodPost, PausePath) | ||
} | ||
|
||
func (c *Client) PlayPause() error { | ||
return c.makeSimpleRequest(http.MethodPost, PlayPausePath) | ||
} | ||
|
||
func (c *Client) SeekNext() error { | ||
return c.makeSimpleRequest(http.MethodPost, NextPath) | ||
} | ||
|
||
func (c *Client) SeekBackOrPrevious() error { | ||
return c.makeSimpleRequest(http.MethodPost, NextPath) | ||
} | ||
|
||
func (c *Client) makeSimpleRequest(method string, path string) error { | ||
var resp *http.Response | ||
var err error | ||
switch method { | ||
case http.MethodGet: | ||
resp, err = c.httpC.Get(path) | ||
case http.MethodPost: | ||
resp, err = c.httpC.Post(path, "application/json", nil) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
var r Response | ||
json.NewDecoder(resp.Body).Decode(&r) | ||
return errors.New(r.Error) | ||
} | ||
return nil | ||
} |
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