| 1 | package messages |
| 2 | |
| 3 | import ( |
| 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 | type UpdateInput struct { |
| 15 | // A message must be in a format that can be included in an XML request with UTF-8 encoding. |
| 16 | // The encoded message can be up to 64 KB in size. |
| 17 | Message string |
| 18 | |
| 19 | // Specifies the valid pop receipt value required to modify this message. |
| 20 | PopReceipt string |
| 21 | |
| 22 | // Specifies the new visibility timeout value, in seconds, relative to server time. |
| 23 | // The new value must be larger than or equal to 0, and cannot be larger than 7 days. |
| 24 | // The visibility timeout of a message cannot be set to a value later than the expiry time. |
| 25 | // A message can be updated until it has been deleted or has expired. |
| 26 | VisibilityTimeout int |
| 27 | } |
| 28 | |
| 29 | // Update updates an existing message based on it's Pop Receipt |
| 30 | func (client Client) Update(ctx context.Context, accountName, queueName string, messageID string, input UpdateInput) (result autorest.Response, err error) { |
| 31 | if accountName == "" { |
| 32 | return result, validation.NewError("messages.Client", "Update", "`accountName` cannot be an empty string.") |
| 33 | } |
| 34 | if queueName == "" { |
| 35 | return result, validation.NewError("messages.Client", "Update", "`queueName` cannot be an empty string.") |
| 36 | } |
| 37 | if strings.ToLower(queueName) != queueName { |
| 38 | return result, validation.NewError("messages.Client", "Update", "`queueName` must be a lower-cased string.") |
| 39 | } |
| 40 | if input.PopReceipt == "" { |
| 41 | return result, validation.NewError("messages.Client", "Update", "`input.PopReceipt` cannot be an empty string.") |
| 42 | } |
| 43 | |
| 44 | req, err := client.UpdatePreparer(ctx, accountName, queueName, messageID, input) |
| 45 | if err != nil { |
| 46 | err = autorest.NewErrorWithError(err, "messages.Client", "Update", nil, "Failure preparing request") |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | resp, err := client.UpdateSender(req) |
| 51 | if err != nil { |
| 52 | result = autorest.Response{Response: resp} |
| 53 | err = autorest.NewErrorWithError(err, "messages.Client", "Update", resp, "Failure sending request") |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | result, err = client.UpdateResponder(resp) |
| 58 | if err != nil { |
| 59 | err = autorest.NewErrorWithError(err, "messages.Client", "Update", resp, "Failure responding to request") |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | // UpdatePreparer prepares the Update request. |
| 67 | func (client Client) UpdatePreparer(ctx context.Context, accountName, queueName string, messageID string, input UpdateInput) (*http.Request, error) { |
| 68 | pathParameters := map[string]interface{}{ |
| 69 | "queueName": autorest.Encode("path", queueName), |
| 70 | "messageID": autorest.Encode("path", messageID), |
| 71 | } |
| 72 | |
| 73 | queryParameters := map[string]interface{}{ |
| 74 | "popreceipt": autorest.Encode("query", input.PopReceipt), |
| 75 | "visibilitytimeout": autorest.Encode("query", input.VisibilityTimeout), |
| 76 | } |
| 77 | |
| 78 | headers := map[string]interface{}{ |
| 79 | "x-ms-version": APIVersion, |
| 80 | } |
| 81 | |
| 82 | body := QueueMessage{ |
| 83 | MessageText: input.Message, |
| 84 | } |
| 85 | |
| 86 | preparer := autorest.CreatePreparer( |
| 87 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 88 | autorest.AsPut(), |
| 89 | autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)), |
| 90 | autorest.WithPathParameters("/{queueName}/messages/{messageID}", pathParameters), |
| 91 | autorest.WithQueryParameters(queryParameters), |
| 92 | autorest.WithXML(body), |
| 93 | autorest.WithHeaders(headers)) |
| 94 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 95 | } |
| 96 | |
| 97 | // UpdateSender sends the Update request. The method will close the |
| 98 | // http.Response Body if it receives an error. |
| 99 | func (client Client) UpdateSender(req *http.Request) (*http.Response, error) { |
| 100 | return autorest.SendWithSender(client, req, |
| 101 | azure.DoRetryWithRegistration(client.Client)) |
| 102 | } |
| 103 | |
| 104 | // UpdateResponder handles the response to the Update request. The method always |
| 105 | // closes the http.Response Body. |
| 106 | func (client Client) UpdateResponder(resp *http.Response) (result autorest.Response, err error) { |
| 107 | err = autorest.Respond( |
| 108 | resp, |
| 109 | client.ByInspecting(), |
| 110 | azure.WithErrorUnlessStatusCode(http.StatusNoContent), |
| 111 | autorest.ByClosing()) |
| 112 | result = autorest.Response{Response: resp} |
| 113 | |
| 114 | return |
| 115 | } |