This repository has been archived on 2024-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
golang-rest-example/pkg/openapi/oas_client_gen.go

1059 lines
27 KiB
Go
Raw Normal View History

2024-02-01 18:57:25 +00:00
// Code generated by ogen, DO NOT EDIT.
2024-02-01 22:09:09 +00:00
package openapi
2024-02-01 18:57:25 +00:00
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"
"github.com/ogen-go/ogen/uri"
)
// Invoker invokes operations described by OpenAPI v3 specification.
type Invoker interface {
// GroupsGet invokes GET /groups operation.
//
// Get all groups.
//
// GET /groups
GroupsGet(ctx context.Context) ([]Group, error)
// GroupsIDDelete invokes DELETE /groups/{id} operation.
//
// Delete group by id.
//
// DELETE /groups/{id}
GroupsIDDelete(ctx context.Context, params GroupsIDDeleteParams) (GroupsIDDeleteRes, error)
// GroupsIDGet invokes GET /groups/{id} operation.
//
// Get group by id.
//
// GET /groups/{id}
GroupsIDGet(ctx context.Context, params GroupsIDGetParams) (GroupsIDGetRes, error)
// GroupsIDPut invokes PUT /groups/{id} operation.
//
// Update group by id.
//
// PUT /groups/{id}
GroupsIDPut(ctx context.Context, request *GroupUpdate, params GroupsIDPutParams) (GroupsIDPutRes, error)
// GroupsPost invokes POST /groups operation.
//
// Create group.
//
// POST /groups
GroupsPost(ctx context.Context, request *GroupUpdate) (*Group, error)
// HealthzGet invokes GET /healthz operation.
//
// GET /healthz
HealthzGet(ctx context.Context) (*Ok, error)
// UsersGet invokes GET /users operation.
//
// Get all users.
//
// GET /users
UsersGet(ctx context.Context) ([]User, error)
// UsersIDDelete invokes DELETE /users/{id} operation.
//
// Delete user by id.
//
// DELETE /users/{id}
UsersIDDelete(ctx context.Context, params UsersIDDeleteParams) (UsersIDDeleteRes, error)
// UsersIDGet invokes GET /users/{id} operation.
//
// Get user by id.
//
// GET /users/{id}
UsersIDGet(ctx context.Context, params UsersIDGetParams) (UsersIDGetRes, error)
// UsersIDPut invokes PUT /users/{id} operation.
//
// Update user by id.
//
// PUT /users/{id}
UsersIDPut(ctx context.Context, request *UserUpdate, params UsersIDPutParams) (UsersIDPutRes, error)
// UsersPost invokes POST /users operation.
//
// Create user.
//
// POST /users
UsersPost(ctx context.Context, request *UserUpdate) (*User, error)
}
// Client implements OAS client.
type Client struct {
serverURL *url.URL
baseClient
}
type errorHandler interface {
NewError(ctx context.Context, err error) *ErrorStatusCode
}
var _ Handler = struct {
errorHandler
*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
}
// GroupsGet invokes GET /groups operation.
//
// Get all groups.
//
// GET /groups
func (c *Client) GroupsGet(ctx context.Context) ([]Group, error) {
res, err := c.sendGroupsGet(ctx)
return res, err
}
func (c *Client) sendGroupsGet(ctx context.Context) (res []Group, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/groups"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "GroupsGet",
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] = "/groups"
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"
result, err := decodeGroupsGetResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// GroupsIDDelete invokes DELETE /groups/{id} operation.
//
// Delete group by id.
//
// DELETE /groups/{id}
func (c *Client) GroupsIDDelete(ctx context.Context, params GroupsIDDeleteParams) (GroupsIDDeleteRes, error) {
res, err := c.sendGroupsIDDelete(ctx, params)
return res, err
}
func (c *Client) sendGroupsIDDelete(ctx context.Context, params GroupsIDDeleteParams) (res GroupsIDDeleteRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("DELETE"),
semconv.HTTPRouteKey.String("/groups/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "GroupsIDDelete",
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] = "/groups/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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, "DELETE", 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"
result, err := decodeGroupsIDDeleteResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// GroupsIDGet invokes GET /groups/{id} operation.
//
// Get group by id.
//
// GET /groups/{id}
func (c *Client) GroupsIDGet(ctx context.Context, params GroupsIDGetParams) (GroupsIDGetRes, error) {
res, err := c.sendGroupsIDGet(ctx, params)
return res, err
}
func (c *Client) sendGroupsIDGet(ctx context.Context, params GroupsIDGetParams) (res GroupsIDGetRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/groups/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "GroupsIDGet",
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] = "/groups/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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"
result, err := decodeGroupsIDGetResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// GroupsIDPut invokes PUT /groups/{id} operation.
//
// Update group by id.
//
// PUT /groups/{id}
func (c *Client) GroupsIDPut(ctx context.Context, request *GroupUpdate, params GroupsIDPutParams) (GroupsIDPutRes, error) {
res, err := c.sendGroupsIDPut(ctx, request, params)
return res, err
}
func (c *Client) sendGroupsIDPut(ctx context.Context, request *GroupUpdate, params GroupsIDPutParams) (res GroupsIDPutRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("PUT"),
semconv.HTTPRouteKey.String("/groups/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "GroupsIDPut",
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] = "/groups/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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, "PUT", u)
if err != nil {
return res, errors.Wrap(err, "create request")
}
if err := encodeGroupsIDPutRequest(request, r); err != nil {
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"
result, err := decodeGroupsIDPutResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// GroupsPost invokes POST /groups operation.
//
// Create group.
//
// POST /groups
func (c *Client) GroupsPost(ctx context.Context, request *GroupUpdate) (*Group, error) {
res, err := c.sendGroupsPost(ctx, request)
return res, err
}
func (c *Client) sendGroupsPost(ctx context.Context, request *GroupUpdate) (res *Group, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/groups"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "GroupsPost",
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] = "/groups"
uri.AddPathParts(u, pathParts[:]...)
stage = "EncodeRequest"
r, err := ht.NewRequest(ctx, "POST", u)
if err != nil {
return res, errors.Wrap(err, "create request")
}
if err := encodeGroupsPostRequest(request, r); err != nil {
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"
result, err := decodeGroupsPostResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// HealthzGet invokes GET /healthz operation.
//
// GET /healthz
func (c *Client) HealthzGet(ctx context.Context) (*Ok, error) {
res, err := c.sendHealthzGet(ctx)
return res, err
}
func (c *Client) sendHealthzGet(ctx context.Context) (res *Ok, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/healthz"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "HealthzGet",
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] = "/healthz"
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"
result, err := decodeHealthzGetResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// UsersGet invokes GET /users operation.
//
// Get all users.
//
// GET /users
func (c *Client) UsersGet(ctx context.Context) ([]User, error) {
res, err := c.sendUsersGet(ctx)
return res, err
}
func (c *Client) sendUsersGet(ctx context.Context) (res []User, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/users"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "UsersGet",
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] = "/users"
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"
result, err := decodeUsersGetResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// UsersIDDelete invokes DELETE /users/{id} operation.
//
// Delete user by id.
//
// DELETE /users/{id}
func (c *Client) UsersIDDelete(ctx context.Context, params UsersIDDeleteParams) (UsersIDDeleteRes, error) {
res, err := c.sendUsersIDDelete(ctx, params)
return res, err
}
func (c *Client) sendUsersIDDelete(ctx context.Context, params UsersIDDeleteParams) (res UsersIDDeleteRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("DELETE"),
semconv.HTTPRouteKey.String("/users/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "UsersIDDelete",
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] = "/users/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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, "DELETE", 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"
result, err := decodeUsersIDDeleteResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// UsersIDGet invokes GET /users/{id} operation.
//
// Get user by id.
//
// GET /users/{id}
func (c *Client) UsersIDGet(ctx context.Context, params UsersIDGetParams) (UsersIDGetRes, error) {
res, err := c.sendUsersIDGet(ctx, params)
return res, err
}
func (c *Client) sendUsersIDGet(ctx context.Context, params UsersIDGetParams) (res UsersIDGetRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("GET"),
semconv.HTTPRouteKey.String("/users/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "UsersIDGet",
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] = "/users/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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"
result, err := decodeUsersIDGetResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// UsersIDPut invokes PUT /users/{id} operation.
//
// Update user by id.
//
// PUT /users/{id}
func (c *Client) UsersIDPut(ctx context.Context, request *UserUpdate, params UsersIDPutParams) (UsersIDPutRes, error) {
res, err := c.sendUsersIDPut(ctx, request, params)
return res, err
}
func (c *Client) sendUsersIDPut(ctx context.Context, request *UserUpdate, params UsersIDPutParams) (res UsersIDPutRes, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("PUT"),
semconv.HTTPRouteKey.String("/users/{id}"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "UsersIDPut",
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] = "/users/"
{
// Encode "id" parameter.
e := uri.NewPathEncoder(uri.PathEncoderConfig{
Param: "id",
Style: uri.PathStyleSimple,
Explode: false,
})
if err := func() error {
if unwrapped := int64(params.ID); true {
return e.EncodeValue(conv.Int64ToString(unwrapped))
}
return nil
}(); 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, "PUT", u)
if err != nil {
return res, errors.Wrap(err, "create request")
}
if err := encodeUsersIDPutRequest(request, r); err != nil {
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"
result, err := decodeUsersIDPutResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}
// UsersPost invokes POST /users operation.
//
// Create user.
//
// POST /users
func (c *Client) UsersPost(ctx context.Context, request *UserUpdate) (*User, error) {
res, err := c.sendUsersPost(ctx, request)
return res, err
}
func (c *Client) sendUsersPost(ctx context.Context, request *UserUpdate) (res *User, err error) {
otelAttrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String("POST"),
semconv.HTTPRouteKey.String("/users"),
}
// 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.
ctx, span := c.cfg.Tracer.Start(ctx, "UsersPost",
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] = "/users"
uri.AddPathParts(u, pathParts[:]...)
stage = "EncodeRequest"
r, err := ht.NewRequest(ctx, "POST", u)
if err != nil {
return res, errors.Wrap(err, "create request")
}
if err := encodeUsersPostRequest(request, r); err != nil {
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"
result, err := decodeUsersPostResponse(resp)
if err != nil {
return res, errors.Wrap(err, "decode response")
}
return result, nil
}