github.com/kaisawind/go-swagger@v0.19.0/examples/generated/restapi/operations/pet/get_pet_by_id.go (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  package pet
     4  
     5  // This file was generated by the swagger tool.
     6  // Editing this file might prove futile when you re-run the generate command
     7  
     8  import (
     9  	"net/http"
    10  
    11  	middleware "github.com/go-openapi/runtime/middleware"
    12  )
    13  
    14  // GetPetByIDHandlerFunc turns a function with the right signature into a get pet by Id handler
    15  type GetPetByIDHandlerFunc func(GetPetByIDParams, interface{}) middleware.Responder
    16  
    17  // Handle executing the request and returning a response
    18  func (fn GetPetByIDHandlerFunc) Handle(params GetPetByIDParams, principal interface{}) middleware.Responder {
    19  	return fn(params, principal)
    20  }
    21  
    22  // GetPetByIDHandler interface for that can handle valid get pet by Id params
    23  type GetPetByIDHandler interface {
    24  	Handle(GetPetByIDParams, interface{}) middleware.Responder
    25  }
    26  
    27  // NewGetPetByID creates a new http.Handler for the get pet by Id operation
    28  func NewGetPetByID(ctx *middleware.Context, handler GetPetByIDHandler) *GetPetByID {
    29  	return &GetPetByID{Context: ctx, Handler: handler}
    30  }
    31  
    32  /*GetPetByID swagger:route GET /pets/{petId} pet getPetById
    33  
    34  Find pet by ID
    35  
    36  Returns a pet when ID < 10.  ID > 10 or nonintegers will simulate API error conditions
    37  
    38  */
    39  type GetPetByID struct {
    40  	Context *middleware.Context
    41  	Handler GetPetByIDHandler
    42  }
    43  
    44  func (o *GetPetByID) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
    45  	route, rCtx, _ := o.Context.RouteInfo(r)
    46  	if rCtx != nil {
    47  		r = rCtx
    48  	}
    49  	var Params = NewGetPetByIDParams()
    50  
    51  	uprinc, aCtx, err := o.Context.Authorize(r, route)
    52  	if err != nil {
    53  		o.Context.Respond(rw, r, route.Produces, route, err)
    54  		return
    55  	}
    56  	if aCtx != nil {
    57  		r = aCtx
    58  	}
    59  	var principal interface{}
    60  	if uprinc != nil {
    61  		principal = uprinc
    62  	}
    63  
    64  	if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
    65  		o.Context.Respond(rw, r, route.Produces, route, err)
    66  		return
    67  	}
    68  
    69  	res := o.Handler.Handle(Params, principal) // actually handle the request
    70  
    71  	o.Context.Respond(rw, r, route.Produces, route, res)
    72  
    73  }