-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_template.go
More file actions
48 lines (38 loc) · 853 Bytes
/
get_template.go
File metadata and controls
48 lines (38 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gowandbox
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
/*
Returns the template for a given language.
If the language is not found, an error is returned (bad template).
Maps to the `/template/:template` endpoint
*/
func GetTemplate(language string, ctx context.Context) (GWBTemplate, error) {
client := http.DefaultClient
templ := GWBTemplate{}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
WandBoxUrl+"template/"+language,
nil,
)
if err != nil {
return templ, err
}
resp, err := client.Do(req)
if err != nil {
return templ, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
e, _ := ioutil.ReadAll(resp.Body)
return templ, errors.New(string(e))
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&templ)
return templ, err
}