From 0e45d090abc5d74cab03f946f62c1c389449c956 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 31 Jul 2024 21:21:24 +0500 Subject: [PATCH] feat: added basic http support. Renamed html to Render. --- tests/html.xgo | 3 +- tests/http.xgo | 19 +++++++++++ xmodules/cjson/dec.go | 4 +-- xmodules/cjson/main.go | 8 ----- xmodules/htmlx/html.go | 28 ++++++----------- xmodules/httpx/module.go | 64 ++++++++++++++++++++++++++++++++++++++ xmodules/httpx/request.go | 6 ++-- xmodules/httpx/response.go | 51 ++++++++++++++++++++++++++++++ 8 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 tests/http.xgo create mode 100644 xmodules/httpx/response.go diff --git a/tests/html.xgo b/tests/html.xgo index a9699b5..10db378 100644 --- a/tests/html.xgo +++ b/tests/html.xgo @@ -1,5 +1,6 @@ fmt := import("fmt") -html := import("html").new_render() +http := import("http") +html := import("html").Renderer() added := html.ul() for i in [1, 2, 3, 4, 5] { diff --git a/tests/http.xgo b/tests/http.xgo new file mode 100644 index 0000000..3b7c42f --- /dev/null +++ b/tests/http.xgo @@ -0,0 +1,19 @@ +fmt := import("fmt") +http := import("http") +os := import("os") + +resp := http.post_json( + "https://jsonplaceholder.typicode.com/posts", { + title: "Hello, World!", + body: "Checking shit out!", + userId: 1 + } +) +if is_error(resp) { + fmt.println(resp) + os.exit(1) +} + +body := resp.body_json +fmt.println(body) + diff --git a/xmodules/cjson/dec.go b/xmodules/cjson/dec.go index 84d4d2e..276cb42 100644 --- a/xmodules/cjson/dec.go +++ b/xmodules/cjson/dec.go @@ -15,11 +15,11 @@ type Decoder struct { } func (d *Decoder) TypeName() string { - return "*sjson.Decoder" + return "cjson.Decoder" } func (d *Decoder) String() string { - return "sjson.Decoder{...}" + return "cjson.Decoder{...}" } func NewDecoder(args ...tengo.Object) (tengo.Object, error) { diff --git a/xmodules/cjson/main.go b/xmodules/cjson/main.go index 8c16261..7bd5f8c 100644 --- a/xmodules/cjson/main.go +++ b/xmodules/cjson/main.go @@ -10,14 +10,6 @@ const ( ) var Module = map[string]tengo.Object{ - "__check": &tengo.BuiltinFunction{ - Name: "__check", - Value: func( - args ...tengo.Object, - ) (tengo.Object, error) { - return tengo.FromInterface("Hello, Check!") - }, - }, "new_decoder": &tengo.BuiltinFunction{ Name: "new_decoder", Value: NewDecoder, diff --git a/xmodules/htmlx/html.go b/xmodules/htmlx/html.go index 8576612..db2ba4c 100644 --- a/xmodules/htmlx/html.go +++ b/xmodules/htmlx/html.go @@ -1,37 +1,27 @@ package htmlx import "github.com/d5/tengo/v2" -import "log" var Module = map[string] tengo.Object{ - "new_render" : &tengo.UserFunction{ - Name: "new_render", + "Renderer" : &tengo.BuiltinFunction{ + Name: "Renderer", Value: func(args ...tengo.Object) (tengo.Object, error){ - ret := &HTML{} + ret := &Renderer{} return ret, nil }, }, } -type HTML struct{ +type Renderer struct{ tengo.ObjectImpl } -func (html *HTML) Import(moduleName string) (any, error) { - log.Println("import:", moduleName) - switch moduleName { - case "html": - return html, nil - } - return nil, nil +func (html *Renderer) TypeName() string { + return "htmlx.Renderer" } -func (html *HTML) TypeName() string { - return "blablabla" -} - -func (html *HTML) String() (string) { - return "HTML{...}" +func (html *Renderer) String() (string) { + return "htmlx.Renderer{...}" } /* @@ -46,7 +36,7 @@ html.div({ */ -func (html *HTML) IndexGet( +func (html *Renderer) IndexGet( index tengo.Object, ) (tengo.Object, error) { str, ok := tengo.ToString(index) diff --git a/xmodules/httpx/module.go b/xmodules/httpx/module.go index 04534c2..80bbd3d 100644 --- a/xmodules/httpx/module.go +++ b/xmodules/httpx/module.go @@ -1,7 +1,71 @@ package httpx import "github.com/d5/tengo/v2" +import "net/http" +import "bytes" +import tjson "github.com/d5/tengo/v2/stdlib/json" var Module = map[string] tengo.Object { + "get": &tengo.BuiltinFunction{ + Name: "get", + Value: func( + args ...tengo.Object, + ) (tengo.Object, error){ + if len(args) != 1 { + return nil, tengo.ErrWrongNumArguments + } + url, ok := tengo.ToString(args[0]) + if !ok { + return nil, tengo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string", + Found: args[0].TypeName(), + } + } + resp, err := http.Get(url) + if err != nil { + return nil, err + } + return &Response{ + Response: resp, + }, nil + }, + }, + "post_json": &tengo.BuiltinFunction{ + Name: "post", + Value: func( + args ...tengo.Object, + ) (tengo.Object, error){ + if len(args) < 2 { + return nil, tengo.ErrWrongNumArguments + } + url, ok := tengo.ToString(args[0]) + if !ok { + return nil, tengo.ErrInvalidArgumentType{ + Name: "first", + Expected: "string", + Found: args[0].TypeName(), + } + } + + var body bytes.Buffer + ret, err := tjson.Encode(args[1]) + if err != nil { + return nil, err + } + body.Write(ret) + + resp, err := http.Post( + url, "application/json", + &body, + ) + if err != nil { + return nil, err + } + return &Response{ + Response: resp, + }, nil + }, + }, } diff --git a/xmodules/httpx/request.go b/xmodules/httpx/request.go index 8f72830..30f6d91 100644 --- a/xmodules/httpx/request.go +++ b/xmodules/httpx/request.go @@ -8,16 +8,16 @@ import ( ) type Request struct { - tengo.ObjectImpl *http.Request + tengo.ObjectImpl } func (r *Request) TypeName() string { - return "*http.Request" + return "http.Request" } func (r *Request) String() string { - return "*http.Request{...}" + return "http.Request{...}" } func (r *Request) IndexGet( diff --git a/xmodules/httpx/response.go b/xmodules/httpx/response.go new file mode 100644 index 0000000..14ae6b6 --- /dev/null +++ b/xmodules/httpx/response.go @@ -0,0 +1,51 @@ +package httpx + +import "net/http" +import "github.com/d5/tengo/v2" +import "io" +//import "encoding/json" +import tjson "github.com/d5/tengo/v2/stdlib/json" + +type Response struct { + *http.Response + tengo.ObjectImpl +} + +func (r *Response) TypeName() string { + return "http.Response" +} + +func (r *Response) String() string { + return "http.Response{...}" +} + +func (r *Response) IndexGet( + index tengo.Object, +) (tengo.Object, error) { + key, ok := tengo.ToString(index) + if !ok { + return nil, tengo.ErrInvalidIndexValueType + } + + switch key { + case "body" : + bts, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + return &tengo.Bytes{ + Value: bts, + }, nil + case "body_json" : + bts, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + + return tjson.Decode(bts) + } + + // Nothing found. + return nil, nil +} +