package beachcomber
import (
"bufio"
"net"
)
type Session struct {
conn net.Conn
scanner *bufio.Scanner
}
func (s *Session) Get(key string, path string) (*Result, error) {
req := map[string]interface{}{"op": "get", "key": key}
if path != "" {
req["path"] = path
}
return s.roundtrip(req)
}
func (s *Session) Poke(key string, path string) error {
req := map[string]interface{}{"op": "poke", "key": key}
if path != "" {
req["path"] = path
}
_, err := s.roundtrip(req)
return err
}
func (s *Session) SetContext(path string) error {
_, err := s.roundtrip(map[string]interface{}{"op": "context", "path": path})
return err
}
func (s *Session) Close() error {
return s.conn.Close()
}
func (s *Session) roundtrip(req map[string]interface{}) (*Result, error) {
if err := writeJSON(s.conn, req); err != nil {
return nil, err
}
return readResponse(s.scanner)
}