Debian Codegraph @

storage/2018-11-09/file/files/ranges_list.go

raw
1 package files
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "strings"
8
9 "github.com/Azure/go-autorest/autorest"
10 "github.com/Azure/go-autorest/autorest/azure"
11 "github.com/Azure/go-autorest/autorest/validation"
12 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
13 )
14
15 type ListRangesResult struct {
16 autorest.Response
17
18 Ranges []Range `xml:"Range"`
19 }
20
21 type Range struct {
22 Start string `xml:"Start"`
23 End string `xml:"End"`
24 }
25
26 // ListRanges returns the list of valid ranges for the specified File.
27 func (client Client) ListRanges(ctx context.Context, accountName, shareName, path, fileName string) (result ListRangesResult, err error) {
28 if accountName == "" {
29 return result, validation.NewError("files.Client", "ListRanges", "`accountName` cannot be an empty string.")
30 }
31 if shareName == "" {
32 return result, validation.NewError("files.Client", "ListRanges", "`shareName` cannot be an empty string.")
33 }
34 if strings.ToLower(shareName) != shareName {
35 return result, validation.NewError("files.Client", "ListRanges", "`shareName` must be a lower-cased string.")
36 }
37 if path == "" {
38 return result, validation.NewError("files.Client", "ListRanges", "`path` cannot be an empty string.")
39 }
40 if fileName == "" {
41 return result, validation.NewError("files.Client", "ListRanges", "`fileName` cannot be an empty string.")
42 }
43
44 req, err := client.ListRangesPreparer(ctx, accountName, shareName, path, fileName)
45 if err != nil {
46 err = autorest.NewErrorWithError(err, "files.Client", "ListRanges", nil, "Failure preparing request")
47 return
48 }
49
50 resp, err := client.ListRangesSender(req)
51 if err != nil {
52 result.Response = autorest.Response{Response: resp}
53 err = autorest.NewErrorWithError(err, "files.Client", "ListRanges", resp, "Failure sending request")
54 return
55 }
56
57 result, err = client.ListRangesResponder(resp)
58 if err != nil {
59 err = autorest.NewErrorWithError(err, "files.Client", "ListRanges", resp, "Failure responding to request")
60 return
61 }
62
63 return
64 }
65
66 // ListRangesPreparer prepares the ListRanges request.
67 func (client Client) ListRangesPreparer(ctx context.Context, accountName, shareName, path, fileName string) (*http.Request, error) {
68 if path != "" {
69 path = fmt.Sprintf("%s/", path)
70 }
71 pathParameters := map[string]interface{}{
72 "shareName": autorest.Encode("path", shareName),
73 "directory": autorest.Encode("path", path),
74 "fileName": autorest.Encode("path", fileName),
75 }
76
77 queryParameters := map[string]interface{}{
78 "comp": autorest.Encode("query", "rangelist"),
79 }
80
81 headers := map[string]interface{}{
82 "x-ms-version": APIVersion,
83 }
84
85 preparer := autorest.CreatePreparer(
86 autorest.AsContentType("application/xml; charset=utf-8"),
87 autorest.AsGet(),
88 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
89 autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters),
90 autorest.WithHeaders(headers),
91 autorest.WithQueryParameters(queryParameters))
92 return preparer.Prepare((&http.Request{}).WithContext(ctx))
93 }
94
95 // ListRangesSender sends the ListRanges request. The method will close the
96 // http.Response Body if it receives an error.
97 func (client Client) ListRangesSender(req *http.Request) (*http.Response, error) {
98 return autorest.SendWithSender(client, req,
99 azure.DoRetryWithRegistration(client.Client))
100 }
101
102 // ListRangesResponder handles the response to the ListRanges request. The method always
103 // closes the http.Response Body.
104 func (client Client) ListRangesResponder(resp *http.Response) (result ListRangesResult, err error) {
105 err = autorest.Respond(
106 resp,
107 client.ByInspecting(),
108 azure.WithErrorUnlessStatusCode(http.StatusOK),
109 autorest.ByUnmarshallingXML(&result),
110 autorest.ByClosing())
111 result.Response = autorest.Response{Response: resp}
112
113 return
114 }