| 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 | // Delete immediately deletes the file from the File Share. |
| 16 | func (client Client) Delete(ctx context.Context, accountName, shareName, path, fileName string) (result autorest.Response, err error) { |
| 17 | if accountName == "" { |
| 18 | return result, validation.NewError("files.Client", "Delete", "`accountName` cannot be an empty string.") |
| 19 | } |
| 20 | if shareName == "" { |
| 21 | return result, validation.NewError("files.Client", "Delete", "`shareName` cannot be an empty string.") |
| 22 | } |
| 23 | if strings.ToLower(shareName) != shareName { |
| 24 | return result, validation.NewError("files.Client", "Delete", "`shareName` must be a lower-cased string.") |
| 25 | } |
| 26 | if fileName == "" { |
| 27 | return result, validation.NewError("files.Client", "Delete", "`fileName` cannot be an empty string.") |
| 28 | } |
| 29 | |
| 30 | req, err := client.DeletePreparer(ctx, accountName, shareName, path, fileName) |
| 31 | if err != nil { |
| 32 | err = autorest.NewErrorWithError(err, "files.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, "files.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, "files.Client", "Delete", resp, "Failure responding to request") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | // DeletePreparer prepares the Delete request. |
| 53 | func (client Client) DeletePreparer(ctx context.Context, accountName, shareName, path, fileName string) (*http.Request, error) { |
| 54 | if path != "" { |
| 55 | path = fmt.Sprintf("%s/", path) |
| 56 | } |
| 57 | pathParameters := map[string]interface{}{ |
| 58 | "shareName": autorest.Encode("path", shareName), |
| 59 | "directory": autorest.Encode("path", path), |
| 60 | "fileName": autorest.Encode("path", fileName), |
| 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}{fileName}", pathParameters), |
| 72 | autorest.WithHeaders(headers)) |
| 73 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 74 | } |
| 75 | |
| 76 | // DeleteSender sends the Delete request. The method will close the |
| 77 | // http.Response Body if it receives an error. |
| 78 | func (client Client) DeleteSender(req *http.Request) (*http.Response, error) { |
| 79 | return autorest.SendWithSender(client, req, |
| 80 | azure.DoRetryWithRegistration(client.Client)) |
| 81 | } |
| 82 | |
| 83 | // DeleteResponder handles the response to the Delete request. The method always |
| 84 | // closes the http.Response Body. |
| 85 | func (client Client) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { |
| 86 | err = autorest.Respond( |
| 87 | resp, |
| 88 | client.ByInspecting(), |
| 89 | azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 90 | autorest.ByClosing()) |
| 91 | result = autorest.Response{Response: resp} |
| 92 | |
| 93 | return |
| 94 | } |