| 1 | package messages |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "time" |
| 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 | type GetInput struct { |
| 16 | // VisibilityTimeout specifies the new visibility timeout value, in seconds, relative to server time. |
| 17 | // The new value must be larger than or equal to 0, and cannot be larger than 7 days. |
| 18 | VisibilityTimeout *int |
| 19 | } |
| 20 | |
| 21 | // Get retrieves one or more messages from the front of the queue |
| 22 | func (client Client) Get(ctx context.Context, accountName, queueName string, numberOfMessages int, input GetInput) (result QueueMessagesListResult, err error) { |
| 23 | if accountName == "" { |
| 24 | return result, validation.NewError("messages.Client", "Get", "`accountName` cannot be an empty string.") |
| 25 | } |
| 26 | if queueName == "" { |
| 27 | return result, validation.NewError("messages.Client", "Get", "`queueName` cannot be an empty string.") |
| 28 | } |
| 29 | if strings.ToLower(queueName) != queueName { |
| 30 | return result, validation.NewError("messages.Client", "Get", "`queueName` must be a lower-cased string.") |
| 31 | } |
| 32 | if numberOfMessages < 1 || numberOfMessages > 32 { |
| 33 | return result, validation.NewError("messages.Client", "Get", "`numberOfMessages` must be between 1 and 32.") |
| 34 | } |
| 35 | if input.VisibilityTimeout != nil { |
| 36 | t := *input.VisibilityTimeout |
| 37 | maxTime := (time.Hour * 24 * 7).Seconds() |
| 38 | if t < 1 || t < int(maxTime) { |
| 39 | return result, validation.NewError("messages.Client", "Get", "`input.VisibilityTimeout` must be larger than or equal to 1 second, and cannot be larger than 7 days.") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | req, err := client.GetPreparer(ctx, accountName, queueName, numberOfMessages, input) |
| 44 | if err != nil { |
| 45 | err = autorest.NewErrorWithError(err, "messages.Client", "Get", nil, "Failure preparing request") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | resp, err := client.GetSender(req) |
| 50 | if err != nil { |
| 51 | result.Response = autorest.Response{Response: resp} |
| 52 | err = autorest.NewErrorWithError(err, "messages.Client", "Get", resp, "Failure sending request") |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | result, err = client.GetResponder(resp) |
| 57 | if err != nil { |
| 58 | err = autorest.NewErrorWithError(err, "messages.Client", "Get", resp, "Failure responding to request") |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | // GetPreparer prepares the Get request. |
| 66 | func (client Client) GetPreparer(ctx context.Context, accountName, queueName string, numberOfMessages int, input GetInput) (*http.Request, error) { |
| 67 | pathParameters := map[string]interface{}{ |
| 68 | "queueName": autorest.Encode("path", queueName), |
| 69 | } |
| 70 | |
| 71 | queryParameters := map[string]interface{}{ |
| 72 | "numofmessages": autorest.Encode("query", numberOfMessages), |
| 73 | } |
| 74 | |
| 75 | if input.VisibilityTimeout != nil { |
| 76 | queryParameters["visibilitytimeout"] = autorest.Encode("query", *input.VisibilityTimeout) |
| 77 | } |
| 78 | |
| 79 | headers := map[string]interface{}{ |
| 80 | "x-ms-version": APIVersion, |
| 81 | } |
| 82 | |
| 83 | preparer := autorest.CreatePreparer( |
| 84 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 85 | autorest.AsGet(), |
| 86 | autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)), |
| 87 | autorest.WithPathParameters("/{queueName}/messages", pathParameters), |
| 88 | autorest.WithQueryParameters(queryParameters), |
| 89 | autorest.WithHeaders(headers)) |
| 90 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 91 | } |
| 92 | |
| 93 | // GetSender sends the Get request. The method will close the |
| 94 | // http.Response Body if it receives an error. |
| 95 | func (client Client) GetSender(req *http.Request) (*http.Response, error) { |
| 96 | return autorest.SendWithSender(client, req, |
| 97 | azure.DoRetryWithRegistration(client.Client)) |
| 98 | } |
| 99 | |
| 100 | // GetResponder handles the response to the Get request. The method always |
| 101 | // closes the http.Response Body. |
| 102 | func (client Client) GetResponder(resp *http.Response) (result QueueMessagesListResult, err error) { |
| 103 | err = autorest.Respond( |
| 104 | resp, |
| 105 | client.ByInspecting(), |
| 106 | autorest.ByUnmarshallingXML(&result), |
| 107 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 108 | autorest.ByClosing()) |
| 109 | result.Response = autorest.Response{Response: resp} |
| 110 | |
| 111 | return |
| 112 | } |