Skip to content

Commit

Permalink
http: Simple Get request and response plus example
Browse files Browse the repository at this point in the history
  • Loading branch information
Catya3 committed May 1, 2024
1 parent b80bf3c commit 924aa0f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/http/get/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test

import (
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
err := os.Chdir("../..")
require.NoError(t, err)

wd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(wd)

cmd := exec.Command("neva", "run", "http/get")

out, err := cmd.CombinedOutput()
require.NoError(t, err)
require.Contains(
t,
string(out),
"<html>",
)

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
14 changes: 14 additions & 0 deletions examples/http/get/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
http
}

component Main(start) (stop) {
nodes {
http.Get
Println
}
net {
:start -> [('http://www.example.com' -> get:url)]
[get:resp.body, get:err] -> println -> :stop
}
}
70 changes: 70 additions & 0 deletions internal/runtime/funcs/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package funcs

import (
"context"
goio "io"
"net/http"

"github.com/nevalang/neva/internal/runtime"
)

type httpGet struct{}

func (httpGet) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
urlIn, err := io.In.Port("url")
if err != nil {
return nil, err
}

respOut, err := io.Out.Port("resp")
if err != nil {
return nil, err
}

errOut, err := io.Out.Port("err")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
for {
var u string
select {
case m := <-urlIn:
u = m.Str()
case <-ctx.Done():
return
}
resp, err := http.Get(u)
if err != nil {
select {
case errOut <- runtime.NewMapMsg(map[string]runtime.Msg{
"text": runtime.NewStrMsg(err.Error()),
}):
continue
case <-ctx.Done():
return
}
}
body, err := goio.ReadAll(resp.Body)
if err != nil {
select {
case errOut <- runtime.NewMapMsg(map[string]runtime.Msg{
"text": runtime.NewStrMsg(err.Error()),
}):
continue
case <-ctx.Done():
return
}
}
select {
case respOut <- runtime.NewMapMsg(map[string]runtime.Msg{
"statusCode": runtime.NewIntMsg(int64(resp.StatusCode)),
"body": runtime.NewStrMsg(string(body)),
}):
case <-ctx.Done():
return
}
}
}, nil
}
2 changes: 2 additions & 0 deletions internal/runtime/funcs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator {
// io/file
"read_all": readAll{},
"write_all": writeAll{},
// http
"http_get": httpGet{},
}
}
7 changes: 7 additions & 0 deletions std/http/http.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub type Response struct {
statusCode int
body string
}

#extern(http_get)
pub component Get(url string) (resp Response, err error)

0 comments on commit 924aa0f

Please sign in to comment.