From 65b6e5578007d5e2101c95733ea1e585dfb3fb62 Mon Sep 17 00:00:00 2001 From: ngrok release bot Date: Wed, 24 Apr 2024 20:05:23 +0000 Subject: [PATCH] Update generated files --- CHANGELOG.md | 6 ++ client.go | 144 ++++++++++++++++++++++++++++++++++++ client_test.go | 26 +++++++ internal/api/BUILD.bazel | 11 +++ internal/api/client.go | 131 +------------------------------- internal/api/client_test.go | 24 +----- internal/api/version.go | 17 +---- version.go | 20 +++++ 8 files changed, 212 insertions(+), 167 deletions(-) create mode 100644 client.go create mode 100644 client_test.go create mode 100644 internal/api/BUILD.bazel create mode 100644 version.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ce3c3..bd18d1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ +## v5.3.1 + +INTERNAL: + +* Moved location of BaseClient + ## v5.3.0 ENHANCEMENTS: diff --git a/client.go b/client.go new file mode 100644 index 0000000..b3076fc --- /dev/null +++ b/client.go @@ -0,0 +1,144 @@ +// Code generated for API Clients. DO NOT EDIT. + +package ngrok + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "runtime" +) + +const ( + apiVersion = "2" +) + +var ( + defaultUserAgent = "ngrok-api-go/" + _version + "/" + runtime.Version() +) + +// BaseClient is a generic client for the ngrok API capable of sending +// arbitrary requests. +type BaseClient struct { + cfg *ClientConfig +} + +// NewBaseClient constructs a new [BaseClient]. +func NewBaseClient(cfg *ClientConfig) *BaseClient { + return &BaseClient{cfg: cfg} +} + +// Do sends a request to the ngrok API. +// +// The `reqBody` and `respBody` parameters will be automatically serialized +// and deserialized. +// +// The `reqURL` may include only the `Path` component. The full URL will be +// built from the `BaseURL` in the [ClientConfig]. +func (c *BaseClient) Do(ctx context.Context, method string, reqURL *url.URL, reqBody interface{}, respBody interface{}) error { + req, err := c.buildRequest(ctx, method, reqURL, reqBody) + if err != nil { + return err + } + resp, err := c.cfg.HTTPClient.Do(req) + if err != nil { + return err + } + if err = c.readResponse(resp, respBody); err != nil { + return err + } + return nil +} + +func (c *BaseClient) buildRequest(ctx context.Context, method string, reqURL *url.URL, reqBody interface{}) (*http.Request, error) { + body, err := c.buildRequestBody(reqBody) + if err != nil { + return nil, err + } + reqURLString := c.cfg.BaseURL.ResolveReference(reqURL).String() + r, err := http.NewRequestWithContext(ctx, method, reqURLString, body) + if err != nil { + return nil, err + } + + r.Header.Set("authorization", fmt.Sprintf("Bearer %s", c.cfg.APIKey)) + r.Header.Set("user-agent", c.userAgent()) + r.Header.Set("ngrok-version", apiVersion) + if body != nil { + r.Header.Set("content-type", "application/json") + } + return r, nil +} + +func (c *BaseClient) buildRequestBody(reqBody interface{}) (io.Reader, error) { + if reqBody == nil { + return nil, nil + } + jsonBytes, err := json.Marshal(reqBody) + if err != nil { + return nil, err + } + return bytes.NewReader(jsonBytes), nil +} + +func (c *BaseClient) readResponse(resp *http.Response, out interface{}) error { + if resp.Body != nil { + defer resp.Body.Close() + } + if resp.StatusCode >= http.StatusBadRequest { + return c.readErrorResponse(resp) + } + return c.readResponseBody(resp, out) +} + +// read an error response body +func (c *BaseClient) readErrorResponse(resp *http.Response) error { + var out Error + err := c.readResponseBody(resp, &out) + if err != nil { + return err + } + return &out +} + +// unmarshal a response body +func (c *BaseClient) readResponseBody(resp *http.Response, out interface{}) error { + if out == nil { + return nil + } + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return c.buildUnmarshalError(resp, bodyBytes, err) + } + if err := json.Unmarshal(bodyBytes, out); err != nil { + return c.buildUnmarshalError(resp, bodyBytes, err) + } + return nil +} + +// if an error occurs while trying to read a response body, construct a new +// error explaining the unmarshalling failure +func (c *BaseClient) buildUnmarshalError(resp *http.Response, bodyBytes []byte, err error) error { + return &Error{ + Msg: fmt.Sprintf("failed to unmarshal response body: %s. body: %s", err, bodyBytes), + StatusCode: int32(resp.StatusCode), + Details: map[string]string{ + "unmarshal_error": err.Error(), + "invalid_body": string(bodyBytes), + "operation_id": resp.Header.Get("ngrok-operation-id"), + }, + } +} + +// Returns a user agent override if one was set on the client config. Otherwise, +// returns the default user agent. +func (c *BaseClient) userAgent() string { + if c.cfg.UserAgent != nil { + return *c.cfg.UserAgent + } + return defaultUserAgent +} diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..574eff4 --- /dev/null +++ b/client_test.go @@ -0,0 +1,26 @@ +// Code generated for API Clients. DO NOT EDIT. + +package ngrok + +import ( + "context" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestUserAgent(t *testing.T) { + url := &url.URL{Path: "/test"} + apiKey := "testKey" + + testUserAgent := func(cfg *ClientConfig, expected string) { + client := NewBaseClient(cfg) + req, err := client.buildRequest(context.TODO(), "GET", url, nil) + assert.NoError(t, err) + assert.Equal(t, expected, req.Header.Get("user-agent")) + } + + testUserAgent(NewClientConfig(apiKey), defaultUserAgent) + testUserAgent(NewClientConfig(apiKey, WithUserAgent("testAgent")), "testAgent") +} diff --git a/internal/api/BUILD.bazel b/internal/api/BUILD.bazel new file mode 100644 index 0000000..368a2de --- /dev/null +++ b/internal/api/BUILD.bazel @@ -0,0 +1,11 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "api", + srcs = [ + "client.go", + ], + importpath = "go.ngrok.com/cmd/apic/gen/golang_client/_static/internal/api", + visibility = ["//go/cmd/apic/gen/golang_client/_static:__subpackages__"], + deps = ["@com_github_ngrok_ngrok_api_go_v4//:ngrok-api-go"], +) diff --git a/internal/api/client.go b/internal/api/client.go index 4e51c9c..83bd855 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -2,135 +2,10 @@ package api -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "runtime" +import "github.com/ngrok/ngrok-api-go/v5" - "github.com/ngrok/ngrok-api-go/v5" -) - -const ( - apiVersion = "2" -) - -var ( - defaultUserAgent = "ngrok-api-go/" + Version + "/" + runtime.Version() -) - -type Client struct { - cfg *ngrok.ClientConfig -} +type Client = ngrok.BaseClient func NewClient(cfg *ngrok.ClientConfig) *Client { - return &Client{cfg: cfg} -} - -func (c *Client) Do(ctx context.Context, method string, reqURL *url.URL, reqBody interface{}, respBody interface{}) error { - req, err := c.buildRequest(ctx, method, reqURL, reqBody) - if err != nil { - return err - } - resp, err := c.cfg.HTTPClient.Do(req) - if err != nil { - return err - } - if err = c.readResponse(resp, respBody); err != nil { - return err - } - return nil -} - -func (c *Client) buildRequest(ctx context.Context, method string, reqURL *url.URL, reqBody interface{}) (*http.Request, error) { - body, err := c.buildRequestBody(reqBody) - if err != nil { - return nil, err - } - reqURLString := c.cfg.BaseURL.ResolveReference(reqURL).String() - r, err := http.NewRequestWithContext(ctx, method, reqURLString, body) - if err != nil { - return nil, err - } - - r.Header.Set("authorization", fmt.Sprintf("Bearer %s", c.cfg.APIKey)) - r.Header.Set("user-agent", c.userAgent()) - r.Header.Set("ngrok-version", apiVersion) - if body != nil { - r.Header.Set("content-type", "application/json") - } - return r, nil -} - -func (c *Client) buildRequestBody(reqBody interface{}) (io.Reader, error) { - if reqBody == nil { - return nil, nil - } - jsonBytes, err := json.Marshal(reqBody) - if err != nil { - return nil, err - } - return bytes.NewReader(jsonBytes), nil -} - -func (c *Client) readResponse(resp *http.Response, out interface{}) error { - if resp.Body != nil { - defer resp.Body.Close() - } - if resp.StatusCode >= http.StatusBadRequest { - return c.readErrorResponse(resp) - } - return c.readResponseBody(resp, out) -} - -// read an error response body -func (c *Client) readErrorResponse(resp *http.Response) error { - var out ngrok.Error - err := c.readResponseBody(resp, &out) - if err != nil { - return err - } - return &out -} - -// unmarshal a response body -func (c *Client) readResponseBody(resp *http.Response, out interface{}) error { - if out == nil { - return nil - } - bodyBytes, err := io.ReadAll(resp.Body) - if err != nil { - return c.buildUnmarshalError(resp, bodyBytes, err) - } - if err := json.Unmarshal(bodyBytes, out); err != nil { - return c.buildUnmarshalError(resp, bodyBytes, err) - } - return nil -} - -// if an error occurs while trying to read a response body, construct a new -// error explaining the unmarshalling failure -func (c *Client) buildUnmarshalError(resp *http.Response, bodyBytes []byte, err error) error { - return &ngrok.Error{ - Msg: fmt.Sprintf("failed to unmarshal response body: %s. body: %s", err, bodyBytes), - StatusCode: int32(resp.StatusCode), - Details: map[string]string{ - "unmarshal_error": err.Error(), - "invalid_body": string(bodyBytes), - "operation_id": resp.Header.Get("ngrok-operation-id"), - }, - } -} - -// Returns a user agent override if one was set on the client config. Otherwise, -// returns the default user agent. -func (c *Client) userAgent() string { - if c.cfg.UserAgent != nil { - return *c.cfg.UserAgent - } - return defaultUserAgent + return ngrok.NewBaseClient(cfg) } diff --git a/internal/api/client_test.go b/internal/api/client_test.go index 423f84d..ce5ee78 100644 --- a/internal/api/client_test.go +++ b/internal/api/client_test.go @@ -2,26 +2,4 @@ package api -import ( - "context" - "net/url" - "testing" - - "github.com/ngrok/ngrok-api-go/v5" - "github.com/stretchr/testify/assert" -) - -func TestUserAgent(t *testing.T) { - url := &url.URL{Path: "/test"} - apiKey := "testKey" - - testUserAgent := func(cfg *ngrok.ClientConfig, expected string) { - client := NewClient(cfg) - req, err := client.buildRequest(context.TODO(), "GET", url, nil) - assert.NoError(t, err) - assert.Equal(t, expected, req.Header.Get("user-agent")) - } - - testUserAgent(ngrok.NewClientConfig(apiKey), defaultUserAgent) - testUserAgent(ngrok.NewClientConfig(apiKey, ngrok.WithUserAgent("testAgent")), "testAgent") -} +// Dummy file to satisfy our code generation pipeline diff --git a/internal/api/version.go b/internal/api/version.go index af166e8..ce5ee78 100644 --- a/internal/api/version.go +++ b/internal/api/version.go @@ -2,19 +2,4 @@ package api -import "runtime/debug" - -const modulePath = "github.com/ngrok/ngrok-api-go" - -var Version string - -func init() { - if buildInfo, ok := debug.ReadBuildInfo(); ok { - for _, dep := range buildInfo.Deps { - if dep.Path == modulePath { - Version = dep.Version - } - } - } - Version = "unknown" -} +// Dummy file to satisfy our code generation pipeline diff --git a/version.go b/version.go new file mode 100644 index 0000000..a917476 --- /dev/null +++ b/version.go @@ -0,0 +1,20 @@ +// Code generated for API Clients. DO NOT EDIT. + +package ngrok + +import "runtime/debug" + +const modulePath = "github.com/ngrok/ngrok-api-go" + +var _version string + +func init() { + if buildInfo, ok := debug.ReadBuildInfo(); ok { + for _, dep := range buildInfo.Deps { + if dep.Path == modulePath { + _version = dep.Version + } + } + } + _version = "unknown" +}