File: zz_generated_responder_policy.go

package info (click to toggle)
golang-github-azure-azure-storage-blob-go 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,084 kB
  • sloc: makefile: 3
file content (74 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package azblob

// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
	"bytes"
	"context"
	"encoding/xml"
	"github.com/Azure/azure-pipeline-go/pipeline"
	"io/ioutil"
)

type responder func(resp pipeline.Response) (result pipeline.Response, err error)

// ResponderPolicyFactory is a Factory capable of creating a responder pipeline.
type responderPolicyFactory struct {
	responder responder
}

// New creates a responder policy factory.
func (arpf responderPolicyFactory) New(next pipeline.Policy, po *pipeline.PolicyOptions) pipeline.Policy {
	return responderPolicy{next: next, responder: arpf.responder}
}

type responderPolicy struct {
	next      pipeline.Policy
	responder responder
}

// Do sends the request to the service and validates/deserializes the HTTP response.
func (arp responderPolicy) Do(ctx context.Context, request pipeline.Request) (pipeline.Response, error) {
	resp, err := arp.next.Do(ctx, request)
	if err != nil {
		return resp, err
	}
	return arp.responder(resp)
}

// validateResponse checks an HTTP response's status code against a legal set of codes.
// If the response code is not legal, then validateResponse reads all of the response's body
// (containing error information) and returns a response error.
func validateResponse(resp pipeline.Response, successStatusCodes ...int) error {
	if resp == nil {
		return NewResponseError(nil, nil, "nil response")
	}
	responseCode := resp.Response().StatusCode
	for _, i := range successStatusCodes {
		if i == responseCode {
			return nil
		}
	}
	// only close the body in the failure case. in the
	// success case responders will close the body as required.
	defer resp.Response().Body.Close()
	b, err := ioutil.ReadAll(resp.Response().Body)
	if err != nil {
		return err
	}
	// the service code, description and details will be populated during unmarshalling
	responseError := NewResponseError(nil, resp.Response(), resp.Response().Status)
	if len(b) > 0 {
		if err = xml.Unmarshal(b, &responseError); err != nil {
			return NewResponseError(err, resp.Response(), "failed to unmarshal response body")
		}
	}
	return responseError
}

// removes any BOM from the byte slice
func removeBOM(b []byte) []byte {
	// UTF8
	return bytes.TrimPrefix(b, []byte("\xef\xbb\xbf"))
}