-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http post #10
Open
Vialeon
wants to merge
8
commits into
main
Choose a base branch
from
http_post
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−18
Open
Http post #10
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c32f345
testing for http
Vialeon d70883a
mod and sum
Vialeon 3a59572
fix makefile and workflow tests
Vialeon bc10c62
change name of read_file to fit previous semantic styling
Vialeon 40f108c
Merge branch 'main' into http_testing
Vialeon 4cc9481
add post request functionality with testing
Vialeon d2e182e
remove TODO's
Vialeon 7d85bb2
rm TODO in get
Vialeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
2 changes: 1 addition & 1 deletion
2
internal/readfile/readfile.go → internal/file_read/file_read.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package readfile | ||
package file_read | ||
|
||
import ( | ||
"io/ioutil" | ||
|
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,29 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func ParseHeaders(headers string) [][]string { | ||
headerList := strings.Split(headers, "|") | ||
var kvHeaders [][]string | ||
for _, s := range headerList { | ||
st := strings.Split(s, ":") | ||
for i := range st { | ||
st[i] = strings.TrimSpace(st[i]) | ||
} | ||
kvHeaders = append(kvHeaders, st) | ||
} | ||
return kvHeaders | ||
} | ||
func HttpRequest(requestUrl string, headers [][]string, requestType string) (*http.Request, error) { | ||
request, err := http.NewRequest(requestType, requestUrl, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, header := range headers { | ||
request.Header.Add(header[0], header[1]) | ||
} | ||
return request, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
_ "github.com/augmentable-dev/flite/internal/sqlite" | ||
"github.com/jmoiron/sqlx" | ||
_ "github.com/mattn/go-sqlite3" | ||
"go.riyazali.net/sqlite" | ||
) | ||
|
||
func TestHeaderParser(t *testing.T) { | ||
header := "Content-Type: application/json |Accept: application/json|accept-encoding: application/gzip|x-api-key: 09187304915uqiyoewue90832174109732y6985132" | ||
expected := [][]string{{"Content-Type", "application/json"}, {"Accept", "application/json"}, {"accept-encoding", "application/gzip"}, {"x-api-key", "09187304915uqiyoewue90832174109732y6985132"}} | ||
parsed := ParseHeaders(header) | ||
for i := range expected { | ||
if expected[i][0] != parsed[i][0] || expected[i][1] != parsed[i][1] { | ||
t.Fatalf("expected %s,%s got %s,%s", expected[i][0], expected[i][1], parsed[i][0], parsed[i][1]) | ||
} | ||
} | ||
} | ||
|
||
type mockRoundTripper struct { | ||
f func(*http.Request) (*http.Response, error) | ||
} | ||
|
||
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return m.f(req) | ||
} | ||
func newMockRoundTripper(f func(*http.Request) (*http.Response, error)) *mockRoundTripper { | ||
return &mockRoundTripper{f: f} | ||
} | ||
func TestHTTPGet(t *testing.T) { | ||
getFunc := NewHTTPGet() | ||
f := getFunc.(*get) | ||
url := "https://some-url.com/v1/some-endpoint.json" | ||
body := "OK" | ||
|
||
f.client.Transport = newMockRoundTripper(func(req *http.Request) (*http.Response, error) { | ||
if req.URL.String() != url { | ||
t.Fatalf("expected request URL: %s, got: %s", url, req.URL.String()) | ||
} | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: ioutil.NopCloser(bytes.NewBufferString(body)), | ||
Header: make(http.Header), | ||
}, nil | ||
}) | ||
sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { | ||
if err := api.CreateFunction("http_get", getFunc); err != nil { | ||
return sqlite.SQLITE_ERROR, err | ||
} | ||
return sqlite.SQLITE_OK, nil | ||
}) | ||
db, err := sqlx.Open("sqlite3", ":memory:") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer db.Close() | ||
|
||
row := db.QueryRow("SELECT http_get($1)", url) | ||
err = row.Err() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var res string | ||
err = row.Scan(&res) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res != body { | ||
t.Fatalf("expected response: %s, got: %s", body, res) | ||
} | ||
} | ||
func TestHTTPPost(t *testing.T) { | ||
postFunc := NewHTTPpost() | ||
f := postFunc.(*post) | ||
url := "https://some-url.com/v1/some-endpoint.json" | ||
body := "OK" | ||
header := "Content-Type: application/json |Accept: application/json|accept-encoding: application/gzip|x-api-key: 09187304915uqiyoewue90832174109732y6985132" | ||
expected := map[string][]string{"Content-Type": {"application/json"}, "Accept": {"application/json"}, "accept-encoding": {"application/gzip"}, "x-api-key": {"09187304915uqiyoewue90832174109732y6985132"}} | ||
f.client.Transport = newMockRoundTripper(func(req *http.Request) (*http.Response, error) { | ||
if req.URL.String() != url { | ||
t.Fatalf("expected request URL: %s, got: %s", url, req.URL.String()) | ||
} | ||
if req.Header.Get("Content-Type") != expected["Content-Type"][0] { | ||
t.Fatalf("expected Header Content-Type: %s got: %s", expected["Content-type"], req.Header.Get("Content-Type")) | ||
} | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: ioutil.NopCloser(bytes.NewBufferString(body)), | ||
Header: expected, | ||
}, nil | ||
}) | ||
sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) { | ||
if err := api.CreateFunction("http_post", postFunc); err != nil { | ||
return sqlite.SQLITE_ERROR, err | ||
} | ||
return sqlite.SQLITE_OK, nil | ||
}) | ||
db, err := sqlx.Open("sqlite3", ":memory:") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer db.Close() | ||
|
||
row := db.QueryRow("SELECT http_post($1,$2)", url, header) | ||
err = row.Err() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var res string | ||
err = row.Scan(&res) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if res != body { | ||
t.Fatalf("expected response: %s, got: %s", body, res) | ||
} | ||
} | ||
func TestHttpRequest(t *testing.T) { | ||
method := "GET" | ||
url := "http://api.citybik.es/v2/networks" | ||
responseRecorder := httptest.NewRecorder() | ||
headers := [][]string{{"Content-Type", "application/json"}} | ||
req, err := HttpRequest(url, headers, method) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
handler := http.HandlerFunc(httpHandler) | ||
handler.ServeHTTP(responseRecorder, req) | ||
if status := responseRecorder.Code; status != http.StatusOK { | ||
t.Fatal(err) | ||
} | ||
expected := `{"alive": true}` | ||
if responseRecorder.Body.String() != expected { | ||
t.Fatalf("received %s expected %s", responseRecorder.Body.String(), expected) | ||
} | ||
|
||
} | ||
func httpHandler(w http.ResponseWriter, r *http.Request) { | ||
println(r.URL.String()) | ||
if r.URL.String() != "http://api.citybik.es/v2/networks" { | ||
w.Header().Set("Content-Type", "application/json") | ||
io.WriteString(w, `{"response": "incorrect url"`) | ||
return | ||
} | ||
if r.Header.Get("Content-Type") != "application/json" { | ||
w.Header().Set("Content-Type", "application/json") | ||
io.WriteString(w, `{"response": "incorrect header"`) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
io.WriteString(w, `{"alive": true}`) | ||
} |
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,53 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"go.riyazali.net/sqlite" | ||
) | ||
|
||
type post struct { | ||
client *http.Client | ||
} | ||
|
||
// TODO add PUT and POST stuff | ||
|
||
func (f *post) Args() int { return -1 } | ||
func (f *post) Deterministic() bool { return false } | ||
func (f *post) Apply(ctx *sqlite.Context, values ...sqlite.Value) { | ||
var ( | ||
url string | ||
headers [][]string | ||
err error | ||
contents []byte | ||
request *http.Request | ||
) | ||
if len(values) > 1 { | ||
url = values[0].Text() | ||
heads := values[1].Text() | ||
headers = ParseHeaders(heads) | ||
} else { | ||
err := errors.New("input a url with headers as the argument to post") | ||
ctx.ResultError(err) | ||
} | ||
request, err = HttpRequest(url, headers, "POST") | ||
if err != nil { | ||
ctx.ResultError(err) | ||
} | ||
response, err := f.client.Do(request) | ||
if err != nil { | ||
ctx.ResultError(err) | ||
} | ||
contents, err = ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
ctx.ResultError(err) | ||
} | ||
ctx.ResultText(string(contents)) | ||
} | ||
|
||
// NewHTTPpost returns a sqlite function for reading the contents of a file | ||
func NewHTTPpost() sqlite.Function { | ||
return &post{http.DefaultClient} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove this probably