openapi: generate
This commit is contained in:
@@ -1077,22 +1077,22 @@ func (s *Server) handleGetScriptRequest(args [1]string, argsEscaped bool, w http
|
||||
}
|
||||
}
|
||||
|
||||
// handleGetScriptPolicyFromHashRequest handles getScriptPolicyFromHash operation.
|
||||
// handleListScriptPolicyRequest handles listScriptPolicy operation.
|
||||
//
|
||||
// Get the policy for the given hash of script source code.
|
||||
// Get list of script policies.
|
||||
//
|
||||
// GET /script-policy/hash/{FromScriptHash}
|
||||
func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
// GET /script-policy
|
||||
func (s *Server) handleListScriptPolicyRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
otelAttrs := []attribute.KeyValue{
|
||||
otelogen.OperationID("getScriptPolicyFromHash"),
|
||||
otelogen.OperationID("listScriptPolicy"),
|
||||
semconv.HTTPRequestMethodKey.String("GET"),
|
||||
semconv.HTTPRouteKey.String("/script-policy/hash/{FromScriptHash}"),
|
||||
semconv.HTTPRouteKey.String("/script-policy"),
|
||||
}
|
||||
|
||||
// Start a span for this request.
|
||||
ctx, span := s.cfg.Tracer.Start(r.Context(), GetScriptPolicyFromHashOperation,
|
||||
ctx, span := s.cfg.Tracer.Start(r.Context(), ListScriptPolicyOperation,
|
||||
trace.WithAttributes(otelAttrs...),
|
||||
serverSpanKind,
|
||||
)
|
||||
@@ -1147,11 +1147,11 @@ func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscape
|
||||
}
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: GetScriptPolicyFromHashOperation,
|
||||
ID: "getScriptPolicyFromHash",
|
||||
Name: ListScriptPolicyOperation,
|
||||
ID: "listScriptPolicy",
|
||||
}
|
||||
)
|
||||
params, err := decodeGetScriptPolicyFromHashParams(args, argsEscaped, r)
|
||||
params, err := decodeListScriptPolicyParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
@@ -1162,27 +1162,43 @@ func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscape
|
||||
return
|
||||
}
|
||||
|
||||
var response *ScriptPolicy
|
||||
var response []ScriptPolicy
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: GetScriptPolicyFromHashOperation,
|
||||
OperationSummary: "Get the policy for the given hash of script source code",
|
||||
OperationID: "getScriptPolicyFromHash",
|
||||
OperationName: ListScriptPolicyOperation,
|
||||
OperationSummary: "Get list of script policies",
|
||||
OperationID: "listScriptPolicy",
|
||||
Body: nil,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "Page",
|
||||
In: "query",
|
||||
}: params.Page,
|
||||
{
|
||||
Name: "Limit",
|
||||
In: "query",
|
||||
}: params.Limit,
|
||||
{
|
||||
Name: "FromScriptHash",
|
||||
In: "path",
|
||||
In: "query",
|
||||
}: params.FromScriptHash,
|
||||
{
|
||||
Name: "ToScriptID",
|
||||
In: "query",
|
||||
}: params.ToScriptID,
|
||||
{
|
||||
Name: "Policy",
|
||||
In: "query",
|
||||
}: params.Policy,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = struct{}
|
||||
Params = GetScriptPolicyFromHashParams
|
||||
Response = *ScriptPolicy
|
||||
Params = ListScriptPolicyParams
|
||||
Response = []ScriptPolicy
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
@@ -1191,14 +1207,14 @@ func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscape
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackGetScriptPolicyFromHashParams,
|
||||
unpackListScriptPolicyParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.GetScriptPolicyFromHash(ctx, params)
|
||||
response, err = s.h.ListScriptPolicy(ctx, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.GetScriptPolicyFromHash(ctx, params)
|
||||
response, err = s.h.ListScriptPolicy(ctx, params)
|
||||
}
|
||||
if err != nil {
|
||||
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
|
||||
@@ -1217,7 +1233,176 @@ func (s *Server) handleGetScriptPolicyFromHashRequest(args [1]string, argsEscape
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeGetScriptPolicyFromHashResponse(response, w, span); err != nil {
|
||||
if err := encodeListScriptPolicyResponse(response, w, span); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleListScriptsRequest handles listScripts operation.
|
||||
//
|
||||
// Get list of scripts.
|
||||
//
|
||||
// GET /scripts
|
||||
func (s *Server) handleListScriptsRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
otelAttrs := []attribute.KeyValue{
|
||||
otelogen.OperationID("listScripts"),
|
||||
semconv.HTTPRequestMethodKey.String("GET"),
|
||||
semconv.HTTPRouteKey.String("/scripts"),
|
||||
}
|
||||
|
||||
// Start a span for this request.
|
||||
ctx, span := s.cfg.Tracer.Start(r.Context(), ListScriptsOperation,
|
||||
trace.WithAttributes(otelAttrs...),
|
||||
serverSpanKind,
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
// Add Labeler to context.
|
||||
labeler := &Labeler{attrs: otelAttrs}
|
||||
ctx = contextWithLabeler(ctx, labeler)
|
||||
|
||||
// Run stopwatch.
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
elapsedDuration := time.Since(startTime)
|
||||
|
||||
attrSet := labeler.AttributeSet()
|
||||
attrs := attrSet.ToSlice()
|
||||
code := statusWriter.status
|
||||
if code != 0 {
|
||||
codeAttr := semconv.HTTPResponseStatusCode(code)
|
||||
attrs = append(attrs, codeAttr)
|
||||
span.SetAttributes(codeAttr)
|
||||
}
|
||||
attrOpt := metric.WithAttributes(attrs...)
|
||||
|
||||
// Increment request counter.
|
||||
s.requests.Add(ctx, 1, attrOpt)
|
||||
|
||||
// Use floating point division here for higher precision (instead of Millisecond method).
|
||||
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
|
||||
}()
|
||||
|
||||
var (
|
||||
recordError = func(stage string, err error) {
|
||||
span.RecordError(err)
|
||||
|
||||
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
|
||||
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
|
||||
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
|
||||
// max redirects exceeded), in which case status MUST be set to Error.
|
||||
code := statusWriter.status
|
||||
if code >= 100 && code < 500 {
|
||||
span.SetStatus(codes.Error, stage)
|
||||
}
|
||||
|
||||
attrSet := labeler.AttributeSet()
|
||||
attrs := attrSet.ToSlice()
|
||||
if code != 0 {
|
||||
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
|
||||
}
|
||||
|
||||
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
|
||||
}
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: ListScriptsOperation,
|
||||
ID: "listScripts",
|
||||
}
|
||||
)
|
||||
params, err := decodeListScriptsParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeParams", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var response []Script
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: ListScriptsOperation,
|
||||
OperationSummary: "Get list of scripts",
|
||||
OperationID: "listScripts",
|
||||
Body: nil,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "Page",
|
||||
In: "query",
|
||||
}: params.Page,
|
||||
{
|
||||
Name: "Limit",
|
||||
In: "query",
|
||||
}: params.Limit,
|
||||
{
|
||||
Name: "Hash",
|
||||
In: "query",
|
||||
}: params.Hash,
|
||||
{
|
||||
Name: "Name",
|
||||
In: "query",
|
||||
}: params.Name,
|
||||
{
|
||||
Name: "Source",
|
||||
In: "query",
|
||||
}: params.Source,
|
||||
{
|
||||
Name: "SubmissionID",
|
||||
In: "query",
|
||||
}: params.SubmissionID,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = struct{}
|
||||
Params = ListScriptsParams
|
||||
Response = []Script
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
Params,
|
||||
Response,
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackListScriptsParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.ListScripts(ctx, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.ListScripts(ctx, params)
|
||||
}
|
||||
if err != nil {
|
||||
if errRes, ok := errors.Into[*ErrorStatusCode](err); ok {
|
||||
if err := encodeErrorResponse(errRes, w, span); err != nil {
|
||||
defer recordError("Internal", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ht.ErrNotImplemented) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
|
||||
defer recordError("Internal", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeListScriptsResponse(response, w, span); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
|
||||
Reference in New Issue
Block a user