Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
milindmadhukar committed Nov 22, 2021
1 parent a591646 commit 64da610
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package main

import (
"fmt"
"log"

piston "github.com/milindmadhukar/go-piston"
)
Expand All @@ -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())
}
Expand Down
3 changes: 2 additions & 1 deletion _examples/with_input/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

piston "github.com/milindmadhukar/go-piston"
)
Expand All @@ -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())
}
6 changes: 3 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand Down
16 changes: 8 additions & 8 deletions piston.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
}
}

Expand All @@ -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,
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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
Expand Down

0 comments on commit 64da610

Please sign in to comment.