From 64da61081d05fd4c05198bf62c2262f99a25f2d6 Mon Sep 17 00:00:00 2001 From: milindmadhukar Date: Mon, 22 Nov 2021 17:32:54 +0530 Subject: [PATCH] Minor fixes. --- README.md | 3 ++- _examples/with_input/main.go | 3 ++- models.go | 6 +++--- piston.go | 16 ++++++++-------- utils.go | 5 ++--- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6c97a58..df8f8ad 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ package main import ( "fmt" + "log" piston "github.com/milindmadhukar/go-piston" ) @@ -36,7 +37,7 @@ func main() { piston.Stdin("hello world"), // Passing input as "hello world". ) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println(output.GetOutput()) } diff --git a/_examples/with_input/main.go b/_examples/with_input/main.go index 9d4d41d..76c5e72 100644 --- a/_examples/with_input/main.go +++ b/_examples/with_input/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" piston "github.com/milindmadhukar/go-piston" ) @@ -15,7 +16,7 @@ func main() { piston.Stdin("hello world"), // Passing input as "hello world". ) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println(output.GetOutput()) } diff --git a/models.go b/models.go index ce846f6..a2c9381 100644 --- a/models.go +++ b/models.go @@ -8,9 +8,9 @@ import ( Client struct to allow the usage of Piston API endpoints. */ type Client struct { - httpClient *http.Client - baseUrl string - apiKey string + HttpClient *http.Client + BaseURL string + ApiKey string } /* diff --git a/piston.go b/piston.go index 7719245..edc7143 100644 --- a/piston.go +++ b/piston.go @@ -11,9 +11,9 @@ Creates a default client object and returns it for access to the methods. */ func CreateDefaultClient() *Client { return &Client{ - httpClient: http.DefaultClient, - baseUrl: "https://emkc.org/api/v2/piston/", - apiKey: "", + HttpClient: http.DefaultClient, + BaseURL: "https://emkc.org/api/v2/piston/", + ApiKey: "", } } @@ -22,9 +22,9 @@ Creates a Client object which allows the use of custom url and api key. */ func New(apiKey string, httpClient *http.Client, baseUrl string) *Client { return &Client{ - httpClient: httpClient, - baseUrl: baseUrl, - apiKey: apiKey, + HttpClient: httpClient, + BaseURL: baseUrl, + ApiKey: apiKey, } } @@ -33,7 +33,7 @@ This endpoint will return the supported languages along with the current version To execute code for a particular language using the Execute() function, either the name or one of the aliases must be provided, along with the version. Multiple versions of the same language may be present at the same time, and may be selected when running a job. */ func (client *Client) GetRuntimes() (*Runtimes, error) { - resp, err := client.handleRequest("GET", client.baseUrl+"runtimes", nil) + resp, err := client.handleRequest("GET", client.BaseURL+"runtimes", nil) if err != nil { return nil, err } @@ -120,7 +120,7 @@ func (client *Client) Execute(language string, version string, code []Code, para } // Sending the POST request to the Piston API. - resp, err := client.handleRequest("POST", client.baseUrl+"execute", body) + resp, err := client.handleRequest("POST", client.BaseURL+"execute", body) if err != nil { return nil, err } diff --git a/utils.go b/utils.go index f7a9e90..4896bba 100644 --- a/utils.go +++ b/utils.go @@ -123,7 +123,6 @@ func handleStatusCode(code int, respBody string) error { // Handles sending the request to the Piston API and returing a response. func (client *Client) handleRequest(method string, url string, body *bytes.Reader) (*http.Response, error) { - // HACK: Why is this needed? if body == nil { body = &bytes.Reader{} } @@ -132,11 +131,11 @@ func (client *Client) handleRequest(method string, url string, body *bytes.Reade return nil, err } - if apiKey := client.apiKey; apiKey != "" { + if apiKey := client.ApiKey; apiKey != "" { req.Header.Add("Authorization", apiKey) } - resp, err := client.httpClient.Do(req) + resp, err := client.HttpClient.Do(req) if err != nil { return nil, err