@@ -5,14 +5,16 @@ import (
55 "context"
66 "fmt"
77 "io"
8+ "net"
89 "net/http"
910 "net/url"
1011 "strings"
12+ "time"
1113)
1214
1315const (
14- userAgent = "goplay.x1unix.com /1.0 (http://goplay.x1unix.com /)"
15- goPlayURL = "https://play.golang.org"
16+ defaultUserAgent = "goplay.tools /1.0 (http://goplay.tools /)"
17+ playgroundUrl = "https://play.golang.org"
1618
1719 // maxSnippetSize value taken from
1820 // https://github.com/golang/playground/blob/master/app/goplay/share.go
@@ -22,33 +24,61 @@ const (
2224// ErrSnippetTooLarge is snippet max size limit error
2325var ErrSnippetTooLarge = fmt .Errorf ("code snippet too large (max %d bytes)" , maxSnippetSize )
2426
25- func newRequest (ctx context.Context , method , queryPath string , body io.Reader ) (* http.Request , error ) {
26- uri := goPlayURL + "/" + queryPath
27+ type Client struct {
28+ client http.Client
29+ baseUrl string
30+ userAgent string
31+ }
32+
33+ // NewClient returns new Go playground client
34+ func NewClient (baseUrl , userAgent string , timeout time.Duration ) * Client {
35+ return & Client {
36+ baseUrl : baseUrl ,
37+ userAgent : userAgent ,
38+ client : http.Client {
39+ Timeout : timeout ,
40+ Transport : & http.Transport {
41+ DialContext : (& net.Dialer {
42+ Timeout : timeout ,
43+ }).DialContext ,
44+ TLSHandshakeTimeout : timeout ,
45+ },
46+ },
47+ }
48+ }
49+
50+ // NewDefaultClient returns Go Playground client with defaults
51+ func NewDefaultClient () * Client {
52+ return NewClient (playgroundUrl , defaultUserAgent , 15 * time .Second )
53+ }
54+
55+ func (c * Client ) newRequest (ctx context.Context , method , queryPath string , body io.Reader ) (* http.Request , error ) {
56+ uri := c .baseUrl + "/" + queryPath
2757 req , err := http .NewRequestWithContext (ctx , method , uri , body )
2858 if err != nil {
2959 return nil , err
3060 }
3161
32- req .Header .Add ("User-Agent" , userAgent )
62+ req .Header .Add ("User-Agent" , c . userAgent )
3363 return req , nil
3464}
3565
36- func getRequest (ctx context.Context , queryPath string ) (* http.Response , error ) {
37- req , err := newRequest (ctx , http .MethodGet , queryPath , nil )
66+ func ( c * Client ) getRequest (ctx context.Context , queryPath string ) (* http.Response , error ) {
67+ req , err := c . newRequest (ctx , http .MethodGet , queryPath , nil )
3868 if err != nil {
3969 return nil , nil
4070 }
4171
42- return http . DefaultClient .Do (req )
72+ return c . client .Do (req )
4373}
4474
45- func doRequest (ctx context.Context , method , url , contentType string , body io.Reader ) ([]byte , error ) {
46- req , err := newRequest (ctx , method , url , body )
75+ func ( c * Client ) doRequest (ctx context.Context , method , url , contentType string , body io.Reader ) ([]byte , error ) {
76+ req , err := c . newRequest (ctx , method , url , body )
4777 if err != nil {
4878 return nil , err
4979 }
5080 req .Header .Add ("Content-Type" , contentType )
51- response , err := http . DefaultClient .Do (req )
81+ response , err := c . client .Do (req )
5282 if err != nil {
5383 return nil , err
5484 }
@@ -66,8 +96,8 @@ func doRequest(ctx context.Context, method, url, contentType string, body io.Rea
6696 return bodyBytes .Bytes (), nil
6797}
6898
69- func postForm (ctx context.Context , url string , data url.Values ) ([]byte , error ) {
70- return doRequest (
99+ func ( c * Client ) postForm (ctx context.Context , url string , data url.Values ) ([]byte , error ) {
100+ return c . doRequest (
71101 ctx , "POST" , url ,
72102 "application/x-www-form-urlencoded" , strings .NewReader (data .Encode ()),
73103 )
0 commit comments