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