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 GetMetaDataResult struct {
16	autorest.Response
17
18	MetaData map[string]string
19}
20
21// GetMetaData returns all user-defined metadata for the specified directory
22func (client Client) GetMetaData(ctx context.Context, accountName, shareName, path string) (result GetMetaDataResult, err error) {
23	if accountName == "" {
24		return result, validation.NewError("directories.Client", "GetMetaData", "`accountName` cannot be an empty string.")
25	}
26	if shareName == "" {
27		return result, validation.NewError("directories.Client", "GetMetaData", "`shareName` cannot be an empty string.")
28	}
29	if strings.ToLower(shareName) != shareName {
30		return result, validation.NewError("directories.Client", "GetMetaData", "`shareName` must be a lower-cased string.")
31	}
32	if path == "" {
33		return result, validation.NewError("directories.Client", "GetMetaData", "`path` cannot be an empty string.")
34	}
35
36	req, err := client.GetMetaDataPreparer(ctx, accountName, shareName, path)
37	if err != nil {
38		err = autorest.NewErrorWithError(err, "directories.Client", "GetMetaData", nil, "Failure preparing request")
39		return
40	}
41
42	resp, err := client.GetMetaDataSender(req)
43	if err != nil {
44		result.Response = autorest.Response{Response: resp}
45		err = autorest.NewErrorWithError(err, "directories.Client", "GetMetaData", resp, "Failure sending request")
46		return
47	}
48
49	result, err = client.GetMetaDataResponder(resp)
50	if err != nil {
51		err = autorest.NewErrorWithError(err, "directories.Client", "GetMetaData", resp, "Failure responding to request")
52		return
53	}
54
55	return
56}
57
58// GetMetaDataPreparer prepares the GetMetaData request.
59func (client Client) GetMetaDataPreparer(ctx context.Context, accountName, shareName, path string) (*http.Request, error) {
60	pathParameters := map[string]interface{}{
61		"shareName": autorest.Encode("path", shareName),
62		"directory": autorest.Encode("path", path),
63	}
64
65	queryParameters := map[string]interface{}{
66		"restype": autorest.Encode("query", "directory"),
67		"comp":    autorest.Encode("query", "metadata"),
68	}
69
70	headers := map[string]interface{}{
71		"x-ms-version": APIVersion,
72	}
73
74	preparer := autorest.CreatePreparer(
75		autorest.AsContentType("application/xml; charset=utf-8"),
76		autorest.AsGet(),
77		autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
78		autorest.WithPathParameters("/{shareName}/{directory}", pathParameters),
79		autorest.WithQueryParameters(queryParameters),
80		autorest.WithHeaders(headers))
81	return preparer.Prepare((&http.Request{}).WithContext(ctx))
82}
83
84// GetMetaDataSender sends the GetMetaData request. The method will close the
85// http.Response Body if it receives an error.
86func (client Client) GetMetaDataSender(req *http.Request) (*http.Response, error) {
87	return autorest.SendWithSender(client, req,
88		azure.DoRetryWithRegistration(client.Client))
89}
90
91// GetMetaDataResponder handles the response to the GetMetaData request. The method always
92// closes the http.Response Body.
93func (client Client) GetMetaDataResponder(resp *http.Response) (result GetMetaDataResult, err error) {
94	if resp != nil && resp.Header != nil {
95		result.MetaData = metadata.ParseFromHeaders(resp.Header)
96	}
97
98	err = autorest.Respond(
99		resp,
100		client.ByInspecting(),
101		azure.WithErrorUnlessStatusCode(http.StatusOK),
102		autorest.ByClosing())
103	result.Response = autorest.Response{Response: resp}
104
105	return
106}
107