Skip to content

Commit

Permalink
Provides The Go Playground Client type for flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Aug 31, 2016
1 parent 1013409 commit bec6126
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/goplay/main.go
Expand Up @@ -23,7 +23,7 @@ func main() {
code = file
}

if err := goplay.Run(code, os.Stdout, os.Stderr); err != nil {
if err := goplay.DefaultClient.Run(code, os.Stdout, os.Stderr); err != nil {
fmt.Println(err)
}
}
86 changes: 56 additions & 30 deletions goplay.go
Expand Up @@ -12,17 +12,44 @@ import (
"time"
)

var (
baseURL = "https://play.golang.org"
shareEndpoint = baseURL + "/share"
compileEndpoint = baseURL + "/compile"
)
const defaultBaseURL = "https://play.golang.org"

var DefaultClient = &Client{}

// Client represensts The Go Playground client.
type Client struct {
// The base URL of The Go Playground. Default is `https://play.golang.org/`.
BaseURL string

var httpClient = &http.Client{}
// The HTTP client to use when sending requests. Defaults to
// `http.DefaultClient`.
HTTPClient *http.Client
}

// Run runs go code on playground and output stdout/stderr.
func Run(code io.Reader, stdout io.Writer, stderr io.Writer) error {
resp, err := Compile(code)
func (c *Client) httpClient() *http.Client {
if c.HTTPClient != nil {
return c.HTTPClient
}
return http.DefaultClient
}

func (c *Client) baseURL() string {
if c.BaseURL != "" {
return c.BaseURL
}
return defaultBaseURL
}

func (c *Client) shareEndpoint() string {
return c.baseURL() + "/share"
}

func (c *Client) compileEndpoint() string {
return c.baseURL() + "/compile"
}

func (c *Client) Run(code io.Reader, stdout io.Writer, stderr io.Writer) error {
resp, err := c.Compile(code)
if err != nil {
return err
}
Expand All @@ -40,34 +67,15 @@ func Run(code io.Reader, stdout io.Writer, stderr io.Writer) error {
return nil
}

// Share creates go playground share link.
func Share(code io.Reader) (string, error) {
req, err := http.NewRequest("POST", shareEndpoint, code)
if err != nil {
return "", err
}
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/p/%s", baseURL, string(b)), nil
}

// Compile compiles code on the go playground.
func Compile(code io.Reader) (*Response, error) {
func (c *Client) Compile(code io.Reader) (*Response, error) {
b, err := ioutil.ReadAll(code)
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("version", "2")
v.Set("body", string(b))
resp, err := httpClient.PostForm(compileEndpoint, v)
resp, err := c.httpClient().PostForm(c.compileEndpoint(), v)
if err != nil {
return nil, err
}
Expand All @@ -79,6 +87,24 @@ func Compile(code io.Reader) (*Response, error) {
return &r, nil
}

// Share creates go playground share link.
func (c *Client) Share(code io.Reader) (string, error) {
req, err := http.NewRequest("POST", c.shareEndpoint(), code)
if err != nil {
return "", err
}
resp, err := c.httpClient().Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/p/%s", c.baseURL(), string(b)), nil
}

// Response represensts response type of /compile.
// Licence: Copyright (c) 2014 The Go Authors. All rights reserved.
// https://github.com/golang/playground/blob/816964eae74f7612221c13ab73f2a8021c581010/sandbox/sandbox.go#L35-L38
Expand Down

0 comments on commit bec6126

Please sign in to comment.