-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (59 loc) · 1.48 KB
/
main.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"io"
"net/http"
"github.com/go-python/gpython/py"
_ "github.com/go-python/gpython/stdlib"
)
func main() {
py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "httpgetlib_go",
Doc: "Simple http client for GPython",
},
Methods: []*py.Method{
py.MustNewMethod("HttpGet", HttpGet, 0, ""),
},
})
ctx := py.NewContext(py.DefaultContextOpts())
defer ctx.Close()
var err error
const moduleDescr = "Simplest GPython HTTP client module. Just a wrapper for Golang implementation."
moduleCode := `
import httpgetlib_go
def Get(url):
return httpgetlib_go.HttpGet(url)
`
codeModCompiled, err := py.Compile(moduleCode, moduleDescr, py.ExecMode, 0, true)
if err != nil {
py.TracebackDump(err)
}
_, err = py.RunCode(ctx, codeModCompiled, moduleDescr, "httpclient")
if err != nil {
py.TracebackDump(err)
}
const runtimeDescr = "<runtime>"
mainCode := `
import httpclient
print(httpclient.Get('https://httpbin.org/anything'))
`
codeCompiled, err := py.Compile(mainCode, runtimeDescr, py.ExecMode, 0, true)
if err != nil {
py.TracebackDump(err)
}
_, err = py.RunCode(ctx, codeCompiled, runtimeDescr, nil)
if err != nil {
py.TracebackDump(err)
}
}
func HttpGet(self py.Object, urlObj py.Object) (py.Object, error) {
pyStr, _ := urlObj.(py.String)
url := string(pyStr)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return py.String(string(body)), err
}