mirror of
https://github.com/ngrok/ngrok-api-go.git
synced 2026-05-17 16:50:37 +00:00
Update generated files
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
<!-- Code generated for API Clients. DO NOT EDIT. -->
|
||||
## v5.3.1
|
||||
|
||||
INTERNAL:
|
||||
|
||||
* Moved location of BaseClient
|
||||
|
||||
## v5.3.0
|
||||
|
||||
ENHANCEMENTS:
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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"],
|
||||
)
|
||||
+3
-128
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-16
@@ -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
|
||||
|
||||
+20
@@ -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"
|
||||
}
|
||||
Reference in New Issue
Block a user