|
Packit |
63bb0d |
package azblob
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Code generated by Microsoft (R) AutoRest Code Generator.
|
|
Packit |
63bb0d |
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
import (
|
|
Packit |
63bb0d |
"bytes"
|
|
Packit |
63bb0d |
"context"
|
|
Packit |
63bb0d |
"encoding/xml"
|
|
Packit |
63bb0d |
"github.com/Azure/azure-pipeline-go/pipeline"
|
|
Packit |
63bb0d |
"io/ioutil"
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type responder func(resp pipeline.Response) (result pipeline.Response, err error)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// ResponderPolicyFactory is a Factory capable of creating a responder pipeline.
|
|
Packit |
63bb0d |
type responderPolicyFactory struct {
|
|
Packit |
63bb0d |
responder responder
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// New creates a responder policy factory.
|
|
Packit |
63bb0d |
func (arpf responderPolicyFactory) New(next pipeline.Policy, po *pipeline.PolicyOptions) pipeline.Policy {
|
|
Packit |
63bb0d |
return responderPolicy{next: next, responder: arpf.responder}
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type responderPolicy struct {
|
|
Packit |
63bb0d |
next pipeline.Policy
|
|
Packit |
63bb0d |
responder responder
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Do sends the request to the service and validates/deserializes the HTTP response.
|
|
Packit |
63bb0d |
func (arp responderPolicy) Do(ctx context.Context, request pipeline.Request) (pipeline.Response, error) {
|
|
Packit |
63bb0d |
resp, err := arp.next.Do(ctx, request)
|
|
Packit |
63bb0d |
if err != nil {
|
|
Packit |
63bb0d |
return resp, err
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
return arp.responder(resp)
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// validateResponse checks an HTTP response's status code against a legal set of codes.
|
|
Packit |
63bb0d |
// If the response code is not legal, then validateResponse reads all of the response's body
|
|
Packit |
63bb0d |
// (containing error information) and returns a response error.
|
|
Packit |
63bb0d |
func validateResponse(resp pipeline.Response, successStatusCodes ...int) error {
|
|
Packit |
63bb0d |
if resp == nil {
|
|
Packit |
63bb0d |
return NewResponseError(nil, nil, "nil response")
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
responseCode := resp.Response().StatusCode
|
|
Packit |
63bb0d |
for _, i := range successStatusCodes {
|
|
Packit |
63bb0d |
if i == responseCode {
|
|
Packit |
63bb0d |
return nil
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
// only close the body in the failure case. in the
|
|
Packit |
63bb0d |
// success case responders will close the body as required.
|
|
Packit |
63bb0d |
defer resp.Response().Body.Close()
|
|
Packit |
63bb0d |
b, err := ioutil.ReadAll(resp.Response().Body)
|
|
Packit |
63bb0d |
if err != nil {
|
|
Packit |
63bb0d |
return err
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
// the service code, description and details will be populated during unmarshalling
|
|
Packit |
63bb0d |
responseError := NewResponseError(nil, resp.Response(), resp.Response().Status)
|
|
Packit |
63bb0d |
if len(b) > 0 {
|
|
Packit |
63bb0d |
if err = xml.Unmarshal(b, &responseError); err != nil {
|
|
Packit |
63bb0d |
return NewResponseError(err, resp.Response(), "failed to unmarshal response body")
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
return responseError
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// removes any BOM from the byte slice
|
|
Packit |
63bb0d |
func removeBOM(b []byte) []byte {
|
|
Packit |
63bb0d |
// UTF8
|
|
Packit |
63bb0d |
return bytes.TrimPrefix(b, []byte("\xef\xbb\xbf"))
|
|
Packit |
63bb0d |
}
|