From d2ded1fe61cfe7f78e8742413309bdea85551f2e Mon Sep 17 00:00:00 2001 From: Sean Johnson Date: Sun, 7 Aug 2022 10:20:44 -0700 Subject: [PATCH] Initial commit. --- License | 7 +++ Readme.md | 1 + call.go | 142 +++++++++++++++++++++++++++++++++++++++++++++++ call_test.go | 103 ++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 5 ++ header.go | 33 +++++++++++ result.go | 18 ++++++ url_util.go | 23 ++++++++ url_util_test.go | 21 +++++++ 10 files changed, 358 insertions(+) create mode 100644 License create mode 100644 Readme.md create mode 100644 call.go create mode 100644 call_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 header.go create mode 100644 result.go create mode 100644 url_util.go create mode 100644 url_util_test.go diff --git a/License b/License new file mode 100644 index 0000000..fa5ac79 --- /dev/null +++ b/License @@ -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. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..1c0e2cb --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +# Call diff --git a/call.go b/call.go new file mode 100644 index 0000000..deb01b8 --- /dev/null +++ b/call.go @@ -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 +} diff --git a/call_test.go b/call_test.go new file mode 100644 index 0000000..bb2fd0d --- /dev/null +++ b/call_test.go @@ -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) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..db238eb --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/cardboardrobots/go-call + +go 1.18 + +require github.com/google/go-querystring v1.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f99081b --- /dev/null +++ b/go.sum @@ -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= diff --git a/header.go b/header.go new file mode 100644 index 0000000..12301ff --- /dev/null +++ b/header.go @@ -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) +} diff --git a/result.go b/result.go new file mode 100644 index 0000000..680c46e --- /dev/null +++ b/result.go @@ -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, + } +} diff --git a/url_util.go b/url_util.go new file mode 100644 index 0000000..4288f31 --- /dev/null +++ b/url_util.go @@ -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 +} diff --git a/url_util_test.go b/url_util_test.go new file mode 100644 index 0000000..1030c65 --- /dev/null +++ b/url_util_test.go @@ -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) + } +}