coreprocessors

package
v0.0.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2021 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuildContentLengthProcessor = processors.DefaultProcessor{
	Name: "core.BuildContentLengthProcessor",
	Handler: func(t interface{}) {
		r := t.(*request.Request)
		var length int64

		if slength := r.HTTPRequest.Header.Get("Content-Length"); slength != "" {
			length, _ = strconv.ParseInt(slength, 10, 64)
		} else {
			if r.HTTPRequest.Body != nil {
				var err error
				if sapio.IsReaderSeekable(r.HTTPRequest.Body) {
					length, err = sapio.SeekerLen(r.HTTPRequest.Body.(io.ReadSeeker))
				} else {
					err = errors.New("request body is not seekable")
				}
				if err != nil {
					r.Error = saperr.New(saperr.Serialization, "failed to get request body's length", err)
					return
				}
			}
		}

		if length > 0 {
			r.HTTPRequest.ContentLength = length
			r.HTTPRequest.Header.Set("Content-Length", fmt.Sprintf("%d", length))
		} else {
			r.HTTPRequest.ContentLength = 0
			r.HTTPRequest.Header.Del("Content-Length")
		}
	},
}

BuildContentLengthProcessor builds the content length of a request based on the body, or will use the HTTPRequest.Header's "Content-Length" if defined. If unable to determine request body length and no "Content-Length" was specified it will panic.

The Content-Length will only be added to the request if the length of the body is greater than 0. If the body is empty or the current `Content-Length` header is <= 0, the header will also be stripped.

View Source
var SendProcessor = processors.DefaultProcessor{
	Name: "core.SendProcessor",
	Handler: func(t interface{}) {
		r := t.(*request.Request)
		sender := sendFollowRedirects
		if r.DisableFollowRedirects {
			sender = sendWithoutFollowRedirects
		}

		if request.NoBody == r.HTTPRequest.Body {

			reqOrig, reqCopy := r.HTTPRequest, *r.HTTPRequest
			reqCopy.Body = nil
			r.HTTPRequest = &reqCopy
			defer func() {
				r.HTTPRequest = reqOrig
			}()
		}

		var err error
		r.HTTPResponse, err = sender(r)
		if err != nil {
			handleSendError(r, err)
		} else {
			r.ResponseBody, _ = ioutil.ReadAll(r.HTTPResponse.Body)
			if r.ResponseBodyHandler != nil {
				if wrappedBody, err := r.ResponseBodyHandler(r.HTTPResponse.StatusCode, r.ResponseBody); err == nil && wrappedBody != nil {
					r.ResponseBody = wrappedBody
				}
			}
		}
	},
}

SendProcessor is a request handler to send service request using HTTP client.

View Source
var ValidateEndpointProcessor = processors.DefaultProcessor{
	Name: "core.ValidateEndpointProcessor",
	Handler: func(t interface{}) {
		r := t.(*request.Request)
		if r.ServiceInfo.Endpoint.Host == "" {
			r.Error = saperr.NewA("MissingEndpoint", "'Endpoint' configuration is required for this service")
		}
	},
}
View Source
var ValidateReqSigProcessor = processors.DefaultProcessor{
	Name: "core.ValidateReqSigProcessor",
	Handler: func(t interface{}) {
		r, _ := t.(*request.Request)
		r.Sign()
	},
}

ValidateReqSigProcessor is a request handler to request's are valid

View Source
var ValidateResponseProcessor = processors.DefaultProcessor{
	Name: "core.ValidateResponseProcessor",
	Handler: func(t interface{}) {
		r := t.(*request.Request)
		if r.HTTPResponse.StatusCode == 0 || r.HTTPResponse.StatusCode >= 300 {

			r.Error = saperr.NewA(r.HTTPResponse.Status, r.Operation.Name)
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL