feat: added basic http support. Renamed html to Render.

This commit is contained in:
Andrey Parhomenko 2024-07-31 21:21:24 +05:00
parent 15a6039b46
commit 0e45d090ab
8 changed files with 150 additions and 33 deletions

View file

@ -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] {

19
tests/http.xgo Normal file
View file

@ -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)

View file

@ -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) {

View file

@ -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,

View file

@ -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)

View file

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

View file

@ -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(

View file

@ -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
}