-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: Simple Get request and response plus example
- Loading branch information
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |