1package directories
2
3import (
4	"context"
5	"net/http"
6	"strings"
7
8	"github.com/Azure/go-autorest/autorest"
9	"github.com/Azure/go-autorest/autorest/azure"
10	"github.com/Azure/go-autorest/autorest/validation"
11	"github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
12	"github.com/tombuildsstuff/giovanni/storage/internal/metadata"
13)
14
15type GetResult struct {
16	autorest.Response
17
18	// A set of name-value pairs that contain metadata for the directory.
19	MetaData map[string]string
20
21	// The value of this header is set to true if the directory metadata is completely
22	// encrypted using the specified algorithm. Otherwise, the value is set to false.
23	DirectoryMetaDataEncrypted bool
24}
25
26// Get returns all system properties for the specified directory,
27// and can also be used to check the existence of a directory.
28func (client Client) Get(ctx context.Context, accountName, shareName, path string) (result GetResult, err error) {
29	if accountName == "" {
30		return result, validation.NewError("directories.Client", "Get", "`accountName` cannot be an empty string.")
31	}
32	if shareName == "" {
33		return result, validation.NewError("directories.Client", "Get", "`shareName` cannot be an empty string.")
34	}
35	if strings.ToLower(shareName) != shareName {
36		return result, validation.NewError("directories.Client", "Get", "`shareName` must be a lower-cased string.")
37	}
38	if path == "" {
39		return result, validation.NewError("directories.Client", "Get", "`path` cannot be an empty string.")
40	}
41
42	req, err := client.GetPreparer(ctx, accountName, shareName, path)
43	if err != nil {
44		err = autorest.NewErrorWithError(err, "directories.Client", "Get", nil, "Failure preparing request")
45		return
46	}
47
48	resp, err := client.GetSender(req)
49	if err != nil {
50		result.Response = autorest.Response{Response: resp}
51		err = autorest.NewErrorWithError(err, "directories.Client", "Get", resp, "Failure sending request")
52		return
53	}
54
55	result, err = client.GetResponder(resp)
56	if err != nil {
57		err = autorest.NewErrorWithError(err, "directories.Client", "Get", resp, "Failure responding to request")
58		return
59	}
60
61	return
62}
63
64// GetPreparer prepares the Get request.
65func (client Client) GetPreparer(ctx context.Context, accountName, shareName, path string) (*http.Request, error) {
66	pathParameters := map[string]interface{}{
67		"shareName": autorest.Encode("path", shareName),
68		"directory": autorest.Encode("path", path),
69	}
70
71	queryParameters := map[string]interface{}{
72		"restype": autorest.Encode("query", "directory"),
73	}
74
75	headers := map[string]interface{}{
76		"x-ms-version": APIVersion,
77	}
78
79	preparer := autorest.CreatePreparer(
80		autorest.AsContentType("application/xml; charset=utf-8"),
81		autorest.AsGet(),
82		autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
83		autorest.WithPathParameters("/{shareName}/{directory}", pathParameters),
84		autorest.WithQueryParameters(queryParameters),
85		autorest.WithHeaders(headers))
86	return preparer.Prepare((&http.Request{}).WithContext(ctx))
87}
88
89// GetSender sends the Get request. The method will close the
90// http.Response Body if it receives an error.
91func (client Client) GetSender(req *http.Request) (*http.Response, error) {
92	return autorest.SendWithSender(client, req,
93		azure.DoRetryWithRegistration(client.Client))
94}
95
96// GetResponder handles the response to the Get request. The method always
97// closes the http.Response Body.
98func (client Client) GetResponder(resp *http.Response) (result GetResult, err error) {
99	if resp != nil && resp.Header != nil {
100		result.MetaData = metadata.ParseFromHeaders(resp.Header)
101		result.DirectoryMetaDataEncrypted = strings.EqualFold(resp.Header.Get("x-ms-server-encrypted"), "true")
102	}
103
104	err = autorest.Respond(
105		resp,
106		client.ByInspecting(),
107		azure.WithErrorUnlessStatusCode(http.StatusOK),
108		autorest.ByClosing())
109	result.Response = autorest.Response{Response: resp}
110
111	return
112}
113