-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d2ded1f
Showing
10 changed files
with
358 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,7 @@ | ||
Copyright 2022 Jitter LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 @@ | ||
# Call |
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,142 @@ | ||
package call | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type CallOptions struct { | ||
Method string | ||
Base string | ||
Headers HeaderMap | ||
Query any | ||
Body any | ||
} | ||
|
||
func (c *CallOptions) GetUrl() (string, error) { | ||
return BuildUrl(c.Base, c.Query) | ||
} | ||
|
||
func (c *CallOptions) GetBody() (io.Reader, error) { | ||
data, err := json.Marshal(c.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewBuffer(data), nil | ||
} | ||
|
||
func (c *CallOptions) GetRequest() (*http.Request, error) { | ||
url, err := c.GetUrl() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.GetBody() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest(c.Method, url, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.updateHeader(req) | ||
|
||
return req, nil | ||
} | ||
|
||
func (c *CallOptions) updateHeader(req *http.Request) { | ||
for key, value := range c.Headers { | ||
req.Header.Add(key, value) | ||
} | ||
} | ||
|
||
func Call[R any]( | ||
options CallOptions, | ||
callback func(resp *http.Response, bytes []byte) (R, error), | ||
) (R, error) { | ||
var zero R | ||
|
||
req, err := options.GetRequest() | ||
if err != nil { | ||
return zero, err | ||
} | ||
|
||
httpClient := http.DefaultClient | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return zero, err | ||
} | ||
|
||
bytes, err := Read(resp) | ||
if err != nil { | ||
return zero, err | ||
} | ||
|
||
return callback(resp, bytes) | ||
} | ||
|
||
func NewCallOptions(options ...CallOptionFunction) CallOptions { | ||
c := CallOptions{ | ||
Method: "Get", | ||
Headers: HeaderMap{}, | ||
} | ||
for _, option := range options { | ||
option(&c) | ||
} | ||
return c | ||
} | ||
|
||
type CallOptionFunction func(c *CallOptions) | ||
|
||
func WithMethod(method string) CallOptionFunction { | ||
return func(c *CallOptions) { | ||
c.Method = method | ||
} | ||
} | ||
|
||
func WithBase(base string) CallOptionFunction { | ||
return func(c *CallOptions) { | ||
c.Base = base | ||
} | ||
} | ||
|
||
func WithQuery(q any) CallOptionFunction { | ||
return func(c *CallOptions) { | ||
c.Query = q | ||
} | ||
} | ||
|
||
func WithBody(b any) CallOptionFunction { | ||
return func(c *CallOptions) { | ||
c.Body = b | ||
} | ||
} | ||
|
||
func WithHeader(name string, value string) CallOptionFunction { | ||
return func(c *CallOptions) { | ||
c.Headers[name] = value | ||
} | ||
} | ||
|
||
func FromBytes[T any](data []byte) (*T, error) { | ||
var result T | ||
err := json.Unmarshal(data, result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &result, nil | ||
} | ||
|
||
func Read(resp *http.Response) ([]byte, error) { | ||
defer resp.Body.Close() | ||
bytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bytes, 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,103 @@ | ||
package call | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestWithMethod(t *testing.T) { | ||
want := "Post" | ||
|
||
c := NewCallOptions( | ||
WithMethod(want), | ||
) | ||
|
||
if c.Method != want { | ||
t.Errorf("Received: %v, Expected: %v", c.Base, want) | ||
} | ||
} | ||
|
||
func TestWithBase(t *testing.T) { | ||
want := "http://localhost" | ||
|
||
c := NewCallOptions( | ||
WithBase(want), | ||
) | ||
|
||
if c.Base != want { | ||
t.Errorf("Received: %v, Expected: %v", c.Base, want) | ||
} | ||
} | ||
|
||
func TestWithQuery(t *testing.T) { | ||
type TestQuery struct { | ||
Name string `url:"name"` | ||
} | ||
|
||
want := TestQuery{} | ||
|
||
c := NewCallOptions( | ||
WithQuery(want), | ||
) | ||
|
||
if c.Query != want { | ||
t.Errorf("Received: %v, Expected: %v", c.Query, want) | ||
} | ||
} | ||
|
||
func TestWithBody(t *testing.T) { | ||
type TestBody struct { | ||
Name string | ||
} | ||
|
||
want := TestBody{} | ||
|
||
c := NewCallOptions( | ||
WithBody(want), | ||
) | ||
|
||
if c.Body != want { | ||
t.Errorf("Received: %v, Expected: %v", c.Body, want) | ||
} | ||
} | ||
|
||
func TestWithHeader(t *testing.T) { | ||
name := "Content-Type" | ||
want := "application/json" | ||
|
||
c := NewCallOptions( | ||
WithHeader(name, want), | ||
) | ||
|
||
if c.Headers[name] != want { | ||
t.Errorf("Received: %v, Expected: %v", c.Headers[name], want) | ||
} | ||
} | ||
|
||
func TestCall(t *testing.T) { | ||
svc := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
})) | ||
|
||
type TestQuery struct { | ||
Name string `url:"name"` | ||
} | ||
|
||
result, err := Call(NewCallOptions( | ||
WithBase(svc.URL), | ||
WithHeader("Content-Type", "application/json"), | ||
WithQuery(TestQuery{ | ||
Name: "test", | ||
}), | ||
), func(resp *http.Response, bytes []byte) (string, error) { | ||
return "", nil | ||
}) | ||
|
||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
if result != "" { | ||
t.Errorf("%v", result) | ||
} | ||
} |
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,5 @@ | ||
module github.com/cardboardrobots/go-call | ||
|
||
go 1.18 | ||
|
||
require github.com/google/go-querystring v1.1.0 |
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,5 @@ | ||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= | ||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
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,33 @@ | ||
package call | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type HeaderMap = map[string]string | ||
|
||
type Header struct { | ||
*http.Header | ||
} | ||
|
||
func NewHeader(header *http.Header) Header { | ||
return Header{ | ||
Header: header, | ||
} | ||
} | ||
|
||
const HEADER_CONTENT_TYPE = "Content-Type" | ||
const HEADER_AUTHORIZATION = "Authorization" | ||
|
||
type ContentType string | ||
|
||
const CONTENT_TYPE_FORM_URLENCODED ContentType = "application/x-www-form-urlencoded; param=value" | ||
const CONTENT_TYPE_JSON ContentType = "application/json" | ||
|
||
func (h *Header) SetContentType(contentType ContentType) { | ||
h.Add(HEADER_CONTENT_TYPE, string(contentType)) | ||
} | ||
|
||
func (h *Header) SetAuthorization(authorization string) { | ||
h.Add(HEADER_AUTHORIZATION, authorization) | ||
} |
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,18 @@ | ||
package call | ||
|
||
type Result[T any, E any] struct { | ||
Value T | ||
Error E | ||
} | ||
|
||
func Ok[T any](value T) Result[T, any] { | ||
return Result[T, any]{ | ||
Value: value, | ||
} | ||
} | ||
|
||
func Err[E any](error E) Result[any, E] { | ||
return Result[any, E]{ | ||
Error: error, | ||
} | ||
} |
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,23 @@ | ||
package call | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/google/go-querystring/query" | ||
) | ||
|
||
func BuildUrl[T any](base string, options T) (string, error) { | ||
out, err := url.Parse(base) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
queryOptions, err := query.Values(options) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
out.RawQuery = queryOptions.Encode() | ||
|
||
return out.String(), 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,21 @@ | ||
package call | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBuildUrl(t *testing.T) { | ||
type TestQuery struct { | ||
Name string `url:"name"` | ||
} | ||
|
||
url, err := BuildUrl("http://localhost:8080", TestQuery{Name: "test"}) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
want := "http://localhost:8080?name=test" | ||
if url != want { | ||
t.Errorf("Received: %v, Expected: %v", url, want) | ||
} | ||
} |