forked from gsn/predictor
493 lines
12 KiB
Go
493 lines
12 KiB
Go
// Code generated by ogen, DO NOT EDIT.
|
|
|
|
package gsn
|
|
|
|
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.26.0"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"github.com/ogen-go/ogen/conv"
|
|
ht "github.com/ogen-go/ogen/http"
|
|
"github.com/ogen-go/ogen/otelogen"
|
|
"github.com/ogen-go/ogen/uri"
|
|
)
|
|
|
|
func trimTrailingSlashes(u *url.URL) {
|
|
u.Path = strings.TrimRight(u.Path, "/")
|
|
u.RawPath = strings.TrimRight(u.RawPath, "/")
|
|
}
|
|
|
|
// Invoker invokes operations described by OpenAPI v3 specification.
|
|
type Invoker interface {
|
|
// PerformPrediction invokes performPrediction operation.
|
|
//
|
|
// Perform prediction.
|
|
//
|
|
// GET /api/v1/prediction
|
|
PerformPrediction(ctx context.Context, params PerformPredictionParams) (*PredictionResult, error)
|
|
// ReadinessCheck invokes readinessCheck operation.
|
|
//
|
|
// Readiness check.
|
|
//
|
|
// GET /ready
|
|
ReadinessCheck(ctx context.Context) (*ReadinessResponse, 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
|
|
}{}
|
|
|
|
// 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
|
|
}
|
|
|
|
// PerformPrediction invokes performPrediction operation.
|
|
//
|
|
// Perform prediction.
|
|
//
|
|
// GET /api/v1/prediction
|
|
func (c *Client) PerformPrediction(ctx context.Context, params PerformPredictionParams) (*PredictionResult, error) {
|
|
res, err := c.sendPerformPrediction(ctx, params)
|
|
return res, err
|
|
}
|
|
|
|
func (c *Client) sendPerformPrediction(ctx context.Context, params PerformPredictionParams) (res *PredictionResult, err error) {
|
|
otelAttrs := []attribute.KeyValue{
|
|
otelogen.OperationID("performPrediction"),
|
|
semconv.HTTPRequestMethodKey.String("GET"),
|
|
semconv.HTTPRouteKey.String("/api/v1/prediction"),
|
|
}
|
|
|
|
// 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(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, PerformPredictionOperation,
|
|
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] = "/api/v1/prediction"
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
stage = "EncodeQueryParams"
|
|
q := uri.NewQueryEncoder()
|
|
{
|
|
// Encode "launch_latitude" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "launch_latitude",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.LaunchLatitude.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "launch_longitude" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "launch_longitude",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.LaunchLongitude.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "launch_datetime" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "launch_datetime",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.LaunchDatetime.Get(); ok {
|
|
return e.EncodeValue(conv.DateTimeToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "launch_altitude" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "launch_altitude",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.LaunchAltitude.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "profile" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "profile",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.Profile.Get(); ok {
|
|
return e.EncodeValue(conv.StringToString(string(val)))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "ascent_rate" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "ascent_rate",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.AscentRate.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "burst_altitude" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "burst_altitude",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.BurstAltitude.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "descent_rate" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "descent_rate",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.DescentRate.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "float_altitude" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "float_altitude",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.FloatAltitude.Get(); ok {
|
|
return e.EncodeValue(conv.Float64ToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "stop_datetime" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "stop_datetime",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.StopDatetime.Get(); ok {
|
|
return e.EncodeValue(conv.DateTimeToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "ascent_curve" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "ascent_curve",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.AscentCurve.Get(); ok {
|
|
return e.EncodeValue(conv.StringToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "descent_curve" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "descent_curve",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.DescentCurve.Get(); ok {
|
|
return e.EncodeValue(conv.StringToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "interpolate" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "interpolate",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.Interpolate.Get(); ok {
|
|
return e.EncodeValue(conv.BoolToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "format" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "format",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.Format.Get(); ok {
|
|
return e.EncodeValue(conv.StringToString(string(val)))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
{
|
|
// Encode "dataset" parameter.
|
|
cfg := uri.QueryParameterEncodingConfig{
|
|
Name: "dataset",
|
|
Style: uri.QueryStyleForm,
|
|
Explode: true,
|
|
}
|
|
|
|
if err := q.EncodeParam(cfg, func(e uri.Encoder) error {
|
|
if val, ok := params.Dataset.Get(); ok {
|
|
return e.EncodeValue(conv.DateTimeToString(val))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return res, errors.Wrap(err, "encode query")
|
|
}
|
|
}
|
|
u.RawQuery = q.Values().Encode()
|
|
|
|
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 := decodePerformPredictionResponse(resp)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "decode response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// ReadinessCheck invokes readinessCheck operation.
|
|
//
|
|
// Readiness check.
|
|
//
|
|
// GET /ready
|
|
func (c *Client) ReadinessCheck(ctx context.Context) (*ReadinessResponse, error) {
|
|
res, err := c.sendReadinessCheck(ctx)
|
|
return res, err
|
|
}
|
|
|
|
func (c *Client) sendReadinessCheck(ctx context.Context) (res *ReadinessResponse, err error) {
|
|
otelAttrs := []attribute.KeyValue{
|
|
otelogen.OperationID("readinessCheck"),
|
|
semconv.HTTPRequestMethodKey.String("GET"),
|
|
semconv.HTTPRouteKey.String("/ready"),
|
|
}
|
|
|
|
// 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(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, ReadinessCheckOperation,
|
|
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] = "/ready"
|
|
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 := decodeReadinessCheckResponse(resp)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "decode response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|