Skip to content

Commit 82021d1

Browse files
Finished docs
1 parent 19d6b12 commit 82021d1

File tree

5 files changed

+158
-7
lines changed

5 files changed

+158
-7
lines changed

docs/Utils.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Utilities
2+
3+
GoWandBox also offers a couple of utilities, most of which wrap around the WandBox API -
4+
5+
## List Supported Languages
6+
7+
```go
8+
languages, err := gwb.GetLanguages(10000) // Timeout of 10000 milliseconds
9+
10+
if err != nil {
11+
log.Fatal(err)
12+
}
13+
14+
for _, lang := range languages() {
15+
fmt.Printf("Language - %v\n", lang.DisplayName) // Name of the language
16+
fmt.Println(lang.Name) // Name with version
17+
fmt.Printf("Version - %v\n", lang.Version)
18+
fmt.Printf("Used with - %v\n", lang.DisplayCompileCommand) // Command used to compile/run
19+
fmt.Printf(
20+
"Supports Compiler Options - %v\nSupports Runtime Options - %v\n",
21+
lang.CompilerOptionRaw,
22+
lang.RuntimeOptionRaw,
23+
)
24+
fmt.Printf("Switches - %v\n", lang.Switches) // lang.Switches is an array of switches
25+
fmt.Printf("Template Names - %v\n", lang.Templates)
26+
// lang.Templates is an array of template names,
27+
// which can be obtained with the GetTemplate() function
28+
29+
fmt.Println("--------------------------------")
30+
}
31+
```
32+
33+
```
34+
$ go run main.go
35+
Language - gcc HEAD
36+
gcc-head-c
37+
Version - 12.0.0 20210721 (experimental)
38+
Used with - gcc prog.c
39+
Supports Compiler Options - true
40+
Supports Runtime Options - false
41+
Switches - [{true warning -Wall -Wextra Warnings} {false optimize -O2 -march=native Optimization} {false cpp-verbose -v Verbose} {gnu11 std-c } {cpp-no-pedantic cpp-pedantic }]
42+
Template Names - [gcc-c]
43+
--------------------------------
44+
Language - gcc
45+
gcc-11.1.0-c
46+
Version - 11.1.0
47+
Used with - gcc prog.c
48+
Supports Compiler Options - true
49+
Supports Runtime Options - false
50+
Switches - [{true warning -Wall -Wextra Warnings} {false optimize -O2 -march=native Optimization} {false cpp-verbose -v Verbose} {gnu11 std-c } {cpp-no-pedantic cpp-pedantic }]
51+
Template Names - [gcc-c]
52+
--------------------------------
53+
......
54+
```
55+
56+
## Get Template
57+
58+
You can get the template for a language, with the `GetTemplate()` function.
59+
60+
```go
61+
template, err := gwb.GetTemplate("cpython", 10000)
62+
// Get the template for `cpython`, with a 10000 millisecond timeout
63+
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
68+
fmt.Println(template.Code)
69+
```
70+
71+
```
72+
$ go run main.go
73+
74+
# This file is a "Hello, world!" in Python language by CPython for wandbox.
75+
76+
print("Hello, world!")
77+
78+
# CPython references:
79+
# https://www.python.org/
80+
```
81+
82+
## Get User State
83+
84+
You can view the state of a WandBox user, given their SessionKey (Provided by WandBox) -
85+
```go
86+
user, err := gwb.GetUser("session-key", 10000)
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
91+
fmt.Printf("Username: %v\n", user.Username)
92+
fmt.Printf("Logged in Currently: %v\n", user.Login)
93+
```
94+
95+
```
96+
$ go run main.go
97+
Username: ABCDEFGH
98+
Logged in Currently: true
99+
```
100+
101+
## Get PermLinks
102+
103+
If you have the the PermLink of a program that was executed, and saved, you can retrieve information about it, such as the code, input, compiler used, time it was executed at, output, errors, etc.
104+
105+
```go
106+
result, err := GetPermLink("permlink", 10000) // Timeout of 10000 milliseconds
107+
108+
if err != nil {
109+
log.Fatal(err)
110+
}
111+
112+
fmt.Printf("Code: %v\n", result.Parameter.Code)
113+
fmt.Printf("Files: %v\n", result.Parameter.Codes)
114+
fmt.Printf("Compiled with: %v\n", result.Parameter.Compiler)
115+
116+
fmt.Printf("Output: %v\n", result.Result.ProgramMessage)
117+
fmt.Printf("Exit Code: %v\n", result.Result.Status)
118+
```
119+
120+
```
121+
$ go run main.go
122+
Code: print(123)
123+
Files: []
124+
Compiled with: cpython-head
125+
Output: 123
126+
127+
Exit Code: 0
128+
```
129+
130+
For a detailed list of information available, check out the pkg.go.dev page - [https://pkg.go.dev/github.com/classPythonAddike/gowandbox](https://pkg.go.dev/github.com/classPythonAddike/gowandbox#GWBPermLink)
131+
132+
## Change WandBox API URL
133+
134+
You can use your own instance of WandBox, instead of the default one with `ChangeWandBoxUrl()`
135+
136+
```go
137+
gwb.ChangeWandBoxUrl("https://my.wandboxinstance.com/api/")
138+
```
139+
140+
To reset the WandBox URL -
141+
```go
142+
gwb.ResetWandBoxUrl()
143+
```

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* [Home](/)
22
* [Compiling](/Compiling.md)
3+
* [Utils](/Utils.md)

get_template.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gowandbox
22

33
import (
4+
"encoding/json"
45
"errors"
56
"io/ioutil"
67
"net/http"
@@ -12,27 +13,29 @@ import (
1213
If the language is not found, an error is returned (bad template).
1314
Maps to the `/template/:template` endpoint
1415
*/
15-
func GetTemplate(language string, timeout int) (string, error) {
16+
func GetTemplate(language string, timeout int) (GWBTemplate, error) {
1617

1718
client := http.Client{
1819
Timeout: time.Duration(timeout) * time.Millisecond,
1920
}
2021

22+
templ := GWBTemplate{}
23+
2124
resp, err := client.Get(
2225
WandBoxUrl + "template/" + language,
2326
)
2427

2528
if err != nil {
26-
return "", err
29+
return templ, err
2730
}
2831

2932
if resp.StatusCode != http.StatusOK {
3033
defer resp.Body.Close()
3134
e, _ := ioutil.ReadAll(resp.Body)
32-
return "", errors.New(string(e))
35+
return templ, errors.New(string(e))
3336
}
3437

3538
defer resp.Body.Close()
36-
res, err := ioutil.ReadAll(resp.Body)
37-
return string(res), err
39+
err = json.NewDecoder(resp.Body).Decode(&templ)
40+
return templ, err
3841
}

get_template_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
)
77

88
func TestTemplate(t *testing.T) {
9-
_, err := GetTemplate("cpython", 10000)
9+
template, err := GetTemplate("cpython", 10000)
1010

1111
if err != nil {
1212
t.Error(err.Error())
1313
}
1414

15-
t.Log("Got template for cpython")
15+
t.Logf("Got template for cpython - %v", template.Code)
1616
}
1717

1818
func TestBadTemplateError(t *testing.T) {

types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,7 @@ type GWBPermLink struct {
205205
ProgramMessage string `json:"program_message"`
206206
} `json:"result"`
207207
}
208+
209+
type GWBTemplate struct {
210+
Code string `json:"code"`
211+
}

0 commit comments

Comments
 (0)