2024-01-29 18:32:16 +00:00
|
|
|
// Code generated by ogen, DO NOT EDIT.
|
|
|
|
|
|
|
|
package oas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-faster/errors"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
|
|
|
"github.com/ogen-go/ogen/conv"
|
|
|
|
ht "github.com/ogen-go/ogen/http"
|
2024-01-29 18:51:23 +00:00
|
|
|
"github.com/ogen-go/ogen/otelogen"
|
2024-01-29 18:32:16 +00:00
|
|
|
"github.com/ogen-go/ogen/uri"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Invoker invokes operations described by OpenAPI v3 specification.
|
|
|
|
type Invoker interface {
|
2024-01-29 18:51:23 +00:00
|
|
|
// CreateShortURL invokes createShortURL operation.
|
|
|
|
//
|
|
|
|
// Create a shorten URL.
|
2024-01-29 18:32:16 +00:00
|
|
|
//
|
|
|
|
// POST /create
|
2024-01-29 18:51:23 +00:00
|
|
|
CreateShortURL(ctx context.Context, request *CreateShortURLReq) (CreateShortURLRes, error)
|
|
|
|
// RedirectLongURL invokes redirectLongURL operation.
|
2024-01-29 18:32:16 +00:00
|
|
|
//
|
|
|
|
// Redirect client to long URL.
|
|
|
|
//
|
|
|
|
// GET /{hash}
|
2024-01-29 18:51:23 +00:00
|
|
|
RedirectLongURL(ctx context.Context, params RedirectLongURLParams) (RedirectLongURLRes, error)
|
2024-01-29 18:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client implements OAS client.
|
|
|
|
type Client struct {
|
|
|
|
serverURL *url.URL
|
|
|
|
baseClient
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Handler = struct {
|
|
|
|
*Client
|
|
|
|
}{}
|
|
|
|
|
|
|
|
func trimTrailingSlashes(u *url.URL) {
|
|
|
|
u.Path = strings.TrimRight(u.Path, "/")
|
|
|
|
u.RawPath = strings.TrimRight(u.RawPath, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient initializes new Client defined by OAS.
|
|
|
|
func NewClient(serverURL string, opts ...ClientOption) (*Client, error) {
|
|
|
|
u, err := url.Parse(serverURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trimTrailingSlashes(u)
|
|
|
|
|
|
|
|
c, err := newClientConfig(opts...).baseClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{
|
|
|
|
serverURL: u,
|
|
|
|
baseClient: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverURLKey struct{}
|
|
|
|
|
|
|
|
// WithServerURL sets context key to override server URL.
|
|
|
|
func WithServerURL(ctx context.Context, u *url.URL) context.Context {
|
|
|
|
return context.WithValue(ctx, serverURLKey{}, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) requestURL(ctx context.Context) *url.URL {
|
|
|
|
u, ok := ctx.Value(serverURLKey{}).(*url.URL)
|
|
|
|
if !ok {
|
|
|
|
return c.serverURL
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:51:23 +00:00
|
|
|
// CreateShortURL invokes createShortURL operation.
|
|
|
|
//
|
|
|
|
// Create a shorten URL.
|
2024-01-29 18:32:16 +00:00
|
|
|
//
|
|
|
|
// POST /create
|
2024-01-29 18:51:23 +00:00
|
|
|
func (c *Client) CreateShortURL(ctx context.Context, request *CreateShortURLReq) (CreateShortURLRes, error) {
|
|
|
|
res, err := c.sendCreateShortURL(ctx, request)
|
2024-01-29 18:32:16 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:51:23 +00:00
|
|
|
func (c *Client) sendCreateShortURL(ctx context.Context, request *CreateShortURLReq) (res CreateShortURLRes, err error) {
|
2024-01-29 18:32:16 +00:00
|
|
|
otelAttrs := []attribute.KeyValue{
|
2024-01-29 18:51:23 +00:00
|
|
|
otelogen.OperationID("createShortURL"),
|
2024-01-29 18:32:16 +00:00
|
|
|
semconv.HTTPMethodKey.String("POST"),
|
|
|
|
semconv.HTTPRouteKey.String("/create"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run stopwatch.
|
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
|
|
elapsedDuration := time.Since(startTime)
|
|
|
|
c.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Increment request counter.
|
|
|
|
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
|
|
|
|
// Start a span for this request.
|
2024-01-29 18:51:23 +00:00
|
|
|
ctx, span := c.cfg.Tracer.Start(ctx, "CreateShortURL",
|
2024-01-29 18:32:16 +00:00
|
|
|
trace.WithAttributes(otelAttrs...),
|
|
|
|
clientSpanKind,
|
|
|
|
)
|
|
|
|
// Track stage for error reporting.
|
|
|
|
var stage string
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, stage)
|
|
|
|
c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
}()
|
|
|
|
|
|
|
|
stage = "BuildURL"
|
|
|
|
u := uri.Clone(c.requestURL(ctx))
|
|
|
|
var pathParts [1]string
|
|
|
|
pathParts[0] = "/create"
|
|
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
|
|
|
|
stage = "EncodeRequest"
|
|
|
|
r, err := ht.NewRequest(ctx, "POST", u)
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "create request")
|
|
|
|
}
|
2024-01-29 18:51:23 +00:00
|
|
|
if err := encodeCreateShortURLRequest(request, r); err != nil {
|
2024-01-29 18:32:16 +00:00
|
|
|
return res, errors.Wrap(err, "encode request")
|
|
|
|
}
|
|
|
|
|
|
|
|
stage = "SendRequest"
|
|
|
|
resp, err := c.cfg.Client.Do(r)
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "do request")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
stage = "DecodeResponse"
|
2024-01-29 18:51:23 +00:00
|
|
|
result, err := decodeCreateShortURLResponse(resp)
|
2024-01-29 18:32:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "decode response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:51:23 +00:00
|
|
|
// RedirectLongURL invokes redirectLongURL operation.
|
2024-01-29 18:32:16 +00:00
|
|
|
//
|
|
|
|
// Redirect client to long URL.
|
|
|
|
//
|
|
|
|
// GET /{hash}
|
2024-01-29 18:51:23 +00:00
|
|
|
func (c *Client) RedirectLongURL(ctx context.Context, params RedirectLongURLParams) (RedirectLongURLRes, error) {
|
|
|
|
res, err := c.sendRedirectLongURL(ctx, params)
|
2024-01-29 18:32:16 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:51:23 +00:00
|
|
|
func (c *Client) sendRedirectLongURL(ctx context.Context, params RedirectLongURLParams) (res RedirectLongURLRes, err error) {
|
2024-01-29 18:32:16 +00:00
|
|
|
otelAttrs := []attribute.KeyValue{
|
2024-01-29 18:51:23 +00:00
|
|
|
otelogen.OperationID("redirectLongURL"),
|
2024-01-29 18:32:16 +00:00
|
|
|
semconv.HTTPMethodKey.String("GET"),
|
|
|
|
semconv.HTTPRouteKey.String("/{hash}"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run stopwatch.
|
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
|
|
elapsedDuration := time.Since(startTime)
|
|
|
|
c.duration.Record(ctx, float64(float64(elapsedDuration)/float64(time.Millisecond)), metric.WithAttributes(otelAttrs...))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Increment request counter.
|
|
|
|
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
|
|
|
|
// Start a span for this request.
|
2024-01-29 18:51:23 +00:00
|
|
|
ctx, span := c.cfg.Tracer.Start(ctx, "RedirectLongURL",
|
2024-01-29 18:32:16 +00:00
|
|
|
trace.WithAttributes(otelAttrs...),
|
|
|
|
clientSpanKind,
|
|
|
|
)
|
|
|
|
// Track stage for error reporting.
|
|
|
|
var stage string
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, stage)
|
|
|
|
c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
}()
|
|
|
|
|
|
|
|
stage = "BuildURL"
|
|
|
|
u := uri.Clone(c.requestURL(ctx))
|
|
|
|
var pathParts [2]string
|
|
|
|
pathParts[0] = "/"
|
|
|
|
{
|
|
|
|
// Encode "hash" parameter.
|
|
|
|
e := uri.NewPathEncoder(uri.PathEncoderConfig{
|
|
|
|
Param: "hash",
|
|
|
|
Style: uri.PathStyleSimple,
|
|
|
|
Explode: false,
|
|
|
|
})
|
|
|
|
if err := func() error {
|
|
|
|
return e.EncodeValue(conv.StringToString(params.Hash))
|
|
|
|
}(); err != nil {
|
|
|
|
return res, errors.Wrap(err, "encode path")
|
|
|
|
}
|
|
|
|
encoded, err := e.Result()
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "encode path")
|
|
|
|
}
|
|
|
|
pathParts[1] = encoded
|
|
|
|
}
|
|
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
|
|
|
|
stage = "EncodeRequest"
|
|
|
|
r, err := ht.NewRequest(ctx, "GET", u)
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "create request")
|
|
|
|
}
|
|
|
|
|
|
|
|
stage = "SendRequest"
|
|
|
|
resp, err := c.cfg.Client.Do(r)
|
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "do request")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
stage = "DecodeResponse"
|
2024-01-29 18:51:23 +00:00
|
|
|
result, err := decodeRedirectLongURLResponse(resp)
|
2024-01-29 18:32:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, errors.Wrap(err, "decode response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|